
Product
Introducing GitHub Actions Scanning Support
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.
another-json-schema
Advanced tools
Another JSON Schema, simple & flexible & intuitive.
$ npm i another-json-schema --save
simple:
var AJS = require('another-json-schema');
var userSchema = AJS('userSchema', {
name: { type: 'string' },
age: { type: 'number', gte: 18 }
});
var user = {
name: 'nswbmw',
age: 17
};
console.log(userSchema.validate(user));
/*
{ valid: false,
error:
{ [Error: ($.age: 17) âś– (gte: 18)]
validator: 'gte',
actual: 17,
expected: { type: 'number', gte: 18 },
path: '$.age',
schema: 'userSchema' },
result: { name: 'nswbmw', age: 17 } }
*/
complex:
var AJS = require('another-json-schema');
var userSchema = AJS('userSchema', {
_id: { type: 'string', pattern: /^[0-9a-z]{24}$/ },
name: { type: 'string' },
age: { type: 'number', gte: 18 },
gender: { type: 'string', enum: ['male', 'female'] }
});
var commentSchema = AJS('commentSchema', {
_id: { type: 'string', pattern: /^[0-9a-z]{24}$/ },
user: userSchema,
content: { type: 'string' }
});
var postSchema = AJS('postSchema', {
_id: { type: 'string', pattern: /^[0-9a-z]{24}$/ },
author: userSchema,
content: { type: 'string' },
comments: [commentSchema]
});
var post = {
_id: 'post11111111111111111111',
author: {
_id: 'user11111111111111111111',
name: 'nswbmw',
age: 100,
gender: 'male',
pet: 'cat'
},
content: 'lalala',
comments: [{
_id: 'comment11111111111111111',
user: {
_id: 'wrong_id',
name: 'user1',
age: 100,
gender: 'male'
},
content: 'sofa'
}]
};
console.log(postSchema.validate(post));
/*
{ valid: false,
error:
{ [Error: ($.comments[].user._id: "wrong_id") âś– (pattern: /^[0-9a-z]{24}$/)]
validator: 'pattern',
actual: 'wrong_id',
expected: { type: 'string', pattern: /^[0-9a-z]{24}$/ },
path: '$.comments[].user._id',
schema: 'userSchema' },
result:
{ _id: 'post11111111111111111111',
author:
{ _id: 'user11111111111111111111',
name: 'nswbmw',
age: 100,
gender: 'male' },
content: 'lalala',
comments: [ [Object] ] } }
*/
custom validate function(like: ObjectId):
var validator = require('validator');
var toObjectId = require('mongodb').ObjectId;
var AJS = require('another-json-schema');
var postSchema = AJS('postSchema', {
author: {
type: function ObjectId(value) {
if (!value || !validator.isMongoId(value.toString())) {
throw new Error('author is not a valid ObjectId');
}
return toObjectId(value);
}
},
content: { type: 'string' }
});
var post = {
author: '111111111111111111111111',
content: 'haha'
};
console.log(postSchema.validate(post));
/*
{ valid: true,
error: null,
result: { author: 111111111111111111111111, content: 'haha' } }
*/
//validate specific field
console.log(postSchema._children.author.validate('lalala'));
/*
{ valid: false,
error:
{ [Error: ($.author: "lalala") âś– (type: ObjectId)]
validator: 'type',
actual: 'lalala',
expected: { type: [Function: ObjectId] },
path: '$.author',
schema: 'postSchema',
originError: [Error: author is not a valid ObjectId] },
result: 'lalala' }
*/
Note: type validator is special, it can overwrite original value by value returned from function. others validator can only validate its value.
Constructor.
Register a validator. eg:
AJS.register('gt', function (actual, expected, key, parentNode) {
return actual > expected;
});
Compile a schema. The following two ways are the same:
var userSchema = AJS('userSchema', {
_id: { type: 'string', pattern: /^[0-9a-z]{24}$/ },
name: { type: 'string' },
age: { type: 'number', gte: 18 },
gender: { type: 'string', enum: ['male', 'female'] }
});
var newSchema = new AJS();
var userSchema = newSchema.compile('userSchema', {
_id: { type: 'string', pattern: /^[0-9a-z]{24}$/ },
name: { type: 'string' },
age: { type: 'number', gte: 18 },
gender: { type: 'string', enum: ['male', 'female'] }
});
Use the compiled template to validate a json. returns a object:
($.comments[].user._id: "wrong_id") âś– (pattern: /^[0-9a-z]{24}$/)pattern,wrong_id,{ type: 'string', pattern: /^[0-9a-z]{24}$/ },$.comments[].user._id,userSchemaopts:
false[]. default: falsesee test.
npm test (coverage 100%)
MIT
FAQs
Another JSON Schema, simple & flexible & intuitive.
The npm package another-json-schema receives a total of 319 weekly downloads. As such, another-json-schema popularity was classified as not popular.
We found that another-json-schema demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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.

Product
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.

Product
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.

Research
The Socket Threat Research Team uncovered malicious NuGet packages typosquatting the popular Nethereum project to steal wallet keys.