6.0.6 • Published 2 years ago

skhema v6.0.6

Weekly downloads
3,889
License
Apache-2.0
Repository
github
Last release
2 years ago

skhema

JSON Schema utility collection

Current Release License Downloads Dependency status

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.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

ParamTypeDescription
subjectSchemaObjectschema
restrictingSchemaObjectschema

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

ParamTypeDescription
schemaObjectJSON schema
objectObjectobject

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

ParamTypeDefaultDescription
schemaObjectJSON schema
objectObjectobject
optionsObjectoptions
options.schemaOnlyBooleanfalseOnly 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

ParamTypeDefaultDescription
schemaObjectJSON schema
objectObjectobject
optionsObjectoptions
options.schemaOnlyBooleanfalseOnly 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

ParamTypeDefaultDescription
schemaObjectJSON schema
objectObjectobject
optionsObjectoptions
options.schemaOnlyBooleanfalseOnly 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

ParamTypeDescription
schemasArray.<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

ParamTypeDescription
schemaObjectschema

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

ParamTypeDefaultDescription
schemaObjectschema
objectObjectobject
optionsObjectoptions
options.schemaOnlyBooleanfalseOnly 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!

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.

6.0.6

2 years ago

6.0.5

2 years ago

6.0.4

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

6.0.3

2 years ago

6.0.2

2 years ago

5.3.4

3 years ago

5.3.3

4 years ago

5.3.2

4 years ago

5.3.1

4 years ago

5.3.0

4 years ago

5.2.10

4 years ago

5.2.9

4 years ago

5.2.8

4 years ago

5.2.7

4 years ago

5.2.6

4 years ago

5.2.5

5 years ago

5.2.4

5 years ago

5.2.3

5 years ago

5.2.2

5 years ago

5.2.1

5 years ago

5.2.0

5 years ago

5.1.1

5 years ago

5.1.0

5 years ago

5.0.0

5 years ago

4.0.4

5 years ago

4.0.3

5 years ago

4.0.2

5 years ago

4.0.1

5 years ago

4.0.0

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.5.2

5 years ago

2.5.1

5 years ago

2.5.0

6 years ago

2.4.1

6 years ago

2.4.0

6 years ago

2.3.0

6 years ago

2.2.0

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago