json-schema-merge-allof
Merge schemas combined using allOf into a more readable composed schema free from allOf.
npm install json-schema-compare --save
Features
- Real and safe merging of schemas combined with allOf
- Results in a more readable root schema
- Removes almost all logical impossibilities
- Throws if no logical intersection is found (your schema would not validate anything from the start)
- Validates in a way not possible by regular simple meta validators
- Pluggable keyword resolvers
- Option to override common impossibility like adding properties when using additionalProperties: false
- Supports all json schema keywords
How
Since allOf require ALL schemas provided (including the parent schema) to apply, we can iterate over all the schemas, extracting all the values for say, type, and find the intersection of valid values. Here is an example:
{
type: ['object', 'null'],
additionalProperties: {
type: 'string',
minLength: 5
},
allOf: [{
type: ['array', 'object'],
additionalProperties: {
type: 'string',
minLength: 10,
maxLength: 20
}
}]
}
This result in the schema :
{
type: 'object',
additionalProperties: {
type: 'string',
minLength: 10,
maxLength: 20
}
}
Notice that type now excludes null and array since those are not logically possible. Also minLength is raised to 10. The other properties have no conflict and are merged into the root schema with no resolving needed.
For other keywords other methods are used, here are some simple examples:
- minLength, minimum, minItems etc chooses the highest value of the conflicting values.
- maxLength, maximum, maxItems etc chooses the lowest value of the conflicting values.
- uniqueItems is true if any of the conflicting values are true
As you can see above the strategy is to choose the most restrictive of the set of values that conflict. For some keywords that is done by intersection, for others like required it is done by a union of all the values, since that is the most restrictive.
What you are left with is a schema completely free of allOf. Except for in a couple of values that are impossible to properly intersect/combine:
pattern
If a schema have the pattern keyword and we have a conflict, then we need to leave that expressed like this:
{
type: 'string',
allOf: [{
pattern: '\\w+\\s\\w+'
}, {
pattern: '123$'
}]
}
Regular expressions does not have an AND operator, only OR.
not
When multiple conflicting not values are found, we also use the approach that pattern use, but instead of allOf we use anyOf. When extraction of common rules from anyOf is in place this can be further simplified.
Options
resolvers Object
Override any default resolver like this:
mergeAllOf(schema, {
resolvers: {
title: function(values, key, mergeSchemas) {
}
}
The function is passed:
- values: an array of the conflicting values that need to be resolved
- key the name of the keyword that caused the resolver to be called (useful if you use the same resolver for multiple keywords)
- mergeSchemas a function you can call that merges an array of schemas
ignoreAdditionalProperties default false
Allows you to combine schema properties even though some schemas have additionalProperties: false
The resulting schema will still get additionalProperties set to false. This is the most common issue people face when trying to expand schemas using allOf and a limitation of the json schema spec.
Resolvers
Resolvers are called whenever multiple conflicting values are found on the same position in the schemas.
You can override a resolver by supplying it in the options.
Lossy vs lossless
All built in reducers for validation keywords are lossless, meaning that they don't remove or add anything in terms of validation.
For meta keywords like title, description, $id, $schema, default the strategy is to use the first possible value if there are conflicting ones. So the root schema is prioritized. This process possibly removes some meta information from your schema. So it's lossy. Override this by providing custom resolvers.
$ref
If one of your schemas contain a $ref property you should resolve them using a ref resolver like json-schema-ref-parser to dereference your schema for you first. Resolving $refs are not the task of this library.
Other libraries
There exists some libraries that claim to merge schemas combined with allOf, but they just merge schemas using a very basic logic. Basically just the same as lodash merge. So you risk ending up with a schema that allows more or less than the original schema would allow.
Restrictions
We cannot merge schemas that are a logical impossibility, like:
{
type: 'object',
allOf: [{
type: 'array'
}]
}
The library will then throw an error reporting the values that had no valid intersection. But then again, your original schema wouldn't validate anything either.
Roadmap
Contributing
Create tests for new functionality and follow the eslint rules.
License
MIT © Martin Hansen