# skhema

> JSON Schema utility collection

Latest version **6.0.6** (published 2022-03-04) · Apache-2.0 license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install skhema
pnpm add skhema
yarn add skhema
bun add skhema
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 6.0.6 |
| Published | 2022-03-04 |
| First published | 2018-06-25 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=12.0.0 |
| Dependencies | 10 |
| Unpacked size | 164.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Balena Inc. |
| Maintainers | balena.io |

## Links

- npm: https://www.npmjs.com/package/skhema
- Repository: https://github.com/balena-io-modules/skhema
- Issues: https://github.com/balena-io-modules/skhema/issues
- npm.io page: https://npm.io/package/skhema

## Dependencies (10)

- [ajv](https://npm.io/package/ajv.md) ^6.5.1
- [lodash](https://npm.io/package/lodash.md) ^4.17.19
- [deep-copy](https://npm.io/package/deep-copy.md) ^1.4.2
- [lru-cache](https://npm.io/package/lru-cache.md) ^6.0.0
- [typed-error](https://npm.io/package/typed-error.md) ^3.2.0
- [ajv-keywords](https://npm.io/package/ajv-keywords.md) ^3.2.0
- [fast-memoize](https://npm.io/package/fast-memoize.md) ^2.5.1
- [json-schema-faker](https://npm.io/package/json-schema-faker.md) ^0.5.0-rc16
- [@types/json-schema](https://npm.io/package/@types/json-schema.md) ^6.0.1
- [json-schema-merge-allof](https://npm.io/package/json-schema-merge-allof.md) ^0.6.0

## Recent versions

- 6.0.6 (latest) — 2022-03-04
- 6.0.6-joshbwlng-dedup-required-bc5cede31b0c6d5abc4f8e14d6c5961ce84f12aa (joshbwlng-dedup-required-bc5cede31b0c6d5abc4f8e14d6c5961ce84f12aa) — 2022-03-04
- 6.0.5-joshbwlng-add-error-type-a7dc337166a74ad9570e30d2d10c91e00661d366 (joshbwlng-add-error-type-a7dc337166a74ad9570e30d2d10c91e00661d366) — 2022-03-04
- 6.0.5-joshbwlng-dedup-required-d7e5c771fa5e10da17933e25cd1da60aa32ce77e (joshbwlng-dedup-required-d7e5c771fa5e10da17933e25cd1da60aa32ce77e) — 2022-03-04
- 6.0.4-joshbwlng-fix-tests-1092785c9d7fdd614c3e2a5b08e5972b747858d2 (joshbwlng-fix-tests-1092785c9d7fdd614c3e2a5b08e5972b747858d2) — 2022-03-04
- 6.0.4-joshbwlng-dedup-required-87d251c60204999fff8003ec64eacfe6628a8e13 (joshbwlng-dedup-required-87d251c60204999fff8003ec64eacfe6628a8e13) — 2022-03-03
- 6.0.4-joshbwlng-dedup-required-8c2581b6c7ff5b6db1f70f9954d3426ff24a8276 (joshbwlng-dedup-required-8c2581b6c7ff5b6db1f70f9954d3426ff24a8276) — 2022-03-03
- 6.0.4-joshbwlng-dedup-required-a24254a88ee7a1b7d9733c7fee0276edbacbb9fa (joshbwlng-dedup-required-a24254a88ee7a1b7d9733c7fee0276edbacbb9fa) — 2022-03-03
- 6.0.4-joshbwlng-dedup-required-bdc05fb0f81f4ed4764b90d28b627bec3fcb6c7f (joshbwlng-dedup-required-bdc05fb0f81f4ed4764b90d28b627bec3fcb6c7f) — 2022-03-03
- 6.0.2-dependabot-npm-and-yarn-lodash-merge-4-6-2-d46e13ca4f67e207f952ab392f64f18ececcc068 (dependabot-npm-and-yarn-lodash-merge-4-6-2-d46e13ca4f67e207f952ab392f64f18ececcc068) — 2022-01-14
- 6.0.1-lucianbuzzo-dependabot-updates-3e2a02de41667caedbb34718edd0d5862eb6f50b (lucianbuzzo-dependabot-updates-3e2a02de41667caedbb34718edd0d5862eb6f50b) — 2022-01-14
- 6.0.0-lucianbuzzo-upgrade-lru-cache-1c0a31691eec10579eaedf8b555804487b99fd68 (lucianbuzzo-upgrade-lru-cache-1c0a31691eec10579eaedf8b555804487b99fd68) — 2022-01-13
- 5.3.5-lucianbuzzo-upgrade-lru-cache-6a091ae1f383e35f4b111e165d782ca6f55a11db (lucianbuzzo-upgrade-lru-cache-6a091ae1f383e35f4b111e165d782ca6f55a11db) — 2022-01-13
- 5.3.5-enable-data-ab307790085c19361036e27054e4301724290680 (enable-data-ab307790085c19361036e27054e4301724290680) — 2021-08-27
- 5.3.5-fix-assignment-c0e65906c17f134741b9490f40e8d1dd98f430a4 (fix-assignment-c0e65906c17f134741b9490f40e8d1dd98f430a4) — 2021-05-03
- … 122 more at https://npm.io/package/skhema/versions

## README

skhema
======

> JSON Schema utility collection

[![Current Release](https://img.shields.io/npm/v/skhema.svg?style=flat-square)](https://npmjs.com/package/skhema)
[![License](https://img.shields.io/npm/l/skhema.svg?style=flat-square)](https://npmjs.com/package/skhema)
[![Downloads](https://img.shields.io/npm/dm/skhema.svg?style=flat-square)](https://npmjs.com/package/skhema)
[![Dependency status](https://img.shields.io/david/resin-io-modules/skhema.svg?style=flat-square)](https://david-dm.org/resin-io-modules/skhema)

Installation
------------

Install `skhema` by running:

```sh
$ npm install --save skhema
```

Documentation
-------------


* [skhema](#module_skhema)
    * [.SchemaMismatch](#module_skhema.SchemaMismatch) : <code>Error</code>
    * [.IncompatibleSchemas](#module_skhema.IncompatibleSchemas) : <code>Error</code>
    * [.restrictSchema(subjectSchema, restrictingSchema)](#module_skhema.restrictSchema) ⇒ <code>Object</code>
    * [.scoreMatch(schema, object)](#module_skhema.scoreMatch) ⇒ <code>Number</code>
    * [.match(schema, object, [options])](#module_skhema.match) ⇒ <code>Object</code>
    * [.isValid(schema, object, [options])](#module_skhema.isValid) ⇒ <code>Boolean</code>
    * [.validate(schema, object, [options])](#module_skhema.validate)
    * [.merge(schemas)](#module_skhema.merge) ⇒ <code>Object</code>
    * [.normaliseRequires(schema)](#module_skhema.normaliseRequires) ⇒ <code>Object</code>
    * [.filter(schema, object, [options])](#module_skhema.filter) ⇒ <code>Object</code> \| <code>Null</code>

<a name="module_skhema.SchemaMismatch"></a>

### skhema.SchemaMismatch : <code>Error</code>
**Kind**: static property of [<code>skhema</code>](#module_skhema)  
**Summary**: Schema mismatch error  
**Access**: public  
<a name="module_skhema.IncompatibleSchemas"></a>

### skhema.IncompatibleSchemas : <code>Error</code>
**Kind**: static property of [<code>skhema</code>](#module_skhema)  
**Summary**: Incompatible schemas error  
**Access**: public  
<a name="module_skhema.restrictSchema"></a>

### skhema.restrictSchema(subjectSchema, restrictingSchema) ⇒ <code>Object</code>
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 [<code>skhema</code>](#module_skhema)  
**Summary**: Restrict a schema using another schema  
**Returns**: <code>Object</code> - restricted schema  
**Access**: public  

| Param | Type | Description |
| --- | --- | --- |
| subjectSchema | <code>Object</code> | schema |
| restrictingSchema | <code>Object</code> | schema |

**Example**  
```js
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' ]
> }
```
<a name="module_skhema.scoreMatch"></a>

### skhema.scoreMatch(schema, object) ⇒ <code>Number</code>
Score a matching object and schema based on specificity. Only
works with values that are valid against the provided schema

**Kind**: static method of [<code>skhema</code>](#module_skhema)  
**Summary**: Score a schema match by specificity  
**Returns**: <code>Number</code> - score  
**Access**: public  

| Param | Type | Description |
| --- | --- | --- |
| schema | <code>Object</code> | JSON schema |
| object | <code>Object</code> | object |

**Example**  
```js
const score = skhema.scoreMatch({
	 type: 'object'
}, {
	 foo: 'bar'
})

console.log(result) // -> 1
```
<a name="module_skhema.match"></a>

### skhema.match(schema, object, [options]) ⇒ <code>Object</code>
**Kind**: static method of [<code>skhema</code>](#module_skhema)  
**Summary**: Match an object against a schema  
**Returns**: <code>Object</code> - results  
**Access**: public  

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| schema | <code>Object</code> |  | JSON schema |
| object | <code>Object</code> |  | object |
| [options] | <code>Object</code> |  | options |
| [options.schemaOnly] | <code>Boolean</code> | <code>false</code> | Only validate the schema |

**Example**  
```js
const results = skhema.match({
	 type: 'object'
}, {
	 foo: 'bar'
})

if (!results.valid) {
	 for (const error of results.errors) {
		 console.error(error)
	 }
}
```
<a name="module_skhema.isValid"></a>

### skhema.isValid(schema, object, [options]) ⇒ <code>Boolean</code>
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 [<code>skhema</code>](#module_skhema)  
**Summary**: Check if an object matches a schema  
**Returns**: <code>Boolean</code> - whether the object matches the schema  
**Access**: public  

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| schema | <code>Object</code> |  | JSON schema |
| object | <code>Object</code> |  | object |
| [options] | <code>Object</code> |  | options |
| [options.schemaOnly] | <code>Boolean</code> | <code>false</code> | Only validate the schema |

**Example**  
```js
const isValid = skhema.isValid({
	 type: 'object'
}, {
	 foo: 'bar'
})

if (isValid) {
	 console.log('The object is valid')
}
```
<a name="module_skhema.validate"></a>

### 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 [<code>skhema</code>](#module_skhema)  
**Summary**: Validate an object and schema and throw if invalid  
**Access**: public  

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| schema | <code>Object</code> |  | JSON schema |
| object | <code>Object</code> |  | object |
| [options] | <code>Object</code> |  | options |
| [options.schemaOnly] | <code>Boolean</code> | <code>false</code> | Only validate the schema |

**Example**  
```js
skhema.validate({
	 type: 'object'
}, {
	 foo: 'bar'
})
```
<a name="module_skhema.merge"></a>

### skhema.merge(schemas) ⇒ <code>Object</code>
**Kind**: static method of [<code>skhema</code>](#module_skhema)  
**Summary**: Merge two or more JSON Schemas  
**Returns**: <code>Object</code> - merged JSON Schema  
**Access**: public  

| Param | Type | Description |
| --- | --- | --- |
| schemas | <code>Array.&lt;Object&gt;</code> | a set of JSON Schemas |

**Example**  
```js
const result = skhema.merge([
	 {
		 type: 'string',
		 maxLength: 5,
		 minLength: 2
	 },
	 {
		 type: 'string',
		 maxLength: 3
	 }
])

console.log(result)
> {
>	 type: 'string',
>	 maxLength: 3,
>	 minLength: 2
> }
```
<a name="module_skhema.normaliseRequires"></a>

### skhema.normaliseRequires(schema) ⇒ <code>Object</code>
**Kind**: static method of [<code>skhema</code>](#module_skhema)  
**Summary**: Set fields on a schema which are required but do not appear in properties  
**Returns**: <code>Object</code> - mutated schema  
**Access**: public  

| Param | Type | Description |
| --- | --- | --- |
| schema | <code>Object</code> | schema |

**Example**  
```js
const schema = skhema.normaliseRequires({
	 type: 'object',
	 properties: {},
	 required: [ 'foo' ]
})

console.log(schema.properties)
> { foo: { additionalProperties: false } }
```
<a name="module_skhema.filter"></a>

### skhema.filter(schema, object, [options]) ⇒ <code>Object</code> \| <code>Null</code>
**Kind**: static method of [<code>skhema</code>](#module_skhema)  
**Summary**: Filter an object based on a schema  
**Returns**: <code>Object</code> \| <code>Null</code> - filtered object  
**Access**: public  

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| schema | <code>Object</code> |  | schema |
| object | <code>Object</code> |  | object |
| [options] | <code>Object</code> |  | options |
| [options.schemaOnly] | <code>Boolean</code> | <code>false</code> | Only validate the schema |

**Example**  
```js
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:

```sh
$ 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](https://github.com/resin-io-modules/skhema/issues)
- Source Code: [github.com/resin-io-modules/skhema](https://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:

```sh
npm run lint
```

Support
-------

If you're having any problem, please [raise an issue](https://github.com/resin-io-modules/skhema/issues/new) on GitHub.

License
-------

The project is licensed under the Apache 2.0 license.

---
_Source: https://npm.io/package/skhema · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
