Skip to content

feat: support JSON Schema as input for a Type - #1159

Merged
ssalbdivad merged 144 commits into
arktypeio:mainfrom
TizzySaurus:json-schema-to-arktype
Apr 1, 2025
Merged

ssalbdivad merged 144 commits into
arktypeio:mainfrom
TizzySaurus:json-schema-to-arktype

Conversation

@TizzySaurus

@TizzySaurus TizzySaurus commented Oct 2, 2024 •

Copy link
Copy Markdown
Contributor

Closes #729.

What

This PR adds @ark/jsonschema, which is a library enabling users to convert a valid JSON Schema schema into an ArkType type.

API

Below is a basic sample-use of @ark/jsonschema:

import { parseJsonSchema } from "@ark/jsonschema"

const type = parseJsonSchema({type: "string"})
// ^? 
// NB: `type` is typed as `Type.Any`

Known Limitations (things I didn't implement from JSON Schema spec)

  • No type inference (this was removed in 07a5215, per discussion)
  • No support for JSON Schema dependencies keyword on objects.
  • No support for JSON Schema if/else/then.
    • Sort of not required, since you can write the same (admittedly more verbosely) by combining any/oneOf/allOf/not.
  • multipleOf on numbers only supports integer divisors, due to ArkType only supporting integer divisors.

ssalbdivad and others added 30 commits June 5, 2024 01:23

@ssalbdivad ssalbdivad left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH lost track of a couple comments so submitting them now haha

).type.errors.snap(
`Argument of type '{ type: "object"; propertyNames: { type: "number"; }; }' is not assignable to parameter of type 'JsonSchemaOrBoolean'.` +
`The types of 'propertyNames.type' are incompatible between these types.` +
`Type '"number"' is not assignable to type '"string"'.`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to make these slightly less fragile, I often just extract the snippet that most captures the error like:

.type.errors(
				`Type '"number"' is not assignable to type '"string"'.`
		)

(passing a value directly to .errors or .toString allows regex or partial matches)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to work here...
image

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there's something actually wrong with the string somehow? We're using it in lots of places:

image

Anyways not a blocker.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it works on other places, just not here for some reason 🤷

Comment thread ark/jsonschema/__tests__/object.test.ts Resolved
Comment thread ark/jsonschema/object.ts Resolved
@ssalbdivad ssalbdivad moved this from In progress to Implemented in arktypeio Mar 27, 2025
@TizzySaurus
TizzySaurus force-pushed the json-schema-to-arktype branch from 813cb25 to a628be9 Compare March 29, 2025 20:00
@TizzySaurus
TizzySaurus force-pushed the json-schema-to-arktype branch from a628be9 to b31364f Compare March 29, 2025 20:00
@TizzySaurus
TizzySaurus force-pushed the json-schema-to-arktype branch from 884224c to 825d78e Compare March 29, 2025 21:02
@TizzySaurus
TizzySaurus force-pushed the json-schema-to-arktype branch from 90c9ca5 to 98687a0 Compare April 1, 2025 20:45

@ssalbdivad ssalbdivad left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just want https://github.com/arktypeio/arktype/pull/1159/files#r2011203523 fixed and we're good to ship 🙌🙌🙌🙌

Amazing work on this. You know you were thorough when you wanted to handle so many edge cases you ended up adding new type reductions to ArkType in mutliple cases 😊

).type.errors.snap(
`Argument of type '{ type: "object"; propertyNames: { type: "number"; }; }' is not assignable to parameter of type 'JsonSchemaOrBoolean'.` +
`The types of 'propertyNames.type' are incompatible between these types.` +
`Type '"number"' is not assignable to type '"string"'.`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there's something actually wrong with the string somehow? We're using it in lots of places:

image

Anyways not a blocker.

export type Branch = Constrainable | Const | String | Numeric | Object | Array
type Composition = Union | OneOf | Intersection | Not

type NonBooleanBranch =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, but intuitively I think of it as meaning JsonObject | JsonArray | string | number | null.

…les regexes internally, so don't have to do it here as well)
@ssalbdivad
ssalbdivad merged commit 882559a into arktypeio:main Apr 1, 2025
@github-project-automation github-project-automation Bot moved this from Implemented to Done (merged or closed) in arktypeio Apr 1, 2025
@gabrielgrant

Copy link
Copy Markdown
Contributor

Wow, this is huge -- amazing work you two, thanks so much for getting this landed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done (merged or closed)

Development

Successfully merging this pull request may close these issues.

Support JSON-schema as an input format

3 participants