Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

another-json-schema

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

another-json-schema

Another JSON Schema, simple & flexible & intuitive.

  • 1.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
24
increased by100%
Maintainers
1
Weekly downloads
 
Created
Source

another-json-schema

Another JSON Schema, simple & flexible & intuitive.

NPM version Build status Dependency Status License Downloads

Install

npm i another-json-schema --save

Usage

var AJS = require('another-json-schema');

var userSchema = AJS('userSchema', {
  _id: { type: 'string', pattern: /^[0-9a-z]{24}$/ },
  name: { type: 'string', required: true },
  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', required: true }
});

var postSchema = AJS('postSchema', {
  _id: { type: 'string', pattern: /^[0-9a-z]{24}$/ },
  author: userSchema,
  content: { type: 'string', required: true },
  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'
  }, {
    _id: 'comment22222222222222222',
    user: {
      _id: 'user22222222222222222222',
      name: 'user2',
      age: 100,
      gender: 'female'
    },
    content: 'bench'
  }]
};

var output = postSchema.validate(post);

assert.deepEqual(output.error.message, '($.comments[].user._id: "wrong_id") ✖ (pattern: /^[0-9a-z]{24}$/)');
assert.deepEqual(output, {
  valid: false,
  error: {
    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: [{
      _id: 'comment11111111111111111',
      user: {
        _id: 'wrong_id',
        name: 'user1',
        age: 100,
        gender: 'male'
      },
      content: 'sofa'
    }, {
      _id: 'comment22222222222222222',
      user: {
        _id: 'user22222222222222222222',
        name: 'user2',
        age: 100,
        gender: 'female'
      },
      content: 'bench'
    }]
  }
});

API

AJS([name], schema)

Constructor.

AJS.register(name, fn)

Register a validator. eg:

AJS.register('gt', function (actual, expected, key, parentNode) {
  return actual > expected;
});
schema.compile([name], schema)

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', required: true },
  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', required: true },
  age: { type: 'number', gte: 18 },
  gender: { type: 'string', enum: ['male', 'female'] }
});
compiledSchema.validate(data, [opts])

Use the compiled template to validate a json. returns a object:

  • valid: {Boolean} wether a valid json
  • error: {Error|null}
    • message: error message, eg: ($.comments[].user._id: "wrong_id") ✖ (pattern: /^[0-9a-z]{24}$/)
    • validator: validator name, eg: pattern,
    • actual: actual value, eg: wrong_id,
    • expected: expected schema, eg: { type: 'string', pattern: /^[0-9a-z]{24}$/ },
    • path: path in object, eg: $.comments[].user._id,
    • schema: schema name, eg: userSchema
  • result: {Any}

opts:

  • additionalProperties: {Boolean} if true, retain the original field. default false
  • gt, gte, lt, lte ...: {Boolean} if false, will not execute this build-in validator.

More examples

see test.

Test

npm test (coverage 100%)

License

MIT

Keywords

FAQs

Package last updated on 28 Apr 2016

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc