What is json-schema-library?
The json-schema-library is a JavaScript library for working with JSON Schema. It provides tools for validating JSON data against a schema, resolving references, and managing schema dependencies.
What are json-schema-library's main functionalities?
Schema Validation
This feature allows you to validate JSON data against a defined schema. The code sample demonstrates how to validate an object with properties 'name' and 'age' against a schema that requires these properties to be of specific types.
const { validate } = require('json-schema-library');
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'integer' }
},
required: ['name', 'age']
};
const data = { name: 'John', age: 30 };
const result = validate(data, schema);
console.log(result.valid); // true
Reference Resolution
This feature resolves JSON Schema references within a schema. The code sample shows how to resolve a reference to a 'person' definition within a schema.
const { resolveRefs } = require('json-schema-library');
const schema = {
definitions: {
person: {
type: 'object',
properties: {
name: { type: 'string' }
}
}
},
$ref: '#/definitions/person'
};
const resolvedSchema = resolveRefs(schema);
console.log(resolvedSchema);
Schema Compilation
This feature compiles a JSON Schema into a more efficient format for validation. The code sample demonstrates compiling a schema that includes an 'email' property with a specific format.
const { compile } = require('json-schema-library');
const schema = {
type: 'object',
properties: {
email: { type: 'string', format: 'email' }
}
};
const compiledSchema = compile(schema);
console.log(compiledSchema);
Other packages similar to json-schema-library
ajv
Ajv is a popular JSON Schema validator that is known for its high performance. It supports JSON Schema draft-04, draft-06, draft-07, and draft-2019-09. Compared to json-schema-library, Ajv offers more extensive support for different JSON Schema drafts and additional features like asynchronous validation and custom keywords.
jsonschema
The jsonschema package is a simple and easy-to-use JSON Schema validator for JavaScript. It supports JSON Schema draft-04 and is suitable for basic validation needs. While it may not have as many features as json-schema-library, it is straightforward and effective for simple use cases.
jsen
Jsen is a fast JSON Schema validator that supports JSON Schema draft-04. It is designed for performance and simplicity, making it a good choice for applications where speed is critical. Compared to json-schema-library, Jsen focuses more on performance and less on features like reference resolution.
json-schema-library
Customizable and hackable json-validator and json-schema utilities for traversal, data generation and validation
This package offers tools and utilities to work with json-schema, create and validate data. Unfortunately, most
packages, editors or validators do not care to expose basic json-schema functionality. Instead of small memory
footprint or high performance, this package focuses on exposing utilities for browser and node environments and
lessens the pain to build custom tools around json-schema.
npm i json-schema-library
.
This package is tested on node v6.9.1.
Overview
Either select an existing validator (core
) or create your own. Each Core holds all functions that are required
for the json-schema operations like validation. In order to overwrite a custom function you can either
- modify the map-objects (i.e. /lib/validation/keywords)
- overwrite functions or keys in the generated instance (
const instance = new Core()
) - Create a custom Core following the examples in /lib/cores
Additionally, helper functions and tools are separately exposed via CommonJS Modules. Most of them do require a
core-object as parameter, but use only some of the functions defined in an core-instance.
Core
The default interface of a validator can be found in /lib/cores/CoreInterface. It exposes
the following methods
method | parameter | description |
---|
constructor | schema | pass the root schema in the constructor or add it on rootSchema |
each | schema, data, callback, [pointer] | Iterates over the data, passing value and its schema |
step | key, schema, data, [pointer] | step into a json-schema by the given key (property or index) |
validate | schema, data, [pointer] | return a list of errors found validating the data |
isValid | schema, data, [pointer] | returns true if the data is validated by the json-schema |
resolveOneOf | schema, data, [pointer] | returns the oneOf-schema for the passed data |
resolveRef | schema | resolves a $ref on a given schema-object |
getSchema | schema, data, [pointer] | returns the json schema of the given data-json-pointer |
getTemplate | schema, data | returns a template object based of the given json-schema |
Examples
getSchema(core, schema, pointer, data)
Returns the json 'schema' matching 'data' at 'pointer'. Should be modified to use a step/next-function, which is already
within the logic (advance VS retrieve from root -> support both)
const core = new require("json-schema-library").core.draft04(rootSchema),
const targetSchema = getSchema(core, rootSchema, '#/path/to/target', rootData);
Currently may also return an error:
if (targetSchema.type === "error") {
throw targetSchema;
}
getTemplate(core, schema, data, rootSchema = schema)
Returns data which is valid to the given json-schema. Additionally, a data object may be given, which will be
extended by any missing items or properties.
const Core = require("json-schema-library").cores.Draft04;
const core = new Core();
const baseData = core.getTemplate(
{ type: "object", properties: { target: { type: "string", default: "v" } } },
{ other: true }
);
validate(core, data, schema, step)
Validate data and get a list of validation errors
const Core = require("json-schema-library").cores.Draft04;
const core = new Core(rootSchema);
const errors = core.validate({ type: "number" }, "");
isValid(core, data, schema, step)
Return true if the given schema validates the data
const Core = require("json-schema-library").cores.Draft04;
const core = new Core(rootSchema);
const baseSchema = core.isValid({ type: "number" }, "");
step(core, key, schema, data, rootSchema = schema)
Get the child schema of a property or index
const Core = require("json-schema-library").cores.Draft04;
const core = new Core(rootSchema);
const baseSchema = core.step(
{ type: "object", properties: { target: {type: "string"}} },
{ target: "value" }
"target",
);
each(core, data, schema, callback)
Iterate over each item (object, array and value), passing each value and its corresponding schema
const Core = require("json-schema-library").cores.Draft04;
const core = new Core({
type: "array",
items: [
{ type: "number" },
{ type: "string" }
]
});
core.each(core.rootSchema, [5, "nine"], (schema, value, pointer) => {
});
Additional helpers
SchemaService(schema)
Retrieve the json-schema at the given json-pointer
const schemaService = new SchemaService(rootSchema);
const targetSchema = schemaService.get('#/path/to/target', rootData);
getChildSchemaSelection
Returns a list of possible schemas for the given child-property or index
const listOfAvailableOptions = getChildSchemaSelection(core, schema, "childKey");
createSchemaOf(data)
Creates a json-schema for the given input-data.
const baseSchema = getTemplate({ target: "" });