
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
mobitel-json-schema-template
Advanced tools
A small helper for generating a JSON schema elements.
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"}
}
}
}
}
const jst = require('mobitel-json-schema-template');
Returns object for generating a JSON schema elements.
Arguments - Object[]|Object
Can accept mix of Object[]
and Object
Example
jst.allOf(
[
{ type: 'string' },
{ maxLength: 5 }
]
);
Result
{
"allOf": [
{ "type": "string" },
{ "maxLength": 5 }
]
}
Arguments - Object[]|Object
Can accept mix of Object[]
and Object
Example
jst.anyOf(
[
{type: 'string'},
jst.number().done()
]
);
Result
{
"anyOf": [
{ "type": "string" },
{ "type": "number" }
]
}
Arguments - Boolean
or 'all'
(default)
Example Boolean
jst.boolean(true);
Result Boolean
{
"type": "boolean",
"enum": [true]
}
Example 'all'
jst.boolean();
Result 'all'
{
"type": "boolean"
}
Arguments - Array|*
Can accept mix of Array
and *
Example
jst.enum(['one', 'two', 'three']);
Result
{
"enum": [
"one",
"two",
"three"
]
}
Arguments - Object
Example
jst.not({type: 'string'});
Result
{
"not": {"type": "string"}
}
Arguments - no
Example
jst.null();
Result
{
"type": "null"
}
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 }
]
}
Arguments - String
Example
jst.ref('#/definitions/subschema');
Result
{
"$ref": "#/definitions/address"
}
Arguments - String
Argument must be values like:
Example
jst.stringFormat('hostname');
Result
{
"type": "string",
"format": "hostname"
}
Arguments - no
Example
jst.array().done();
Result
{
"type": "array"
}
Arguments - Boolean
Example
jst.array().additional(true).done();
Result
{
"type": "array",
"additionalItems": true
}
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"}
]
}
Arguments - positive Number
Example
jst.array().max(10).done();
Result
{
"type": "array",
"maxItems": 10
}
Arguments - positive Number
Example
jst.array().min(1).done();
Result
{
"type": "array",
"minItems": 1
}
Arguments - no
Example
jst.array().unique().done();
Result
{
"type": "array",
"uniqueItems": true
}
Arguments - no Finalize creation JSON schema template by type and return complete object.
Example
jst.array().max(10).done();
Result
{
"type": "array",
"maxItems": 10
}
Arguments - no
Example
jst.integer().done();
Result
{
"type": "integer"
}
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 }
]
}
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
}
]
}
Arguments - no
Example
jst.integer().eMax().done();
Result
{
"type": "integer",
"exclusiveMaximum": true
}
Arguments - no
Example
jst.integer().eMin().done();
Result
{
"type": "integer",
"exclusiveMinimum": true
}
Arguments - Array|*
Can accept mix of Array
and *
Example
jst.integer().enum([1, 2, 3]).done();
Result
{
"type": "integer",
"enum": [1, 2, 3]
}
Arguments - Number
as integer
Example
jst.integer().max(10).done();
Result
{
"type": "integer",
"maximum": 10
}
Arguments - Number
as integer
Example
jst.integer().min(1).done();
Result
{
"type": "integer",
"minimum": 1
}
Arguments - positive Number
as integer
Example
jst.integer().multipleOf(10).done();
Result
{
"type": "integer",
"multipleOf": 10
}
Arguments - Object
Example
jst.integer().not({enum: [1, 2, 3]}).done();
Result
{
"type": "integer",
"not": {
"enum": [1, 2, 3]
}
}
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 }
]
}
Arguments - no Finalize creation JSON schema template by type and return complete object.
Example
jst.integer().max(10).done();
Result
{
"type": "integer",
"maximum": 10
}
Arguments - no
Example
jst.number().done();
Result
{
"type": "number"
}
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 }
]
}
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
}
]
}
Arguments - no
Example
jst.number().eMax().done();
Result
{
"type": "number",
"exclusiveMaximum": true
}
Arguments - no
Example
jst.number().eMin().done();
Result
{
"type": "number",
"exclusiveMinimum": true
}
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]
}
Arguments - Number
Example
jst.number().max(10.5).done();
Result
{
"type": "number",
"maximum": 10.5
}
Arguments - Number
Example
jst.number().min(1.5).done();
Result
{
"type": "number",
"minimum": 1.5
}
Arguments - positive Number
as integer
Example
jst.number().multipleOf(10).done();
Result
{
"type": "number",
"multipleOf": 10
}
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
]
}
}
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 }
]
}
Arguments - no Finalize creation JSON schema template by type and return complete object.
Example
jst.number().max(10).done();
Result
{
"type": "number",
"maximum": 10
}
Arguments - no
Example
jst.string().done();
Result
{
"type": "string"
}
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 }
]
}
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
}
]
}
Arguments - Array|*
Can accept mix of Array
and *
Example
jst.string().enum(['one', 'two', 'three']).done();
Result
{
"type": "string",
"enum": [
"one",
"two",
"three"
]
}
Arguments - positive Number
as integer
Example
jst.string().max(10).done();
Result
{
"type": "string",
"maxLength": 10
}
Arguments - positive Number
as integer
Example
jst.string().min(1).done();
Result
{
"type": "string",
"minLength": 1
}
Arguments - Object
Example
jst.string().not({enum: ['one', 'two', 'three']}).done();
Result
{
"type": "string",
"not": {
"enum": [
"one",
"two",
"three"
]
}
}
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 }
]
}
Arguments - RegExp|String
Example
jst.string().pattern("^\\d+$").done();
Result
{
"type": "string",
"pattern": "^\\d+$"
}
Arguments - no Finalize creation JSON schema template by type and return complete object.
Example
jst.string().max(10).done();
Result
{
"type": "string",
"maxLength": 10
}
npm run test
FAQs
NodeJs module for helping creation JSON schemas
We found that mobitel-json-schema-template demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.