Imagining a future JSON schema.
Mainly because JSON schema's syntax is inconsistent. Every draft there are new keywords being added, and it's hard to keep track of them all. Schema2 built upon the @zuze-lab/schema but got rid of the functional flavor while keeping the abstract syntax tree.
Popular validators like ajv or hyperjump are undoubtedly powerful, but, having evolved with JSON schema, have lots of complex configuration options, inconsistent syntax, and have become bloated.
schema2
, at it's core, is only about 2kb minzipped and under 7kb minzipped for all 7 schemas (mixed, number, boolean, string, date, object, array) including all 24 transformation operators and all 35 test operators.
The only keywords you need to know about for Schema2 are:
type
- specifies the type of schemalabel
- a label, helpful, for error messagestests
- all the validations that you want to do (this is where all the keywords go)transforms
- ability to transform valuestypeError
- ability to specify an error message for invalid type during validationinner
- the inner schema definition for objects and arrays/tuplesconditions
- the ability to modify the validations or transforms of any schema depending on the value being validated or external contextnullable
- null and undefined are treated as special values. Tests WILL NOT be run on null and undefined values. A specialnonNullable
error will be returned for a given schema if null is received as a value for a schema wherenullable
is false
These are the list of validators shipped with schema2
:
{
"type": "object",
"label": "user",
"inner": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
}
},
"tests": [
{
"name": "keys",
"schema": {
"type": "string",
"tests": [
{
"type": "oneOf",
"values": ["firstName", "lastName"]
}
]
}
}
],
"conditions": [
{
"when": {
"user.firstName": {
"tests": [{ "type": "required" }]
}
},
"then": {
"inner": {
"lastName": {
"tests": [{ "type": "required" }]
}
}
}
}
]
}