Verify JSON using minimal schema

栏目: IT技术 · 发布时间: 4年前

内容简介:Library to verify JSON structure easily using a lightweight JSON schema syntaxThis project results from my need to verify JSON schema in a lightweight manner, without the need for an extensive definition and development code.The schema syntax is minimalist

Verify JSON

Library to verify JSON structure easily using a lightweight JSON schema syntax

About

This project results from my need to verify JSON schema in a lightweight manner, without the need for an extensive definition and development code.

The schema syntax is minimalist and extremely easy to write.

Installation

npm install -s verify-json

const verify = require('verify-json')

import verify from 'verify-json';

Example

const { verify } = require("verify-json");

let json = {
  markers: [
    {
      name: "Rixos The Palm Dubai",
      location: [25.1212, 55.1535],
      favorite: true,
      color: "red",
    },
    {
      name: "Shangri-La Hotel",
      location: [25.2084, 55.2719],
      color: "blue",
    },
  ],
};

// <key>:<validator>
// <key>:!<validator> - uses ! for optional
// <key> - required non null attribute of any type
// Skip all the quotations
const schema = `{markers: [{
      name:string,
      location:[:lat,:long],
      favorite:!b,
      color
  }]
}`;

// customValidators are optional. See built-in validators.
const customValidators = {
  lat: (val) => val >= -90 && val <= 90,
  long: (val) => val >= -180 && val <= 180,
};

let result = verify(json, schema, customValidators);

console.log(result); // true

json.markers[0].location[0] = 1000;

try {
  verify(json, schema, customValidators);
} catch (error) {
  console.log("error", error); // json.markers.0.location.0: validation failed
}

Built-in Validators

Following validators are built in and can be used directly -

{
    string    : _.isString,
    s         : _.isString,      // alias for string
    number    : _.isNumber,
    n         : _.isNumber,      // alias for number
    boolean   : _.isBoolean,
    b         : _.isBoolean,     // alias for boolean
    integer   : _.isInteger,
    i         : _.isInteger,     // alias for integer
    func      : _.isFunction,
    f         : _.isFunction,    // alias for func
    an        : (v) => v.match(/^[a-zA-Z0-9]+$/), // alpha-numeric
}

Use as a mixin

Since lodash is a dependency, this method is also exposed as a lodash mixin. Once imported anywhere, you can simply use _.verify to access it.

_.verify(json, schema, customValidators)

License

MIT © Yusuf Bhabhrawala


以上所述就是小编给大家介绍的《Verify JSON using minimal schema》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

微信力量

微信力量

谢晓萍等著 / 机械工业出版社华章公司 / 2015-10-28 / 59.00

微信俨然已进化为一种万能的连接器,拥有连接一切的能力,彰显出强大的连接力,无处不在,无所不能。本书将为你讲述连接为何能产生如此巨大的力量,这股力量正在商业和民生领域产生的变化,将为你展现微信生态进化的全景世界。 连接即入口,微信上的每一次有效的连接都会形成有效的入口。本书详细讲解了如何通过微信帮助餐饮、酒店、医院、零售、商场、电气、母婴、航空、客运、停车场、商圈、城市服务等数十个传统行业与它......一起来看看 《微信力量》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

随机密码生成器
随机密码生成器

多种字符组合密码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码