npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

skhema

v6.0.6

Published

JSON Schema utility collection

Downloads

20,583

Readme

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

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

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.