yom-json-validator

1.2.0 • Public • Published

yom-json-validator

Validate json with schema, and strip properties not defined in schema.

Run code

var schema = {
  type: {
    $type: 'string',
    $set: ['success', 'error'],
    $default: 'success',
    $nullable: false
  },
  data: {
    $type: 'array',
    $nullable: false,
    $minLength: 0,
    $maxLength: 100,
    $item: {
      $type: 'object',
      $nullable: false,
      $value: {
        firstName: '1', // not nullable
        lastName: '', // nullable
        fullName: {
          $type: 'dynamic',
          $validator: function (opt) {
            return opt.value || opt.getValueByPath('./firstName') + ' ' + opt.getValueByPath('./lastName');
          }
        },
        title: {
          $type: 'string',
          $minLength: 1,
          $maxLength: 20,
          $default: 'manager'
        },
        height: 0, // nullable
        class: 1, // not nullable
        grade: {
          $type: 'number',
          $set: [1, 2, 3],
          $nullable: true
        },
        age: {
          $type: 'number',
          $min: 1,
          $max: 150,
          $default: 1
        },
        vip: false, // nullable
        member: true, // not nullable
        admin: {
          $type: 'boolean',
          $default: false,
          $nullable: false
        },
        skills: ['', null], // nullable
        hobbits: [''], // not nullable
        relatives: [{
          name: ''
        }]
      }
    }
  }
};

var data = {
  type: 'success',
  data: [{
    firstName: 'Gary',
    lastName: 'Wang',
    class: 1,
    member: true,
    admin: false,
    hobbits: [],
    relatives: [],
    some: '',
    other: 1
  }]
};

YomJsonValidator.validate(schema, data);

Returns

{
  type: 'success',
  data: [{
    firstName: 'Gary',
    lastName: 'Wang',
    fullName: 'Gary Wang',
    class: 1,
    member: true,
    admin: false,
    hobbits: [],
    relatives: []
  }]
}

Readme

Keywords

Package Sidebar

Install

npm i yom-json-validator

Weekly Downloads

1

Version

1.2.0

License

MIT

Unpacked Size

27.2 kB

Total Files

8

Last publish

Collaborators

  • webyom