Compare schemas
Check whether JSON Schemas, Zod schemas and Types accept the same values, a subset of each other's or none in common, and combine them.
Every Type knows the set of values it accepts, and fromJsonSchema reads a JSON Schema or a Standard JSON Schema, like a Zod schema, into a Type. Together, they compare schemas in any of those formats with each other and with your own Types:
import { fromJsonSchema } from "arktype"
import { z } from "zod"
const const V1: Type<{
id: string;
age: number;
}>
V1 = fromJsonSchema(
z.object({ id: z.string(), age: z.number().int().min(0) })
)
const const V2: Type<{
id: string;
age?: number;
}>
V2 = fromJsonSchema({
type: "object",
properties: { id: { type: "string" }, age: { type: "number" } },
required: ["id"]
})
// V2 is backwards compatible: it accepts everything V1 did
const V1: Type<{
id: string;
age: number;
}>
V1.extends(const V2: Type<{
id: string;
age?: number;
}>
V2) // true
// but not the other way around: V2 accepts { id: "a" }
const V2: Type<{
id: string;
age?: number;
}>
V2.extends(const V1: Type<{
id: string;
age: number;
}>
V1) // falseUse it to check in CI that a new version of an API schema still accepts what the old one did, to find out whether two schemas can ever accept the same value, to merge schemas from different libraries into one, or to normalize any of them to JSON Schema.
Comparisons
| method | true means |
|---|---|
l.extends(r) | every value l accepts, r accepts |
l.equals(r) | l and r accept exactly the same values |
l.overlaps(r) | some value is accepted by both |
import { fromJsonSchema } from "arktype"
import { z } from "zod"
fromJsonSchema({ type: "integer", minimum: 0 }).equals("number.integer >= 0") // true
fromJsonSchema({ enum: ["a", "b"] }).extends(
fromJsonSchema({ type: "string", maxLength: 1 })
) // true
fromJsonSchema({ type: "string", maxLength: 2 }).overlaps(
fromJsonSchema(z.string().min(3))
) // falseEach method takes a definition, so pass a JSON Schema through fromJsonSchema first: .or({ type: "number" }) is an object with a type key.
What true proves
A comparison is sound for what each schema is read as: true is a proof. A Type is read exactly and a JSON Schema as Keyword support describes.
false from .extends() or .equals() means the relation couldn't be proven, either because it doesn't hold or because a schema checks something the algebra can't reason about. Those are the JSON Schema keywords Keyword support lists as opaque, like not, uniqueItems and if, and a Type's narrow predicates:
import { fromJsonSchema } from "arktype"
// true, but not provable through `not`
fromJsonSchema({ type: "string" }).extends(
fromJsonSchema({ not: { type: "number" } })
) // falseAn opaque keyword is still compared with itself, so a schema with uniqueItems: true and a maxItems extends one with only uniqueItems: true.
Standard Schemas are read as their JSON Schema
A Standard Schema is read as the JSON Schema of its input, so what that can't express, like Zod's refinements, transforms and undefined, is invisible, and true proves the relation only for those JSON Schemas:
import { fromJsonSchema } from "arktype"
import { z } from "zod"
const const X: Type<string>X = fromJsonSchema(z.string().refine(s => s === "x"))
// the refinement isn't in its JSON Schema
fromJsonSchema(z.string()).extends(const X: Type<string>X) // trueA format like Zod's z.url() writes is an annotation, as JSON Schema defaults to, so it accepts any string too.
arktype objects can be arrays
ArkType's object includes arrays, like TypeScript's. JSON Schema's "object" excludes them. An arktype Type is compared with its own meaning, so it accepts values a JSON Schema with the same properties doesn't:
import { fromJsonSchema } from "arktype"
const const User: Type<{
name: string;
}>
User = type({ name: "string" })
const const JsonUser: Type<{
name: string;
}>
JsonUser = fromJsonSchema({
type: "object",
properties: { name: { type: "string" } },
required: ["name"]
})
const JsonUser: Type<{
name: string;
}>
JsonUser.extends(const User: Type<{
name: string;
}>
User) // true
// User accepts Object.assign([], { name: "" })
const User: Type<{
name: string;
}>
User.extends(const JsonUser: Type<{
name: string;
}>
JsonUser) // false
fromJsonSchema(const User: Type<{
name: string;
}>
User.toJsonSchema()).extends(const JsonUser: Type<{
name: string;
}>
JsonUser) // trueTo compare a Type with JSON's meaning, read its .toJsonSchema() with fromJsonSchema.
OpenAPI
OpenAPI 3.0's nullable: true adds null to the type beside it, as ajv reads it, and the schema's other keywords still apply, so { type: "string", enum: ["x"], nullable: true } is "x". Without a type — beside allOf, anyOf, oneOf, $ref, enum, const, or any other subschema — nullable: true unions the schema with null. A $ref into a document's components resolves when the document is spread into the schema that refers to it, as From JSON Schema shows.
Combining
.and() returns a Type of the values both accept, and .or() one of the values either accepts, without a branch another already accepts. .and() throws a ParseError if no value satisfies both, so check .overlaps() first if they may not:
import { fromJsonSchema } from "arktype"
import { z } from "zod"
const const User: Type<{
id: string;
age: number;
}>
User = fromJsonSchema(z.object({ id: z.string(), age: z.number() }))
const const Capped: Type<{
id: string;
age: number;
}>
Capped = const User: Type<{
id: string;
age: number;
}>
User.and(
fromJsonSchema({ type: "object", properties: { age: { maximum: 120 } } })
)
const Capped: Type<{
id: string;
age: number;
}>
Capped.expression // "{ age: number <= 120, id: string } & isNonArray"
fromJsonSchema(z.string()).or(fromJsonSchema({ type: "number" })).expression // "number | string"Normalizing
.toJsonSchema() writes any of them back as JSON Schema, so it converts a schema from any format, or normalizes one JSON Schema into another:
import { fromJsonSchema } from "arktype"
fromJsonSchema({
$defs: { Name: { type: "string" } },
allOf: [{ $ref: "#/$defs/Name" }, { minLength: 2 }]
}).toJsonSchema(){
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "string",
"minLength": 2
}It takes the usual options and throws the same way. A keyword read opaquely, like uniqueItems, has no JSON Schema of its own once it's read, so handle its predicate fallback:
import { fromJsonSchema } from "arktype"
fromJsonSchema({ type: "array", uniqueItems: true }).toJsonSchema({
fallback: { predicate: ctx => ({ ...ctx.base, uniqueItems: true }) }
})