# json-schema-applier

> Apply a given JSON Schema to a given object creating a new instance that conforms the schema used

Latest version **0.3.5** (published 2020-11-23) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install json-schema-applier
pnpm add json-schema-applier
yarn add json-schema-applier
bun add json-schema-applier
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.5 |
| Published | 2020-11-23 |
| First published | 2020-11-20 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.0.0 |
| Dependencies | 0 |
| Unpacked size | 20.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Marco Silva |
| Maintainers | marcosilva |
| Keywords | json, schema, apply |

## Links

- npm: https://www.npmjs.com/package/json-schema-applier
- Repository: https://github.com/MarcoASilva/json-schema-applier
- Homepage: https://github.com/MarcoASilva/json-schema-applier#readme
- Issues: https://github.com/MarcoASilva/json-schema-applier/issues
- npm.io page: https://npm.io/package/json-schema-applier

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.3.5 (latest) — 2020-11-23
- 0.3.4 — 2020-11-23
- 0.3.3 — 2020-11-22
- 0.3.2 — 2020-11-22
- 0.3.1 — 2020-11-22
- 0.3.0 — 2020-11-22
- 0.2.17 — 2020-11-20

## README

# json-schema-applier

json-schema-applier is an NPM Package for applying (enforcing) objects to be schema compliant.

## Installation

Use the package manager [npm](https://www.npmjs.com/) to install json-schema-applier.

```bash
npm i json-schema-applier
```


## Description

json-schema-applier is a package for applying a json schema to a given object instance and it does 2 things: 

* __CREATE FIELDS__: For fields that are missing in the object instance:

  * Creates required fields
  * Creates fields that have a default value
    * You can override the default property in the field schema with a function that receives the instance object as parameter and should return the default value for that field

* __CAST FIELDS__: For fields that exist in the object instance:
  * Casts value accordingly
    * You can overwrite global map config object to cast any instance field value to anything that you want based on the field type.
    * You can create custom overwrites for each field independently of their types

__All according to the json schema passed as parameter__

## Usage 

### Create Fields

Create default and required fields

#### Create defaults

json-schema-applier will create or populate fields that have a default value on its schema

```javascript
const apply = require('json-schema-applier');

const peopleSchema = {
  $schema: "http://json-schema.org/draft-07/schema",
  $id: "http://example.com/example.json",
  type: "object",
  title: "Person schema",
  description: "The root schema comprises the entire JSON document.",
  default: {},
  examples: [
    {
      name: "Rose Mary",
      age: 30
    }
  ],
  required: [],
  properties: {
    name: {
      $id: "#/properties/name",
      type: "string",
      default: "Guest",
      examples: ["John Doe"]
    },
    age: {
      $id: "#/properties/age",
      type: "number",
      examples: [25],
      default: 25
    },
  }
};

const person = {}

validPerson = jsonSchemaApplier(person, peopleSchema);

console.log(validPerson);
// OUTPUTS:
{ name: 'Guest', age: 25}
```

or overriding the schema.properties[prop].default value with a function:

```javascript
const peopleSchema = {
  $schema: "http://json-schema.org/draft-07/schema",
  $id: "http://example.com/example.json",
  type: "object",
  title: "Person schema",
  description: "The root schema comprises the entire JSON document.",
  default: {},
  examples: [
    {
      name: "Rose Mary",
			age: 30
    }
  ],
  required: [],
  properties: {
    name: {
      $id: "#/properties/name",
      type: "string",
      default: "Guest",
      examples: ["John Doe"]
    },
    dateOfBirth: {
      $id: "#/properties/deteOfBirth",
      type: "string",
      default: "1900-01-01",
      examples: ["1900-01-01"]
    },
    age: {
      $id: "#/properties/age",
      type: "integer",
      examples: [25],
      default: 25
    },
  }
};

const person = {};

peopleSchema.properties.age.default = (person) => ((+new Date() - +new Date(person.dateOfBirth)) /1000/60/60/24/365); 

validPerson = jsonSchemaApplier(person, peopleSchema);

console.log(validPerson);
// OUTPUTS:
{ name: 'Guest', dateOfBirth: '1900-01-01', age: 120 }
```

#### Create required

json-schema-applier will fill required fields that are missing from the object based, first, on the overwrite object and fallback to global config object (overwritable too). So overwrites > global as global is the default "from/to" mapping object.

GLOBAL config (default mapping):

```javascript
const global = {
  NaN: {
    integer: 0,
    number: 0
  },
  undefined: {
    string: '',
    boolean: true,
    integer: 123,
    number: 0
  }
}
```
Fill required fields example:

```javascript
const jsonSchemaApplier = require("./lib");

const peopleSchema = {
  $schema: "http://json-schema.org/draft-07/schema",
  $id: "http://example.com/example.json",
  type: "object",
  title: "Person schema",
  description: "The root schema comprises the entire JSON document.",
  default: {},
  examples: [
    {
      name: "Rose Mary",
      age: 30
    }
  ],
  required: ["id"],
  properties: {
    id: {
      $id: "#/properties/id",
      type: "integer",
      title: "The id schema",
      description: "An explanation about the purpose of this instance.",
      examples: [123]
    },    
    name: {
      $id: "#/properties/name",
      type: "string",
      default: "Guest",
      examples: ["John Doe"]
    },
    age: {
      $id: "#/properties/age",
      type: "number",
      examples: [25],
      default: 25
    },
  }
};

const person = {}

validPerson = jsonSchemaApplier(person, peopleSchema);

console.log(validPerson);
// OUTPUTS:
{ name: 'Guest', age: 25, id: 0 }
```

### Cast Fields

```javascript
const jsonSchemaApplier = require("../lib");

const peopleSchema = {
  $schema: "http://json-schema.org/draft-07/schema",
  $id: "http://example.com/example.json",
  type: "object",
  title: "Person schema",
  description: "The root schema comprises the entire JSON document.",
  default: {},
  examples: [
    {
      name: "Rose Mary",
			age: 30
    }
  ],
  required: [],
  properties: {
    id: {
      $id: "#/properties/id",
      type: "integer",
      title: "The id schema",
      description: "An explanation about the purpose of this instance.",
      examples: [123]
    },    
    name: {
      $id: "#/properties/name",
      type: "string",
      default: "Guest",
      examples: ["John Doe"]
    },
    age: {
      $id: "#/properties/age",
      type: "integer",
      examples: [25],
      default: "25"
    },
    accountBalance: {
      $id: "#/properties/age",
      type: "number",
      examples: [1, 9.01],
      default: 0
    }
  }
};

const person = {id: "1", name: "John Doe", age: 30, accountBalance: "2536.72"}

validPerson = jsonSchemaApplier(person, peopleSchema);

console.log(validPerson);
// OUTPUTS:
{ id: 1, name: 'John Doe', age: 30, accountBalance: 2536.72 }
```

## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

## License
[APACHE-2.0](https://www.apache.org/licenses/LICENSE-2.0)

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