All Projects → ruzicka → to-json-schema

ruzicka / to-json-schema

Licence: MIT license
Converts JS objects to JSON Schema

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to to-json-schema

Cube2sphere
Python script to map 6 cube (cubemap, skybox) faces into an equirectangular (cylindrical projection, skysphere) map.
Stars: ✭ 120 (+44.58%)
Mutual labels:  converter, convert
qTsConverter
A simple tool to convert qt translation file (ts) to other format (xlsx / csv) and vice versa
Stars: ✭ 26 (-68.67%)
Mutual labels:  converter, convert
Gitconverter
Синхронизация хранилища конфигурации "1С:Предприятия" с репозиторием Git и последующим переходом на разработку в 1C:Enterprise Development Tools (1C:EDT) с сохранением истории
Stars: ✭ 149 (+79.52%)
Mutual labels:  converter, convert
Ssfconv
Sogou input method skin file (.ssf file) converter, supports conversion to fcitx or fcitx5 format.
Stars: ✭ 44 (-46.99%)
Mutual labels:  converter, convert
bafi
Universal JSON, BSON, YAML, CSV, XML converter with templates
Stars: ✭ 65 (-21.69%)
Mutual labels:  converter, convert
Youtube Channel Name Converter
A Youtube Channel Name to ID Converter
Stars: ✭ 75 (-9.64%)
Mutual labels:  converter, convert
arrest
Swagger REST framework for Node.js, with support for MongoDB and JSON-Schema
Stars: ✭ 17 (-79.52%)
Mutual labels:  schema, jsonschema
tex-equation-to-svg
Convert a TeX or LaTeX string to an SVG.
Stars: ✭ 34 (-59.04%)
Mutual labels:  converter, convert
json2codable
A command line tool to generate a Swift Codable struct from a JSON document
Stars: ✭ 19 (-77.11%)
Mutual labels:  converter, convert
typeconv
Convert between JSON Schema, TypeScript, GraphQL, Open API and SureType
Stars: ✭ 229 (+175.9%)
Mutual labels:  converter, jsonschema
Breakdance
It's time for your markup to get down! HTML to markdown converter. Breakdance is a highly pluggable, flexible and easy to use.
Stars: ✭ 418 (+403.61%)
Mutual labels:  converter, convert
schema2ldif
Schema 2 ldif : tool to convert .schema to .ldif files and mange them live into an openldap server
Stars: ✭ 14 (-83.13%)
Mutual labels:  converter, schema
Cashify
💸 Lightweight currency conversion library, successor of money.js
Stars: ✭ 329 (+296.39%)
Mutual labels:  converter, convert
Wx Voice
Convert audio files between Tencent apps (Weixin / Wechat, QQ) and Silk codec with other general formats such as MP3 and M4A
Stars: ✭ 93 (+12.05%)
Mutual labels:  converter, convert
Length.js
📏 JavaScript library for length units conversion.
Stars: ✭ 292 (+251.81%)
Mutual labels:  converter, convert
Gelatin
Transform text files to XML, JSON, or YAML
Stars: ✭ 150 (+80.72%)
Mutual labels:  converter, convert
svg2vector
Online batch converter of SVG images to Android vector drawable XML resource files
Stars: ✭ 39 (-53.01%)
Mutual labels:  converter, convert
xbytes
Parse bytes to human readable sizes (4747) → ('4.75 KB') and vice versa.
Stars: ✭ 17 (-79.52%)
Mutual labels:  converter, convert
ddal
DDAL(Distributed Data Access Layer) is a simple solution to access database shard.
Stars: ✭ 33 (-60.24%)
Mutual labels:  converter, schema
godmt
Tool that can parse Go files into an abstract syntax tree and translate it to several programming languages.
Stars: ✭ 42 (-49.4%)
Mutual labels:  converter, schema

npm version Build Status Coverage Status

to-json-schema

Converts javascript objects (and other types) to corresponding JSON schema

Install

npm install to-json-schema

Example usage

const toJsonSchema = require('to-json-schema');

const objToBeConverted = {
  name: 'David',
  rank: 7,
  born: '1990-04-05T15:09:56.704Z',
  luckyNumbers: [7, 77, 5]
};

const schema = toJsonSchema(objToBeConverted);

schema generated from above code will look like this:

{
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "rank": {
      "type": "integer"
    },
    "born": {
      "type": "string",
      "format": "date-time"
    },
    "luckyNumbers": {
      "type": "array",
      "items": {
        "type": "integer"
      }
    }
  }
}

toJsonSchema(value, options)

to-json-schema exports function that converts most javascript values to JSON schema. Such a schema can be used to further validation of similar objects/values

  • value: required Any javascript value
  • options: optional options object

Options

Common options

Possible option values

required (true|false default is false) specify true to make all properties required.

const schema = toJsonSchema(33, {required: false});
/*
{
  "type": "integer"
}
*/
const schema = toJsonSchema(33, {required: true});
/*
{
  "type": "integer",
  "required": true
}
*/

postProcessFnc (function)

parameters:

  • type (string) - JSON schema type of the value
  • schema (object) - Generated JSON schema
  • value - (any) - input value
  • defaultFunc (function) - standard function that is used to post-process generated schema. Takes the type, schema, value params.

By providing postProcessFnc, you can modify or replace generated schema. This function will be called recursively for all the properties and sub-properties and array items from leaves to the root. If you want to preserve default functionality, don't forget to call defaultFunc which is currently responsible for setting required for the schema items if there is common option required set tu true.

Following example is showing configuration options leading to all integer values to be automatically required

const options = {
  postProcessFnc: (type, schema, value, defaultFunc) =>
    (type === 'integer') ? {...schema, required: true} : defaultFunc(type, schema, value),
}

const instance = {
  a: 1,
  b: 'str',
}

const schema = toJsonSchema(instance, options);
/*
{
  type: 'object',
  properties: {
    a: {type: 'integer', required: true},
    b: {type: 'string'},
  },
}*/

Arrays options

arrays.mode (all|first|uniform|tuple default is all)

all option causes parser to go through all array items, finding the most compatible yet most descriptive schema possible.

Array items are all of compatible type:

const arr = [33, 44, 55];
const schema = toJsonSchema(arr, {arrays: {mode: 'all'}});
/*
{
  "type": "array",
  "items": {
    "type": "integer"
  }
}
*/

Items' types are incompatible. Type is omitted in schema to be able to validate input object:

const arr = [33, 'str', 55];
const schema = toJsonSchema(arr, {arrays: {mode: 'all'}});
/* 
{
  "type": "array"
}
*/

Incompatible in sub-item. Schema still describes object properties

const arr = [
  {name: 'john', grades: [1, 2, 3]},
  {name: 'david', grades: ['a', 'b', 'c']}
];
const schema = toJsonSchema(arr, {arrays: {mode: 'all'}});
/*
{
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "name": {
        "type": "string"
      },
      "grades": {
        "type": "array" // due to incompatible array items' types, `items` field is omitted
      }
    }
  }
}
*/

first option takes only first item in the array into account. If performance is a concern, you may consider this option.

const arr = ['str', 11, 30];
const schema = toJsonSchema(arr, {arrays: {mode: 'first'}});
/* Other than first array item is ignored
{
  "type": "array",
  "items": {
    "type": "string"
  }
}
*/

uniform option requires all items in array to have same structure (to convert to the same schema). If not, error is thrown.

const arr = ['str', 11, 30];
const schema = toJsonSchema(arr, {arrays: {mode: 'uniform'}});
/*
 Above code will throw 'Error: Invalid schema, incompatible array items'
*/

tuple option generates a tuple array (array of objects) from arrays.

const arr = ['str', 11, 30];
const schema = toJsonSchema(arr, {arrays: {mode: 'tuple'}});
/*
{
  "type": "array",
  "items": [
    {
      "type": "string"
    },
    {
      "type": "integer"
    },
    {
      "type": "integer"
    }
  ]
}
*/

Objects options

objects.additionalProperties (boolean, default true)

if set to false, all object schemas will include JSON schema property additionalProperties: false which makes generated schema to perevent any extra properties.

const options = {
  objects: {additionalProperties: false},
}
const obj = {
  a: {
    c: 1,
    d: 1,
  },
  b: 'str',
}
const schema = toJsonSchema(obj, options);
/*
{
  type: 'object',
  properties: {
    a: {
      type: 'object',
      properties: {
        c: {type: 'integer'},
        d: {type: 'integer'},
      },
      additionalProperties: false,
    },
    b: {type: 'string'},
  },
  additionalProperties: false,
}
*/

objects.preProcessFnc (function)

parameters:

  • obj - (object) - input object value that is supposed to be converted into JSON schema
  • defaultFunc (function) - standard function that is used to generate schema from object. Takes just the obj param.

By providing custom function you will be able to modify any object value (including nested ones) and pre-process it before it gets converted into schema or modify generated schema or do the schema conversion entirely by yourself.

Custom function from example bellow ignores all properties other than a and b from input object:

const options = {
  objects: {
    preProcessFnc: (obj, defaultFnc) => defaultFnc({a: obj.a, b: obj.b})
  }
};
const obj = {a: 1, b: 2, c: 3};
const schema = toJsonSchema(obj, options);
/*
{
  "type": "object",
  "properties": {
    "a": {
      "type": "integer"
    },
    "b": {
      "type": "integer",
    }
  }
}
*/

objects.postProcessFnc (function)

parameters:

  • schema (object) - Generated JSON schema
  • obj - (object) - input value
  • defaultFunc (function) - standard function that is used to post-process generated schema. Takes the schema, obj params.

By providing postProcessFnc, you can modify or replace generated schema. This function will be called recursively for all the properties and sub-properties and array items from leaves to the root of the obj object.

Custom objects.postProcessFnc makes properties required on parent type level:

const options = {
  objects: {
    postProcessFnc: (schema, obj, defaultFnc) => ({...defaultFnc(schema, obj), required: Object.getOwnPropertyNames(obj)})
  }
};
const obj = {a: 1, b: 'str'};
const schema = toJsonSchema(obj, options);
/*
{
  type: 'object',
  properties: {
    a: {type: 'integer'},
    b: {type: 'string'},
  }
  required: ['a', 'b']
}
*/

strings options

strings.preProcessFnc (function)

By providing custom function you will be able to modify any string value (including nested ones) and pre-process it before it gets converted to schema, modify generated schema or do the schema conversion entirely by yourself.

Provided function will receive two parameters:

  • string to be converted into JSON schema
  • default function that normally generates the schema. This function receives only string to be converted to JSON schema

Custom function from example bellow converts any string of object containing string to JSON schema and if string's content is 'date' than sets the format property to 'date':

const options = {
  strings: {
    preProcessFnc: (value, defaultFnc) => {
      const schema = defaultFnc(value);
      if (value === 'date') {
        schema.format = 'date';
      }
      return schema;
    },
  },
}
const schema = toJsonSchema('date', options);
/*
{
  "type": "string",
  "format": "date"
}
*/

strings.detectFormat (true|false default id true)

When set to true format of the strings values may be detected based on it's content.

These JSON schema string formats can be detected:

  • date-time
  • date
  • time
  • utc-millisec
  • color
  • style
  • phone
  • uri
  • email
  • ip-address
  • ipv6
const obj = {
  a: '2012-07-08T16:41:41.532Z',
  b: '+31 42 123 4567',
  c: 'http://www.google.com/',
  d: '[email protected]'
};
const schema = toJsonSchema(obj, {strings: {detectFormat: true}});
/*
{
  "type": "object",
  "properties": {
    "a": {
      "type": "string",
      "format": "date-time"
    },
    "b": {
      "type": "string",
      "format": "phone"
    },
    "c": {
      "type": "string",
      "format": "uri"
    },
    "d": {
      "type": "string",
      "format": "email"
    }
  }
}
*/
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].