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

mobitel-json-schema-template

v1.0.3

Published

NodeJs module for helping creation JSON schemas

Downloads

3,876

Readme

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.

Navigation

Installation

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

Example

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

API

Initializing

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

Returns object for generating a JSON schema elements. up to navigation

.allOf(arg[, arg2[, arg3]...])

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

.anyOf(arg[, arg2[, arg3]...])

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

.boolean([arg])

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

.enum(arg[, arg2[, arg3]...])

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

Example

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

Result

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

up to navigation

.not(arg)

Arguments - Object

Example

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

Result

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

up to navigation

.null()

Arguments - no

Example

jst.null();

Result

{
  "type": "null"
}

up to navigation

.oneOf(arg[, arg2[, arg3]...])

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

.ref(arg)

Arguments - String

Example

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

Result

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

up to navigation

.stringFormat(arg)

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

.array()

Arguments - no

Example

jst.array().done();

Result

{
  "type": "array"
}

up to navigation

.array().additional(arg)

Arguments - Boolean

Example

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

Result

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

up to navigation

.array().items(arg[, arg2[, arg3]...])

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

.array().max(arg)

Arguments - positive Number

Example

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

Result

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

up to navigation

.array().min(arg)

Arguments - positive Number

Example

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

Result

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

up to navigation

.array().unique()

Arguments - no

Example

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

Result

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

up to navigation

.array().done()

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

.integer()

Arguments - no

Example

jst.integer().done();

Result

{
  "type": "integer"
}

up to navigation

.integer().allOf(arg[, arg2[, arg3]...])

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

.integer().anyOf(arg[, arg2[, arg3]...])

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

.integer().eMax()

Arguments - no

Example

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

Result

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

up to navigation

.integer().eMin()

Arguments - no

Example

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

Result

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

up to navigation

.integer().enum(arg[, arg2[, arg3]...])

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

.integer().max(arg)

Arguments - Number as integer

Example

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

Result

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

up to navigation

.integer().min(arg)

Arguments - Number as integer

Example

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

Result

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

up to navigation

.integer().multipleOf(arg)

Arguments - positive Number as integer

Example

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

Result

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

up to navigation

.integer().not(arg)

Arguments - Object

Example

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

Result

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

up to navigation

.integer().oneOf(arg[, arg2[, arg3]...])

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

.integer().done()

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

.number()

Arguments - no

Example

jst.number().done();

Result

{
  "type": "number"
}

up to navigation

.number().allOf(arg[, arg2[, arg3]...])

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

.number().anyOf(arg[, arg2[, arg3]...])

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

.number().eMax()

Arguments - no

Example

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

Result

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

up to navigation

.number().eMin()

Arguments - no

Example

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

Result

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

up to navigation

.number().enum(arg[, arg2[, arg3]...])

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

.number().max(arg)

Arguments - Number

Example

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

Result

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

up to navigation

.number().min(arg)

Arguments - Number

Example

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

Result

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

up to navigation

.number().multipleOf(arg)

Arguments - positive Number as integer

Example

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

Result

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

up to navigation

.number().not(arg)

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

.number().oneOf(arg[, arg2[, arg3]...])

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

.number().done()

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

.string()

Arguments - no

Example

jst.string().done();

Result

{
  "type": "string"
}

up to navigation

.string().allOf(arg[, arg2[, arg3]...])

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

.string().anyOf(arg[, arg2[, arg3]...])

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

.string().enum(arg[, arg2[, arg3]...])

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

.string().max(arg)

Arguments - positive Number as integer

Example

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

Result

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

up to navigation

.string().min(arg)

Arguments - positive Number as integer

Example

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

Result

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

up to navigation

.string().not(arg)

Arguments - Object

Example

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

Result

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

up to navigation

.string().oneOf(arg[, arg2[, arg3]...])

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

.string().pattern(arg)

Arguments - RegExp|String

Example

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

Result

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

up to navigation

.string().done()

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

Testing

npm run test

up to navigation

License

MIT License. Copyright (c) 2017 Mobitel Ltd up to navigation