Recommend a Node.js parameter verification module-minijoi
intention of
Because when using Joi
string.trim().required()
and so on must be written every time during verification. As parameter verification is frequent and necessary, more and more writing, the code is neither good-looking, nor easy to maintain, the pattern rules are not easy to remember, and joi throw
needs to be dealt with separately. So most commonly used for daily calibration, encapsulates Joi's API, and can simultaneously incoming call when the
custom Error, use
joi
friendly error stack message while
throw
our custom Error
, no longer Handle joi's Error separately. So there
miniJoi
, is simple version of joi.
Welcome to mention Issue and PR , the test cases of the code are in the tests
directory, write the test cases, and execute the command:
pnpm run coverage(推荐)
npm run coverage
yarn run coverage
The console will output the use case status and code coverage. Developers can also miniJoi
to create application modules that are more in line with their own.
minijoi
const miniJoi = require('minijoi');
miniJoi.requireAndNotEmptyForStr(value , options)
options : {
error : new Error("This is an Error") //公有
pattern : /^1[3456789]\d{9}$/ // email ID URL phone name
mode : 'strict' //phone
version : 'ipv6' //IP
generation : 'first' //ID
}
friendly stack prompt, on the basis of Joi, at the same time the error is reported to the developer, the developer can clearly see the input value and type, and the value of the _original field is the input value. , As follows:
Error [ValidationError]: "value" is not allowed to be empty, But the type of the argument passed in is [String], Please check the value in the "_original" field.
at Object.exports.process (D:\data\git\minijoi\node_modules\.pnpm\joi@17.4.2\node_modules\joi\lib\errors.js:184:16)
at Object.internals.entry (D:\data\git\minijoi\node_modules\.pnpm\joi@17.4.2\node_modules\joi\lib\validator.js:150:26)
at Object.exports.entry (D:\data\git\minijoi\node_modules\.pnpm\joi@17.4.2\node_modules\joi\lib\validator.js:27:30)
at internals.Base.validate (D:\data\git\minijoi\node_modules\.pnpm\joi@17.4.2\node_modules\joi\lib\base.js:548:26)
at validate (D:\data\git\minijoi\apply.js:12:37)
at Object.requireAndNotEmptyForStr (D:\data\git\minijoi\apply.js:39:12)
at Object.<anonymous> (D:\data\git\minijoi\test.js:101:7)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32) {
_original: '',
details: [
{
message: '"value" is not allowed to be empty, But the type of the argument passed in is [String], Please check the value in the "_original" field.',
path: [],
type: 'string.empty',
context: { label: 'value', value: '' }
}
]
}
Node.js version requirements:
supports Node.js V10, V12, V14, V16
The API is as follows:
Developers can customize the Error, just pass the error parameter when calling the API, miniJoi will automatically throw a developer-defined Error, and output the above error message by default.
String is required and not empty
miniJoi.requireAndNotEmptyForStr(value)
miniJoi.requireAndNotEmptyForStr(value , {error : new Error("This is an Error")})
The string is required and can be empty
miniJoi.requireAndIsEmptyForStr(value)
miniJoi.requireAndIsEmptyForStr(value , {error : new Error("This is an Error")})
Required string enumeration
miniJoi.requireAndEnumForStr(value , ["test"])
miniJoi.requireAndEnumForStr(value , ["test","test1"] , {error : new Error("This is an Error")})
Specific length of required string
miniJoi.length(value , limit)
miniJoi.length(value , limit , {error : new Error("This is an Error")})
Maximum length of required string
miniJoi.max(value , limit)
miniJoi.max(value , limit , {error : new Error("This is an Error")})
Minimum length of required string
miniJoi.min(value , limit)
miniJoi.min(value , limit , {error : new Error("This is an Error")})
Mandatory string Chinese name
miniJoi.name(value )
miniJoi.name(value , {error : new Error("This is an Error")})
miniJoi.name(value , {pattern : xxxx})
如miniJoi提供的中文姓名校验功能不符合开发者的要求,开发者可自定义传入正则表达式
Required string and legal email address
miniJoi.requireEmail(value )
miniJoi.requireEmail(value , {error : new Error("This is an Error")})
输入pattern字段,则使用pattern
miniJoi.requireEmail(value , {
error : new Error("This is an Error"),
pattern : /^1[3456789]\d{9}$/
})
如miniJoi提供的邮箱校验功能不符合开发者的要求,开发者可自定义传入正则表达式
The required character string and the legal phone number mode field refer to anyrule
miniJoi.requirePhone(value )
miniJoi.requirePhone(value , {error : new Error("This is an Error")})
输入pattern字段,则使用pattern
mode 'strict'||'loose'
strict 表示严谨
loose 表示宽松
默认为(最宽松), 只要是1开头即可
miniJoi.requirePhone(value , {
error : new Error("This is an Error"),
pattern : /^1[3456789]\d{9}$/
mode : 'strict'
})
如miniJoi提供的电话号码校验功能不符合开发者的要求,开发者可自定义传入正则表达式
Required string and legal IP
miniJoi.requireIP(value )
miniJoi.requireIP(value , {error : new Error("This is an Error")})
输入pattern字段,则使用pattern
version 'ipv4'||'ipv6'
ipv4 表示只校验ipv4
ipv6 表示只校验ipv6
默认同时校验ipv4和ipv6
miniJoi.requireIP(value , {
error : new Error("This is an Error"),
version : 'ipv6'
})
如miniJoi提供的IP校验功能不符合开发者的要求,开发者可自定义传入正则表达式
Required string and legal Url
miniJoi.requireUrl(value )
miniJoi.requireUrl(value , {error : new Error("This is an Error")})
输入pattern字段,则使用pattern
miniJoi.requireUrl(value , {
error : new Error("This is an Error"),
pattern : /^1[3456789]\d{9}$/
})
如miniJoi提供的Url校验功能不符合开发者的要求,开发者可自定义传入正则表达式
Required string and legal ID
miniJoi.requireID(value )
miniJoi.requireID(value , {error : new Error("This is an Error")})
输入pattern字段,则使用pattern
generation : "first" || "second"
first 表示只校验一代身份证
second 表示只校验二代身份证
默认同时校验一代和二代
miniJoi.requireID(value , {
error : new Error("This is an Error"),
pattern : /^1[3456789]\d{9}$/
generation : "first"
})
如miniJoi提供的身份证校验功能不符合开发者的要求,开发者可自定义传入正则表达式
Required number
miniJoi.requireForNum(value)
miniJoi.requireForNum(value , {error : new Error("This is an Error")})
Required integer
miniJoi.requireForInt(value)
miniJoi.requireForInt(value , {error : new Error("This is an Error")})
Required number enumeration
miniJoi.requireAndEnumForNum(value)
miniJoi.requireAndEnumForNum(value ,[1,2], {error : new Error("This is an Error")})
The decimal place of the required number shall not be greater than the limit place
miniJoi.requireAndPrecision(value)
miniJoi.requireAndPrecision(value , limit , {error : new Error("This is an Error")})
2.2 PASS
2.22 PASS
2.222 FAIL
Required number range API
miniJoi.requireForRangeNum(value ,op, limit )
miniJoi.requireForRangeNum(value ,op, limit , {error : new Error("This is an Error")})
op的值为 gt(>) || gte(>=) || lt(<) || lte(<=)
比如需要参数 > 0 , 则
miniJoi.requireForRangeNum(value ,"gt" , 0 ) 可代表正数
参数 >= 0
miniJoi.requireForRangeNum(value ,"gte" , 0 )
参数 <= 0
miniJoi.requireForRangeNum(value ,"lte" , 0 )
参数 < 0
miniJoi.requireForRangeNum(value ,"lt" , 0 ) 可代表负数
必填数字范围API
miniJoi.requireForRangeNum(value ,op, rangeArr )
miniJoi.requireForRangeNum(value ,op, rangeArr , {error : new Error("This is an Error")})
op的值为
left-close-right-close `[0,100]` 简称l-c-r-c
left-close-right-open `[0,100)` 简称 l-c-r-o
left-open-right-open `(0,100)` 简称 l-o-r-o
left-open-right-close `(0,100]` 简称 l-o-r-c
比如需要参数 > 0 and <= 100 , 则
miniJoi.requireForRangeNum(value ,"left-open-right-close" , [0,100] )
miniJoi.requireForRangeNum(value ,"l-o-r-c" , [0,100] )
比如需要参数 >= 0 and <= 100 , 则
miniJoi.requireForRangeNum(value ,"left-close-right-close" , [0,100] )
miniJoi.requireForRangeNum(value ,"l-c-r-c" , [0,100] )
比如需要参数 > 0 and < 100 , 则
miniJoi.requireForRangeNum(value ,"left-open-right-open" , [0,100] )
miniJoi.requireForRangeNum(value ,"l-o-r-o" , [0,100] )
比如需要参数 >= 0 and < 100 , 则
miniJoi.requireForRangeNum(value ,"left-close-right-open" , [0,100] )
miniJoi.requireForRangeNum(value ,"l-c-r-o" , [0,100] )
Mandatory integer range API
miniJoi.requireForRangeInt(value ,op, limit )
miniJoi.requireForRangeInt(value ,op, limit , {error : new Error("This is an Error")})
op的值为 gt(>) || gte(>=) || lt(<) || lte(<=)
比如需要参数 > 0 , 则
miniJoi.requireForRangeInt(value ,"gt" , 0 ) 可代表正整数
参数 >= 0
miniJoi.requireForRangeInt(value ,"gte" , 0 )
参数 <= 0
miniJoi.requireForRangeInt(value ,"lte" , 0 )
参数 < 0
miniJoi.requireForRangeInt(value ,"lt" , 0 ) 可代表负整数
必填整数范围API
miniJoi.requireForRangeInt(value ,op, rangeArr )
miniJoi.requireForRangeInt(value ,op, rangeArr , {error : new Error("This is an Error")})
Required Boolean
miniJoi.requireForBool(value)
miniJoi.requireForBool(value , {error : new Error("This is an Error")})
The array is required and not empty
miniJoi.requireAndNotEmptyForArr(value)
miniJoi.requireAndNotEmptyForArr(value , {error : new Error("This is an Error")})
The array is required and can be empty
miniJoi.requireAndIsEmptyForArr(value)
miniJoi.requireAndIsEmptyForArr(value , {error : new Error("This is an Error")})
Object is required and not empty
miniJoi.requireAndNotEmptyForObj(value)
miniJoi.requireAndNotEmptyForObj(value , {error : new Error("This is an Error")})
Object is required and can be empty
miniJoi.requireAndIsEmptyForObj(value)
miniJoi.requireAndIsEmptyForObj(value , {error : new Error("This is an Error")})
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。