skhema
JSON Schema utility collection
![Dependency status](https://img.shields.io/david/resin-io-modules/skhema.svg?style=flat-square)
Installation
Install skhema
by running:
$ npm install --save skhema
Documentation
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.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.customFormats] | customFormats | {} | custom formats |
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.customFormats] | customFormats | {} | custom formats |
Example
const isValid = skhema.isValid({
type: 'object'
}, {
foo: 'bar'
})
if (isValid) {
console.log('The object is valid')
}
skhema.validate(schema, object, [options])
Kind: static method of skhema
Summary: Validate an object and throw if invalid
Access: public
Param | Type | Default | Description |
---|
schema | Object | | JSON schema |
object | Object | | object |
[options] | Object | | options |
[options.customFormats] | customFormats | {} | custom formats |
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.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.force] | Boolean | false | force filter |
[options.customFormats] | customFormats | {} | custom formats |
Example
const result = skhema.filter({
type: 'object',
properties: {
foo: {
type: 'number'
}
},
required: [ 'foo' ]
}, {
foo: 1,
bar: 2
})
console.log(result)
> {
> foo: 1
> }
skhema~formatValidationCallback : function
This callback is called when validating a custom format. It should return
true if the value is valid or false otherwise
Kind: inner typedef of skhema
Param | Type | Description |
---|
value | * | The value to validate |
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!
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.