PSV 发布 3.1.4 版,JS 数据格式验证工具

栏目: 软件资讯 · 发布时间: 7年前

内容简介:JS 数据格式验证工具 PSV 新发布 3.1.4 版本,较之前优化语法结构,测试覆盖率提升至99%。 Porco Schema Validate (PSV) 是一款轻量级 JS 数据格式验证工具,相比于其他功能齐备的验证工具,PSV 的优势在于体积...

JS 数据格式验证工具 PSV 新发布 3.1.4 版本,较之前优化语法结构,测试覆盖率提升至99%。

Porco Schema Validate (PSV) 是一款轻量级 JS 数据格式验证工具,相比于其他功能齐备的验证工具,PSV 的优势在于体积非常小,最开始的核心代码只有 130 行。因此 PSV 非常适合做小型项目、原型试错、个人 Demo 以及教学。

下载、安装

npm install psv --save

使用

首先你需要定义出自己的 schema,比如我:

var schema = {
    key1: {
        type: String,
        required: true
    },
    key2: {
        type: String,
        required: true
    },
};

这个 schema 的意思是,两个字段(key1,key2),都是必填,string 类型。那么我传入待验证的 data 结构是:

var data = {
    key1: 'psv',
    key2: 'psv',
}

接着我们导入并创建 PSV 对象进行验证

import Psv from 'psv';
function testPsv(schema, data) {
	const psv = new Psv(schema, data);
	const validate = psv.validate();
	if (!validate) {
		psv.printErrors();
	}
}

上面的代码首先创建 PSV 对象,并通过构造函数传入 schema 和 data。接着调用 validate 函数,该函数返回值为 true or false, 如果为 true 代表 data 符合 schema 定义,为 false 的话,可以通过 psv.printErrors() 或者 psv.getErrors() 来获取错误信息。


【声明】文章转载自:开源中国社区 [http://www.oschina.net]


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

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

Numerical Methods and Methods of Approximation in Science and En

Numerical Methods and Methods of Approximation in Science and En

Karan Surana / CRC Press / 2018-10-31

ABOUT THIS BOOK Numerical Methods and Methods of Approximation in Science and Engineering prepares students and other readers for advanced studies involving applied numerical and computational anal......一起来看看 《Numerical Methods and Methods of Approximation in Science and En》 这本书的介绍吧!

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具