This library provide all the core validation functions for couple other libraries. Also it has a plugin system, and some commonly use plugins to help with the validation.
As of version 0.8.0, we move all the plugins related methods into this module. And this will be able to share between many different
@jsonql/validator
instance to use, instead of every time we init a ValidatorFactory
and have to load all the plugins again.
More to come
The core library provide function to validate against primitive types and their value. When we need to test the value in a more specific way, we can use the plugin system. Also this plugin system allow developer to create their own plugin to suit their specific need.
A plugin file looks like this:
// test for integer
const name = "int"
function main(value: number): boolean {
return Number.isInteger(value)
}
export default {
name,
main,
}
If you require additional parameter:
// example from our built-in plugin between
// between
import moreThan from './more-than'
import lessThan from './less-than'
const name = 'between'
function main(
max: number,
min: number,
value: number | string
): boolean {
return lessThan.main(max, value) && moreThan.main(min, value)
}
// Then when we register it, we know what `params` we should expect
export default {
main,
name,
params: ['max', 'min']
}
The value
that will get validate MUST be the last argument. Because we will curry the main method before insert into our validation queue system.
Here is list of available built-in plugins:
{ plugin: 'between', max: 100, min: 1}
Check a number or string (length) is < max
and > min
{ plugin: 'email'}
Check if the input is email address
{ plugin: 'float' }
Check if the value is a float number
{ plugin: 'int' }
Check if the value is an signed integer
{ plugin: 'lessThanEqual', num: 100}
Check if a number or string (length) is =< num
{ plugin: 'lessThan', num: 100}
Check if a number or string (length) is < num
{ plugin: 'moreThanEqual', num: 100}
Check if a number or string (length) is >= num
{ plugin: 'moreThan', num: 100}
Check if a number of string (length) is > num
{ plugin: 'uint' }
Check if a number is a unsigned integer (>=0)
{ plugin: 'within', max: 100, min: 1}
Check if a number or string (length) is <= max
and >= min
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。