watsons是被prop-types启发的,一个强大的参数验证及JSON schema验证模块。具有代码量低,可扩展性极强,效率极高的特点,支持node6及以上。使用方法类似prop-types,例子如下:
const watsons = require('watsons.js')
const obj = {
s: "string value",
n: 3,
a: [1, 2, 3, 4],
e: "female",
shape: {
a: 100,
b: [1, 2, true]
},
nullableString: null,
place: "New York"
}
const checker = {
s: watsons.string,
b: watsons.bool,
n: watsons.number.required,
a: watsons.arrayOf(
watsons.number
),
e: watsons.oneOf(["male", "female"]),
shape: watsons.shape({
a: watsons.number.required,
b: watsons.array.required
}),
nullableString: watsons.oneOfType([
watsons.string,
watsons.null
]).required,
place: watsons.validateWith((v) => v === "New York")
}
watsons.validate(obj, checker) // will not throw
github地址:https://github.com/zhangkaiyulw/watsons
安装方式:
npm install watsons.js