# 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`](https://arktype.io/docs/json-schema/from-json-schema) 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:

```ts
import { fromJsonSchema } from "arktype"
import { z } from "zod"

const V1 = fromJsonSchema(
	z.object({ id: z.string(), age: z.number().int().min(0) })
)

const V2 = fromJsonSchema({
	type: "object",
	properties: { id: { type: "string" }, age: { type: "number" } },
	required: ["id"]
})

// V2 is backwards compatible: it accepts everything V1 did
V1.extends(V2) // true
// but not the other way around: V2 accepts { id: "a" }
V2.extends(V1) // false
```

Use 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 |

```ts
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))
) // false
```

Each 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](https://arktype.io/docs/json-schema/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](https://arktype.io/docs/json-schema/support) lists as opaque, like `not`, `uniqueItems` and `if`, and a Type's [`narrow`](https://arktype.io/docs/expressions#narrow) predicates:

```ts
import { fromJsonSchema } from "arktype"

// true, but not provable through `not`
fromJsonSchema({ type: "string" }).extends(
	fromJsonSchema({ not: { type: "number" } })
) // false
```

An 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:

```ts
import { fromJsonSchema } from "arktype"
import { z } from "zod"

const X = fromJsonSchema(z.string().refine(s => s === "x"))

// the refinement isn't in its JSON Schema
fromJsonSchema(z.string()).extends(X) // true
```

A `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:

```ts
import { fromJsonSchema } from "arktype"

const User = type({ name: "string" })

const JsonUser = fromJsonSchema({
	type: "object",
	properties: { name: { type: "string" } },
	required: ["name"]
})

JsonUser.extends(User) // true
// User accepts Object.assign([], { name: "" })
User.extends(JsonUser) // false
fromJsonSchema(User.toJsonSchema()).extends(JsonUser) // true
```

To 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](https://arktype.io/docs/json-schema/from-json-schema#how-it-reads-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:

```ts
import { fromJsonSchema } from "arktype"
import { z } from "zod"

const User = fromJsonSchema(z.object({ id: z.string(), age: z.number() }))

const Capped = User.and(
	fromJsonSchema({ type: "object", properties: { age: { maximum: 120 } } })
)

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:

```ts
import { fromJsonSchema } from "arktype"

fromJsonSchema({
	$defs: { Name: { type: "string" } },
	allOf: [{ $ref: "#/$defs/Name" }, { minLength: 2 }]
}).toJsonSchema()
```

```json
{
	"$schema": "https://json-schema.org/draft/2020-12/schema",
	"type": "string",
	"minLength": 2
}
```

It takes the [usual options](https://arktype.io/docs/json-schema/to-json-schema) 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](https://arktype.io/docs/json-schema/to-json-schema#fallback-codes):

```ts
import { fromJsonSchema } from "arktype"

fromJsonSchema({ type: "array", uniqueItems: true }).toJsonSchema({
	fallback: { predicate: ctx => ({ ...ctx.base, uniqueItems: true }) }
})
```
