A JavaScript library for comparing JSON Schema specificity and extending schemas. This library helps determine if one JSON schema is more specific than another, meaning that any JSON document that would be validated by the extension schema would also be validated by the original schema. It also provides functionality to extend schemas by merging them.
Try out the library with our interactive demo. The demo allows you to input two JSON schemas and instantly see if one is more specific than the other.
npm install json-schema-specificity
import { isMoreSpecific, extendSchema } from 'json-schema-specificity';
// Check if one schema is more specific than another
const original = {
type: 'object',
properties: {
name: { type: 'string' }
}
};
const extension = {
type: 'object',
properties: {
name: { type: 'string', minLength: 1 }
}
};
const isMoreSpecificResult = isMoreSpecific(original, extension);
// result: true (because extension is more specific than original)
// Extend a schema with another
const base = {
type: 'object',
properties: {
user: {
type: 'object',
properties: {
name: { type: 'string' }
}
}
}
};
const delta = {
properties: {
user: {
properties: {
age: { type: 'number' }
}
}
}
};
const extended = extendSchema(base, delta);
// result: {
// type: 'object',
// properties: {
// user: {
// type: 'object',
// properties: {
// name: { type: 'string' },
// age: { type: 'number' }
// }
// }
// }
// }
Determines if the extension schema is more specific than the original schema.
-
original
(Object): The original JSON schema -
extension
(Object): The extension JSON schema to compare
-
boolean
: Returnstrue
if the extension schema is more specific than the original schema,false
otherwise.
Recursively merges two JSON schemas, with the extension schema overriding or extending the base schema.
-
base
(Object): The base JSON schema -
extension
(Object): The extension JSON schema to merge with the base
-
Object
: A new object representing the merged schema
- Supports JSON Schema Draft-07
- Handles various schema keywords:
- Type compatibility (including number/integer relationships)
- Required properties
- Property constraints
- Numeric constraints (minimum, maximum, multipleOf)
- String constraints (minLength, maxLength, pattern)
- Array constraints (minItems, maxItems, uniqueItems)
- Enum values
- Const values
- Additional properties
- Schema extension capabilities:
- Deep merging of nested objects
- Array handling
- Primitive value overrides
- Null/undefined handling
isMoreSpecific(
{ type: 'number' },
{ type: 'integer' }
); // true
isMoreSpecific(
{ type: 'number', minimum: 0, maximum: 100 },
{ type: 'number', minimum: 10, maximum: 50 }
); // true
isMoreSpecific(
{ type: 'array', items: { type: 'string' }, maxItems: 5 },
{ type: 'array', items: { type: 'string' }, minItems: 1, maxItems: 3 }
); // true
extendSchema(
{ type: 'object', required: ['name'] },
{ additionalProperties: false }
); // { type: 'object', required: ['name'], additionalProperties: false }
ISC
Contributions are welcome! Please feel free to submit a Pull Request.