Git Product home page Git Product logo

json-schema-fix's Introduction

JSON Schema Fix

JSON creation, validation, fix and documentation according schema.

If only validation is important to you, we suggest ajv.

Table of Contents

Installation

npm install --save json-schema-fix

Schema Definition

The schema has to be defined according https://json-schema.org/.

Accepted/used properties are type, properties, default and description.

Usage

Create JSON from schema

const json = require('json-schema-fix');

const schema = {
  type: 'object',
  properties: {
    name: {
      type: 'string',
      default: 'Name',
    },
    age: {
      type: 'number',
      default: '123',
    },
  },
};

const data = json.create(schema);

data:

{
  name: 'Name',
  age: 123
}

Validate JSON according schema

const json = require('json-schema-fix');

const schema = {
  type: 'object',
  properties: {
    name: {
      type: 'string',
      default: 'Name',
    },
    age: {
      type: 'number',
      default: '123',
    },
  },
};

const isValid = json.validate(schema, { name: 'Mr Nice Guy', age: 30 });
const isNotValid1 = json.validate(schema, 'any wrong data');
const isNotValid2 = json.validate(schema, { name: 'Mr Nice Guy', age: 'any wrong data' });

isValid:

null

isNotValid1:

[
  {
    error: 'Should be an "object".',
    path: 'root'
  }
]

isNotValid2:

[
  {
    error: 'Should be a "number".',
    path: 'root.age'
  }
]

Fix JSON according schema

const json = require('json-schema-fix');

const schema = {
  type: 'object',
  properties: {
    name: {
      type: 'string',
      default: 'Name',
    },
    age: {
      type: 'number',
      default: '123',
    },
  },
};

const fixedData1 = json.fix(schema, undefined);
const fixedData2 = json.fix(schema, { name: 'Mr Nice Guy' });
const fixedData3 = json.fix(schema, { unrelevant: undefined });

fixedData1:

{
  name: 'Name',
  age: '123'
}

fixedData2:

{
  name: 'Mr Nice Guy',
  age: '123'
}

fixedData3:

{
  name: 'Name',
  age: '123'
}

Create Markdown documentation from schema

const json = require('json-schema-fix');

const schema = {
  type: 'object',
  properties: {
    name: {
      type: 'string',
      default: 'Name',
      description: 'Name of the person.'
    },
    age: {
      type: 'number',
      default: '123',
      description: 'Age of the person.'
    },
  },
};

const markdownDocumentation = json.docu(schema);

markdownDocumentation:

| Property | Type | Default Value | Description |
|:--- |:--- |:--- |:--- |
| root | *object* | | |
| root.name | *string* | `Name` | Name of the person. |
| root.age | *number* | `123` | Age of the person. |

Rendered:

Property Type Default Value Description
root object
root.name string Name Name of the person.
root.age number 123 Age of the person.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.