Skip to content

MarcoASilva/json-schema-applier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-schema-applier

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

Installation

Use the package manager npm to install json-schema-applier.

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

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:

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):

const global = {
  NaN: {
    integer: 0,
    number: 0
  },
  undefined: {
    string: '',
    boolean: true,
    integer: 123,
    number: 0
  }
}

Fill required fields example:

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

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

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published