mobitel-json-schema-template
v1.0.3
Published
NodeJs module for helping creation JSON schemas
Downloads
4,161
Maintainers
Readme
Mobitel Ltd. JSON-Schema template
A small helper for generating a JSON schema elements.
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
- Example
- API
- Testing
- License
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"}
}
}
}
}
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 }
]
}
.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" }
]
}
.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"
}
.enum(arg[, arg2[, arg3]...])
Arguments - Array|*
Can accept mix of Array
and *
Example
jst.enum(['one', 'two', 'three']);
Result
{
"enum": [
"one",
"two",
"three"
]
}
.not(arg)
Arguments - Object
Example
jst.not({type: 'string'});
Result
{
"not": {"type": "string"}
}
.null()
Arguments - no
Example
jst.null();
Result
{
"type": "null"
}
.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 }
]
}
.ref(arg)
Arguments - String
Example
jst.ref('#/definitions/subschema');
Result
{
"$ref": "#/definitions/address"
}
.stringFormat(arg)
Arguments - String
Argument must be values like:
- date-time
- hostname
- ipv4
- ipv6
- uri
Example
jst.stringFormat('hostname');
Result
{
"type": "string",
"format": "hostname"
}
.array()
Arguments - no
Example
jst.array().done();
Result
{
"type": "array"
}
.array().additional(arg)
Arguments - Boolean
Example
jst.array().additional(true).done();
Result
{
"type": "array",
"additionalItems": true
}
.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"}
]
}
.array().max(arg)
Arguments - positive Number
Example
jst.array().max(10).done();
Result
{
"type": "array",
"maxItems": 10
}
.array().min(arg)
Arguments - positive Number
Example
jst.array().min(1).done();
Result
{
"type": "array",
"minItems": 1
}
.array().unique()
Arguments - no
Example
jst.array().unique().done();
Result
{
"type": "array",
"uniqueItems": true
}
.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
}
.integer()
Arguments - no
Example
jst.integer().done();
Result
{
"type": "integer"
}
.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 }
]
}
.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
}
]
}
.integer().eMax()
Arguments - no
Example
jst.integer().eMax().done();
Result
{
"type": "integer",
"exclusiveMaximum": true
}
.integer().eMin()
Arguments - no
Example
jst.integer().eMin().done();
Result
{
"type": "integer",
"exclusiveMinimum": true
}
.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]
}
.integer().max(arg)
Arguments - Number
as integer
Example
jst.integer().max(10).done();
Result
{
"type": "integer",
"maximum": 10
}
.integer().min(arg)
Arguments - Number
as integer
Example
jst.integer().min(1).done();
Result
{
"type": "integer",
"minimum": 1
}
.integer().multipleOf(arg)
Arguments - positive Number
as integer
Example
jst.integer().multipleOf(10).done();
Result
{
"type": "integer",
"multipleOf": 10
}
.integer().not(arg)
Arguments - Object
Example
jst.integer().not({enum: [1, 2, 3]}).done();
Result
{
"type": "integer",
"not": {
"enum": [1, 2, 3]
}
}
.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 }
]
}
.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
}
.number()
Arguments - no
Example
jst.number().done();
Result
{
"type": "number"
}
.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 }
]
}
.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
}
]
}
.number().eMax()
Arguments - no
Example
jst.number().eMax().done();
Result
{
"type": "number",
"exclusiveMaximum": true
}
.number().eMin()
Arguments - no
Example
jst.number().eMin().done();
Result
{
"type": "number",
"exclusiveMinimum": true
}
.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]
}
.number().max(arg)
Arguments - Number
Example
jst.number().max(10.5).done();
Result
{
"type": "number",
"maximum": 10.5
}
.number().min(arg)
Arguments - Number
Example
jst.number().min(1.5).done();
Result
{
"type": "number",
"minimum": 1.5
}
.number().multipleOf(arg)
Arguments - positive Number
as integer
Example
jst.number().multipleOf(10).done();
Result
{
"type": "number",
"multipleOf": 10
}
.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
]
}
}
.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 }
]
}
.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
}
.string()
Arguments - no
Example
jst.string().done();
Result
{
"type": "string"
}
.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 }
]
}
.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
}
]
}
.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"
]
}
.string().max(arg)
Arguments - positive Number
as integer
Example
jst.string().max(10).done();
Result
{
"type": "string",
"maxLength": 10
}
.string().min(arg)
Arguments - positive Number
as integer
Example
jst.string().min(1).done();
Result
{
"type": "string",
"minLength": 1
}
.string().not(arg)
Arguments - Object
Example
jst.string().not({enum: ['one', 'two', 'three']}).done();
Result
{
"type": "string",
"not": {
"enum": [
"one",
"two",
"three"
]
}
}
.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 }
]
}
.string().pattern(arg)
Arguments - RegExp|String
Example
jst.string().pattern("^\\d+$").done();
Result
{
"type": "string",
"pattern": "^\\d+$"
}
.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
}
Testing
npm run test
License
MIT License. Copyright (c) 2017 Mobitel Ltd up to navigation