Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

skhema

Package Overview
Dependencies
Maintainers
1
Versions
137
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

skhema

JSON Schema utility collection

  • 6.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4.3K
decreased by-0.56%
Maintainers
1
Weekly downloads
 
Created
Source

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
[options]Objectoptions
[options.schemaOnly]BooleanfalseOnly 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
[options]Objectoptions
[options.schemaOnly]BooleanfalseOnly 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
[options]Objectoptions
[options.schemaOnly]BooleanfalseOnly 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
[options]Objectoptions
[options.schemaOnly]BooleanfalseOnly 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.

FAQs

Package last updated on 04 Mar 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc