Package Exports
- json-schema-specificity
- json-schema-specificity/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (json-schema-specificity) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
json-schema-specificity
A JavaScript library for working with JSON Schema specificity. This library helps you:
- Determine if one JSON schema is more specific than another
- Create extension schemas by specifying only the changes you want to make
Live Demos
- Schema Comparison Demo - Compare two schemas to check if one is more specific than the other
- Extension Creator Demo - Create a more specific schema by providing only the changes you want to make
Installation
npm install json-schema-specificityUsage
import { isMoreSpecific } from 'json-schema-specificity';
const original = {
type: 'object',
properties: {
name: { type: 'string' }
}
};
const extension = {
type: 'object',
properties: {
name: { type: 'string', minLength: 1 }
}
};
const result = isMoreSpecific(original, extension);
// result: true (because extension is more specific than original)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: Returnstrueif the extension schema is more specific than the original schema,falseotherwise.
createExtension(base, delta)
Creates a new schema by merging a base schema with delta changes, ensuring the result is more specific than the base schema.
Parameters
base(Object): The base JSON schemadelta(Object): The delta changes to apply
Returns
Object: A new schema that extends the base schema with the delta changes
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
Examples
Comparing Schema Specificity
Type Compatibility
isMoreSpecific(
{ type: 'number' },
{ type: 'integer' }
); // trueNumeric Constraints
isMoreSpecific(
{ type: 'number', minimum: 0, maximum: 100 },
{ type: 'number', minimum: 10, maximum: 50 }
); // trueArray Constraints
isMoreSpecific(
{ type: 'array', items: { type: 'string' }, maxItems: 5 },
{ type: 'array', items: { type: 'string' }, minItems: 1, maxItems: 3 }
); // trueCreating Extension Schemas
import { createExtension } from 'json-schema-specificity';
const base = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' }
}
};
const delta = {
properties: {
name: { minLength: 1 },
age: { minimum: 0, maximum: 120 }
}
};
const extension = createExtension(base, delta);
// Result:
// {
// type: 'object',
// properties: {
// name: { type: 'string', minLength: 1 },
// age: { type: 'number', minimum: 0, maximum: 120 }
// }
// }License
ISC
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.