PSVJS 数据格式验证工具
Porco Schema Validate (psv)
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() 来获取错误信息。
api
目前支持五种数据类型的定义:String, Number, Array, Boolean, Object。
1.String
str: {
type: String,
max: 5,
min: 3,
pattern: '^[0-9]*$',
required: true
},
max : 最大长度 (数字)
min : 最小长度 (数字)
pattern : 自定义正则表达式 (正则表达式)
required : 是否必须 (布尔)
2.Number
num: {
type: Number,
max: 5,
min: 3,
required: true
},
max : 最大值 (数字)
min : 最小值 (数字)
required : 是否必须 (布尔)
3.Array
array: {
type: [Number],
max: 5,
min: 3,
required: true
},
max : 最大长度 (数字)
min : 最小长度 (数字)
required : 是否必须 (布尔)
数组支持对象元素检测(可嵌套,同样,根据我们团队的实际应用场景,我们建议采用扁平化的数据结构设计)
const schema2 = {
str: {
type: String,
required: true
},
num: {
type: Number,
required: true
}
};
const schema = {
key: {
type: [schema2],
required: true
}
};
如果你不希望对数组检测,或者说,数组元素类型不确定,可以采用 Array 定义,我们将不会对其进行类型检测
const schema = {
array: {
type: Array,
max: 5,
min: 3,
required: true
}
};
const data = {
array: [1, '1', true, {}]
};
4.Boolean
boo: {
type: Boolean,
required: true
},
required : 是否必须 (布尔)
5.Object
object: {
type: Object,
required: true
},
required : 是否必须 (布尔)
注意:当 type = Object 时,说明该字段可以是任何 js 基本类型或对象,甚至可以是 一个 函数(慎用)。
同样,psv 支持嵌套定义
const schema2 = {
str2: {
type: String,
required: true
}
}
const schema = {
str1: {
type: schema2,
required: true
},
};
升级:
2.1.8
开始加入枚举类型,目前仅限 string, number 两大类型支持。
string :
const schema = {
enum: {
type: String,
enum: ['1', '2', '3'],
required: true
}
};
const data = {
enum: '1'
};
number:
const schema = {
enum: {
type: Number,
enum: [1, 2, 3],
required: true
}
};
const data = {
enum: 3
};