json-schema-specificity v2.0.0
json-schema-specificity
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.
Live Demo
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.
Installation
npm install json-schema-specificity
Usage
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' }
// }
// }
// }
// }
API
isMoreSpecific(original, extension)
Determines if the extension schema is more specific than the original schema.
Parameters
original
(Object): The original JSON schemaextension
(Object): The extension JSON schema to compare
Returns
boolean
: Returnstrue
if the extension schema is more specific than the original schema,false
otherwise.
extendSchema(base, extension)
Recursively merges two JSON schemas, with the extension schema overriding or extending the base schema.
Parameters
base
(Object): The base JSON schemaextension
(Object): The extension JSON schema to merge with the base
Returns
Object
: A new object representing the merged schema
Features
- 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
Examples
Type Compatibility
isMoreSpecific(
{ type: 'number' },
{ type: 'integer' }
); // true
Numeric Constraints
isMoreSpecific(
{ type: 'number', minimum: 0, maximum: 100 },
{ type: 'number', minimum: 10, maximum: 50 }
); // true
Array Constraints
isMoreSpecific(
{ type: 'array', items: { type: 'string' }, maxItems: 5 },
{ type: 'array', items: { type: 'string' }, minItems: 1, maxItems: 3 }
); // true
Schema Extension
extendSchema(
{ type: 'object', required: ['name'] },
{ additionalProperties: false }
); // { type: 'object', required: ['name'], additionalProperties: false }
License
ISC
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.