Skip to content

mobitel-ltd/mobitel-json-schema-template

Repository files navigation

Mobitel Ltd. JSON-Schema template

A small helper for generating a JSON schema elements.

What is JSON schema? Look here and here

Attention

This module writing and testing on NodeJs v.8+ and NPM v.5+. Using the module in previous versions of NodeJs does not guarantee its correct operation.

npm i --save mobitel-json-schema-template

Writing JSON-schema

const jst = require('mobitel-json-schema-template');

module.exports = {
    id: 'exampleSchema',
    type: 'object',
    additionalProperties: false,
    required: [
        'propArray',
        'propInteger',
        'propNumber',
        'propString',
        'propEnum',
        'propNull',
        'propBoolean',
        'propStringFormat',
        'propAnyOf',
        'propAllOf',
        'propOneOf',
        'propNot',
        'propRef',
    ],
    properties: {
        propArray: jst.array()
            .additional(false)
            .items(
                [
                    {type: 'object'},
                    jst.boolean(),
                ]
            ).done(),
        propInteger: jst.integer().min(10).max(100).eMax().done(),
        propNumber: jst.number().enum([1, 3, 5, 7, 9]).done(),
        propString: jst.string().pattern(/^\w+$/).done(),
        propEnum: jst.enum('viva', 'vita'),
        propNull: jst.null(),
        propBoolean: jst.boolean(false),
        propStringFormat: jst.stringFormat('hostname'),
        propAnyOf: jst.anyOf([
            jst.string().done(),
            jst.integer().done(),
        ]),
        propAllOf: jst.allOf([
            jst.string().done(),
            jst.string().max(10).done(),
        ]),
        propOneOf: jst.oneOf([
            jst.string().done(),
            jst.integer().done(),
        ]),
        propNot: jst.not(jst.null()),
        propRef: jst.ref('#/definitions/refExample'),
    },
    definitions: {
        refExample: {
            type: 'object',
            required: [
                'asString',
                'asNumber',
                'asNull',
            ],
            properties: {
                asString: jst.string().min(1).done(),
                asNumber: jst.number().min(1).done(),
                asNull: jst.null(),
            },
        },
    },
};

Result

{
  "id": "exampleSchema",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "propArray",
    "propInteger",
    "propNumber",
    "propString",
    "propEnum",
    "propNull",
    "propBoolean",
    "propStringFormat",
    "propAnyOf",
    "propAllOf",
    "propOneOf",
    "propNot",
    "propRef"
  ],
  "properties": {
    "propArray": {
      "type": "array",
      "additionalItems": false,
      "items": [
        {"type": "object"},
        {"type": "boolean"}
      ]
    },
    "propInteger": {
      "type":"integer",
      "minimum": 10,
      "maximum": 100,
      "exclusiveMaximum": true
    },
    "propNumber": {
      "type": "number",
      "enum": [1, 3, 5, 7, 9]
    },
    "propString": {
      "type": "string",
      "pattern": "/^\\w+$/"
    },
    "propEnum": {
      "enum": ["viva", "vita"]
    },
    "propNull": {
      "type": "null"
    },
    "propBoolean": {
      "type": "boolean",
      "enum": [false]
    },
    "propStringFormat": {
      "type": "string",
      "format": "hostname"
    },
    "propAnyOf": {
      "anyOf": [
        {"type": "string"},
        {"type": "integer"}
      ]
    },
    "propAllOf": {
      "allOf": [
        {"type": "string"},
        {
          "type": "string",
          "maxLength": 10
        }
      ]
    },
    "propOneOf": {
      "oneOf": [
        {"type": "string"},
        {"type": "integer"}
      ]
    },
    "propNot": {
      "not": {"type": "null"}
    },
    "propRef": {
    "$ref": "#/definitions/refExample"
    }
  },
  "definitions":{
    "refExample": {
      "type": "object",
      "required": [
        "asString",
        "asNumber",
        "asNull"
      ],
      "properties": {
        "asString": {
          "type": "string",
          "minLength": 1
        },
        "asNumber": {
          "type": "number",
          "minimum": 1
        },
        "asNull": {"type": "null"}
      }
    }
  }
}

up to navigation

const jst = require('mobitel-json-schema-template');

Returns object for generating a JSON schema elements.

up to navigation

Arguments - Object[]|Object Can accept mix of Object[] and Object

Example

jst.allOf(
    [
        { type: 'string' },
        { maxLength: 5 }
    ]
);

Result

{
  "allOf": [
    { "type": "string" },
    { "maxLength": 5 }
  ]
}

up to navigation

Arguments - Object[]|Object Can accept mix of Object[] and Object

Example

jst.anyOf(
    [
        {type: 'string'},
        jst.number().done()
    ]
);

Result

{
  "anyOf": [
    { "type": "string" },
    { "type": "number" }
  ]
}

up to navigation

Arguments - Boolean or 'all' (default)

Example Boolean

jst.boolean(true);

Result Boolean

{
  "type": "boolean",
  "enum": [true]
}

Example 'all'

jst.boolean();

Result 'all'

{
  "type": "boolean"
}

up to navigation

Arguments - Array|* Can accept mix of Array and *

Example

jst.enum(['one', 'two', 'three']);

Result

{
  "enum": [
    "one",
    "two",
    "three"
  ]
}

up to navigation

Arguments - Object

Example

jst.not({type: 'string'});

Result

{
  "not": {"type": "string"}
}

up to navigation

Arguments - no

Example

jst.null();

Result

{
  "type": "null"
}

up to navigation

Arguments - Object[]|Object Can accept mix of Object[] and Object

Example

jst.oneOf(
    [
        { type: 'number', multipleOf: 5 },
        jst.number().multipleOf(3).done()
    ]
);

Result

{
  "oneOf": [
    { "type": "number", "multipleOf": 5 },
    { "type": "number", "multipleOf": 3 }
  ]
}

up to navigation

Arguments - String

Example

jst.ref('#/definitions/subschema');

Result

{
  "$ref": "#/definitions/address"
}

up to navigation

Arguments - String Argument must be values like:

  • date-time
  • email
  • hostname
  • ipv4
  • ipv6
  • uri

Example

jst.stringFormat('hostname');

Result

{
  "type": "string",
  "format": "hostname"
}

up to navigation

Arguments - no

Example

jst.array().done();

Result

{
  "type": "array"
}

up to navigation

Arguments - Boolean

Example

jst.array().additional(true).done();

Result

{
  "type": "array",
  "additionalItems": true
}

up to navigation

Arguments - Object[]|Object Can accept mix of Object[] and Object

Example

jst.array().items(
    [
        {type: 'string'},
        jst.number().done()
    ]
).done();

Result

{
  "type": "array",
  "items": [
    {"type": "string"},
    {"type": "number"}
  ]
}

up to navigation

Arguments - positive Number

Example

jst.array().max(10).done();

Result

{
  "type": "array",
  "maxItems": 10
}

up to navigation

Arguments - positive Number

Example

jst.array().min(1).done();

Result

{
  "type": "array",
  "minItems": 1
}

up to navigation

Arguments - no

Example

jst.array().unique().done();

Result

{
  "type": "array",
  "uniqueItems": true
}

up to navigation

Arguments - no Finalize creation JSON schema template by type and return complete object.

Example

jst.array().max(10).done();

Result

{
  "type": "array",
  "maxItems": 10
}

up to navigation

Arguments - no

Example

jst.integer().done();

Result

{
  "type": "integer"
}

up to navigation

Arguments - Object[]|Object Can accept mix of Object[] and Object

Example

jst.integer().allOf(
 [
     { type: 'integer' },
     { maximum: 5 }
 ]
).done();

Result

{
  "type": "integer",
  "allOf": [
    { "type": "integer" },
    { "maximum": 5 }
  ]
}

up to navigation

Arguments - Object[]|Object Can accept mix of Object[] and Object

Example

jst.integer().anyOf(
    [
        {type: 'integer', enum: [1, 5, 10]},
        jst.integer().min(10).done()
    ]
).done();

Result

{
  "anyOf": [
    {
      "type": "integer",
      "enum": [1, 5, 10]
    },
    {
      "type": "integer",
      "minimum": 10
    }
  ]
}

up to navigation

Arguments - no

Example

jst.integer().eMax().done();

Result

{
  "type": "integer",
  "exclusiveMaximum": true
}

up to navigation

Arguments - no

Example

jst.integer().eMin().done();

Result

{
  "type": "integer",
  "exclusiveMinimum": true
}

up to navigation

Arguments - Array|* Can accept mix of Array and *

Example

jst.integer().enum([1, 2, 3]).done();

Result

{
  "type": "integer",
  "enum": [1, 2, 3]
}

up to navigation

Arguments - Number as integer

Example

jst.integer().max(10).done();

Result

{
  "type": "integer",
  "maximum": 10
}

up to navigation

Arguments - Number as integer

Example

jst.integer().min(1).done();

Result

{
  "type": "integer",
  "minimum": 1
}

up to navigation

Arguments - positive Number as integer

Example

jst.integer().multipleOf(10).done();

Result

{
  "type": "integer",
  "multipleOf": 10
}

up to navigation

Arguments - Object

Example

jst.integer().not({enum: [1, 2, 3]}).done();

Result

{
  "type": "integer",
  "not": {
    "enum": [1, 2, 3]
  }
}

up to navigation

Arguments - Object[]|Object Can accept mix of Object[] and Object

Example

jst.integer().oneOf(
    [
        { type: 'integer', maximum: 5 },
        jst.integer().max(3).done()
    ]
).done();

Result

{
  "oneOf": [
    { "type": "integer", "maximum": 5 },
    { "type": "integer", "maximum": 3 }
  ]
}

up to navigation

Arguments - no Finalize creation JSON schema template by type and return complete object.

Example

jst.integer().max(10).done();

Result

{
  "type": "integer",
  "maximum": 10
}

up to navigation

Arguments - no

Example

jst.number().done();

Result

{
  "type": "number"
}

up to navigation

Arguments - Object[]|Object Can accept mix of Object[] and Object

Example

jst.number().allOf(
 [
     { type: 'number' },
     { maximum: 5 }
 ]
).done();

Result

{
  "type": "number",
  "allOf": [
    { "type": "number" },
    { "maximum": 5 }
  ]
}

up to navigation

Arguments - Object[]|Object Can accept mix of Object[] and Object

Example

jst.number().anyOf(
    [
        {type: 'number', enum: [1, 5, 10]},
        jst.number().min(10).done()
    ]
).done();

Result

{
  "anyOf": [
    {
      "type": "number",
      "enum": [1, 5, 10]
    },
    {
      "type": "number",
      "minimum": 10
    }
  ]
}

up to navigation

Arguments - no

Example

jst.number().eMax().done();

Result

{
  "type": "number",
  "exclusiveMaximum": true
}

up to navigation

Arguments - no

Example

jst.number().eMin().done();

Result

{
  "type": "number",
  "exclusiveMinimum": true
}

up to navigation

Arguments - Array|* Can accept mix of Array and *

Example

jst.number().enum([1.5, 2.5, 3.5]).done();

Result

{
  "type": "number",
  "enum": [1.5, 2.5, 3.5]
}

up to navigation

Arguments - Number

Example

jst.number().max(10.5).done();

Result

{
  "type": "number",
  "maximum": 10.5
}

up to navigation

Arguments - Number

Example

jst.number().min(1.5).done();

Result

{
  "type": "number",
  "minimum": 1.5
}

up to navigation

Arguments - positive Number as integer

Example

jst.number().multipleOf(10).done();

Result

{
  "type": "number",
  "multipleOf": 10
}

up to navigation

Arguments - Object

Example

jst.number().not({enum: [1.5, 2.5, 3.5]}).done();

Result

{
  "type": "number",
  "not": {
    "enum": [
      1.5,
      2.5,
      3.5
    ]
  }
}

up to navigation

Arguments - Object[]|Object Can accept mix of Object[] and Object

Example

jst.number().oneOf(
    [
        { type: 'number', maximum: 5 },
        jst.number().max(3).done()
    ]
).done();

Result

{
  "oneOf": [
    { "type": "number", "maximum": 5 },
    { "type": "number", "maximum": 3 }
  ]
}

up to navigation

Arguments - no Finalize creation JSON schema template by type and return complete object.

Example

jst.number().max(10).done();

Result

{
  "type": "number",
  "maximum": 10
}

up to navigation

Arguments - no

Example

jst.string().done();

Result

{
  "type": "string"
}

up to navigation

Arguments - Object[]|Object Can accept mix of Object[] and Object

Example

jst.string().allOf(
 [
     { type: 'string' },
     { maxLength: 5 }
 ]
).done();

Result

{
  "type": "string",
  "allOf": [
    { "type": "string" },
    { "maxLength": 5 }
  ]
}

up to navigation

Arguments - Object[]|Object Can accept mix of Object[] and Object

Example

jst.string().anyOf(
    [
        {type: 'string', pattern: "^\\d+$"},
        jst.string().min(10).done()
    ]
).done();

Result

{
  "anyOf": [
    {
      "type": "string",
      "pattern": "^\\d+$"
    },
    {
      "type": "string",
      "minLength": 10
    }
  ]
}

up to navigation

Arguments - Array|* Can accept mix of Array and *

Example

jst.string().enum(['one', 'two', 'three']).done();

Result

{
  "type": "string",
  "enum": [
    "one",
    "two",
    "three"
  ]
}

up to navigation

Arguments - positive Number as integer

Example

jst.string().max(10).done();

Result

{
  "type": "string",
  "maxLength": 10
}

up to navigation

Arguments - positive Number as integer

Example

jst.string().min(1).done();

Result

{
  "type": "string",
  "minLength": 1
}

up to navigation

Arguments - Object

Example

jst.string().not({enum: ['one', 'two', 'three']}).done();

Result

{
  "type": "string",
  "not": {
    "enum": [
      "one",
      "two",
      "three"
    ]
  }
}

up to navigation

Arguments - Object[]|Object Can accept mix of Object[] and Object

Example

jst.string().oneOf(
    [
        { type: 'string', maxLength: 5 },
        jst.string().max(3).done()
    ]
).done();

Result

{
  "oneOf": [
    { "type": "string", "maxLength": 5 },
    { "type": "string", "maxLength": 3 }
  ]
}

up to navigation

Arguments - RegExp|String

Example

jst.string().pattern("^\\d+$").done();

Result

{
  "type": "string",
  "pattern": "^\\d+$"
}

up to navigation

Arguments - no Finalize creation JSON schema template by type and return complete object.

Example

jst.string().max(10).done();

Result

{
  "type": "string",
  "maxLength": 10
}

up to navigation

npm run test

up to navigation

MIT License. Copyright (c) 2017 Mobitel Ltd

up to navigation

About

NodeJs module for helping and simplify creation JSON schemas

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors