feat: Add JSON Schema to ArkType conversion
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 correctly inferred as `Type<string>`, and can now be used as-per any other ArkType type
Remaining Work Before Merging
-
For some reason the
innerParseJsonSchemaconst is giving a type errorType "string" is not assignable to type "never". It seems my explicit type definition is somehow wrong 🤷 -
For some reason
parseJsonSchema({type: "array", items: {type: "string"}})raisesTypeError: this.Schema1Apply is not a functiondespite working against an earlier ArkType version (probably a bug in ArkType that I've not yet delved into to properly give details). -
Similar to above,
const t = parseJsonSchema({oneOf: [{type: "string"}, {type: "number"}]})raisesTypeError: this.CompositionKeywords1Apply is not a function: -
Tests are only partially implemented.
- I wasn't entirely confident on what should/shouldn't be tested, and where things should/shouldn't be tested, so am waiting on feedback for this before finishing the tests.
- For example, I have tests for
{type: "string", "maxLength": 5}and{type: "string", "pattern": "es"}, but not{type: "string", "maxLength": 5, "pattern": "es"}. Is it worth having tests for the various "permutations"? I think this is perhaps overly excessive?
- For example, I have tests for
- All test files also give this linting errors stating it can't find the
@ark/jsonschemapackage:
- I wasn't entirely confident on what should/shouldn't be tested, and where things should/shouldn't be tested, so am waiting on feedback for this before finishing the tests.
-
Haven't yet properly filled out
ark/jsonschema/CHANGELOG.md. -
Constraints are correctly inferred, but should ideally be displayed more "prettily" (see below image):
-
I'd like to get custom type-errors working again, however the current problem is that calling
parseJsonSchemawith an invalid JSON Schema always results in aexcessively deep and possibly infiniteerror, making this [seemingly] impossible.
Known Limitations (things I didn't implement from JSON Schema spec)
- No support for JSON Schema
dependencieskeyword 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.
- Sort of not required, since you can write the same (admittedly more verbosely) by combining
-
multipleOfon numbers only supports integer divisors, due to ArkType only supporting integer divisors.