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

json-schema-applier

v0.3.5

Published

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

Downloads

5

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