skhema
v6.0.6
Published
JSON Schema utility collection
Downloads
20,583
Readme
skhema
JSON Schema utility collection
Installation
Install skhema
by running:
$ npm install --save skhema
Documentation
- skhema
- .SchemaMismatch : Error
- .IncompatibleSchemas : Error
- .restrictSchema(subjectSchema, restrictingSchema) ⇒ Object
- .scoreMatch(schema, object) ⇒ Number
- .match(schema, object, [options]) ⇒ Object
- .isValid(schema, object, [options]) ⇒ Boolean
- .validate(schema, object, [options])
- .merge(schemas) ⇒ Object
- .normaliseRequires(schema) ⇒ Object
- .filter(schema, object, [options]) ⇒ Object | Null
skhema.SchemaMismatch : Error
Kind: static property of skhema
Summary: Schema mismatch error
Access: public
skhema.IncompatibleSchemas : Error
Kind: static property of skhema
Summary: Incompatible schemas error
Access: public
skhema.restrictSchema(subjectSchema, restrictingSchema) ⇒ Object
Removes values from a subject schema so that a value that matches the resulting schema will also validate against the restricting schema.
Kind: static method of skhema
Summary: Restrict a schema using another schema
Returns: Object - restricted schema
Access: public
| Param | Type | Description | | --- | --- | --- | | subjectSchema | Object | schema | | restrictingSchema | Object | schema |
Example
const result = skhema.restrictSchema({
type: 'object',
properties: {
foo: {
type: 'number'
},
bar: {
type: 'string'
}
},
required: [ 'foo' ]
}, {
type: 'object',
properties: {
foo: {
type: 'number'
}
},
additionalProperties: false,
required: [ 'foo' ]
})
console.log(result)
> {
> type: 'object',
> properties: {
> foo: {
> type: 'number'
> },
> },
> additionalProperties: false,
> required: [ 'foo' ]
> }
skhema.scoreMatch(schema, object) ⇒ Number
Score a matching object and schema based on specificity. Only works with values that are valid against the provided schema
Kind: static method of skhema
Summary: Score a schema match by specificity
Returns: Number - score
Access: public
| Param | Type | Description | | --- | --- | --- | | schema | Object | JSON schema | | object | Object | object |
Example
const score = skhema.scoreMatch({
type: 'object'
}, {
foo: 'bar'
})
console.log(result) // -> 1
skhema.match(schema, object, [options]) ⇒ Object
Kind: static method of skhema
Summary: Match an object against a schema
Returns: Object - results
Access: public
| Param | Type | Default | Description | | --- | --- | --- | --- | | schema | Object | | JSON schema | | object | Object | | object | | [options] | Object | | options | | [options.schemaOnly] | Boolean | false | Only validate the schema |
Example
const results = skhema.match({
type: 'object'
}, {
foo: 'bar'
})
if (!results.valid) {
for (const error of results.errors) {
console.error(error)
}
}
skhema.isValid(schema, object, [options]) ⇒ Boolean
This is a shorthand function for .match()
which can be used
if the caller is not interested in the actual error messages.
Kind: static method of skhema
Summary: Check if an object matches a schema
Returns: Boolean - whether the object matches the schema
Access: public
| Param | Type | Default | Description | | --- | --- | --- | --- | | schema | Object | | JSON schema | | object | Object | | object | | [options] | Object | | options | | [options.schemaOnly] | Boolean | false | Only validate the schema |
Example
const isValid = skhema.isValid({
type: 'object'
}, {
foo: 'bar'
})
if (isValid) {
console.log('The object is valid')
}
skhema.validate(schema, object, [options])
The .validate()
method will throw if the provided schema isn't
valid or if the object doesn't validate against the schema. If you just want
to validate a schema, you use the schemaOnly
option.
Kind: static method of skhema
Summary: Validate an object and schema and throw if invalid
Access: public
| Param | Type | Default | Description | | --- | --- | --- | --- | | schema | Object | | JSON schema | | object | Object | | object | | [options] | Object | | options | | [options.schemaOnly] | Boolean | false | Only validate the schema |
Example
skhema.validate({
type: 'object'
}, {
foo: 'bar'
})
skhema.merge(schemas) ⇒ Object
Kind: static method of skhema
Summary: Merge two or more JSON Schemas
Returns: Object - merged JSON Schema
Access: public
| Param | Type | Description | | --- | --- | --- | | schemas | Array.<Object> | a set of JSON Schemas |
Example
const result = skhema.merge([
{
type: 'string',
maxLength: 5,
minLength: 2
},
{
type: 'string',
maxLength: 3
}
])
console.log(result)
> {
> type: 'string',
> maxLength: 3,
> minLength: 2
> }
skhema.normaliseRequires(schema) ⇒ Object
Kind: static method of skhema
Summary: Set fields on a schema which are required but do not appear in properties
Returns: Object - mutated schema
Access: public
| Param | Type | Description | | --- | --- | --- | | schema | Object | schema |
Example
const schema = skhema.normaliseRequires({
type: 'object',
properties: {},
required: [ 'foo' ]
})
console.log(schema.properties)
> { foo: { additionalProperties: false } }
skhema.filter(schema, object, [options]) ⇒ Object | Null
Kind: static method of skhema
Summary: Filter an object based on a schema
Returns: Object | Null - filtered object
Access: public
| Param | Type | Default | Description | | --- | --- | --- | --- | | schema | Object | | schema | | object | Object | | object | | [options] | Object | | options | | [options.schemaOnly] | Boolean | false | Only validate the schema |
Example
const result = skhema.filter({
type: 'object',
properties: {
foo: {
type: 'number'
}
},
required: [ 'foo' ]
}, {
foo: 1,
bar: 2
})
console.log(result)
> {
> foo: 1
> }
Tests
Run the test suite by doing:
$ npm test
Contribute
We're looking forward to support more operating systems. Please raise an issue or even better, send a PR to increase support!
- Issue Tracker: github.com/resin-io-modules/skhema/issues
- Source Code: github.com/resin-io-modules/skhema
Before submitting a PR, please make sure that you include tests, and that the linter runs without any warning:
npm run lint
Support
If you're having any problem, please raise an issue on GitHub.
License
The project is licensed under the Apache 2.0 license.