A node.js parameter verification module – minijoi is recommended
Purpose:
With Joi, the schema rules string.trim().required() and so on are written every time the validation is done. Because parameter validation is frequent and necessary, more and more of it is written, the code is neither pretty nor easy to maintain, the schema rules are hard to remember, and joi throw errors need to be handled separately. Therefore, the most commonly used daily check, encapsulated JOI API, and can be called at the time of passing in a custom Error, using joI friendly Error stack prompt message at the same time throw our custom Error, no longer need to deal with joI Error. So you have miniJoi, which is a simpler version of JOi.
Welcome to Issue and PR. In the Tests directory for the code, write the test case and run the following command:
PNPM Run coverage(recommended) NPM run coverage Yarn run coverageCopy the code
The console outputs the use case situation and code coverage. Developers can also re-develop miniJoi to fit their own application modules.
minijoi
const miniJoi = require('minijoi'); MiniJoi. RequireAndNotEmptyForStr (the value of the options) options: {error: new error (" This is an error ") / / public pattern: /^1[3456789]\d{9}$/ // email ID URL phone name mode : 'strict' //phone version : 'ipv6' //IP generation : 'first' //ID }Copy the code
This is a friendly stack prompt that tells the developer the type of the parameter to be verified based on Joi. The developer can clearly see the input value and type. The value of _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/[email protected] / node_modules/joi \ lib \ errors js: 184:16) at the Object. The internals. The entry (D: \ data \ git \ minijoi/node_modules/PNPM/[email protected] / node_modules/joi \ lib \ validator js: 150:26) at the Object. Exports. The entry (D: \ data \ git \ minijoi/node_modules/PNPM/[email protected] / node_modules/joi \ lib \ validator js: 27:30) at internals. Base. Validate (D: \ data \ git \ minijoi/node_modules/PNPM/[email protected] / 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: '}}]}Copy the code
Node.js version requirements:
Support node.js V10,V12, V14, V16
The API is as follows:
MiniJoi will automatically throw a developer-defined Error. By default, the Error message above will be output.
The string is mandatory and not empty
miniJoi.requireAndNotEmptyForStr(value)
miniJoi.requireAndNotEmptyForStr(value , {error : new Error("This is an Error")})
Copy the code
The string must be empty
miniJoi.requireAndIsEmptyForStr(value)
miniJoi.requireAndIsEmptyForStr(value , {error : new Error("This is an Error")})
Copy the code
Mandatory string enumeration
miniJoi.requireAndEnumForStr(value , ["test"])
miniJoi.requireAndEnumForStr(value , ["test","test1"] , {error : new Error("This is an Error")})
Copy the code
Mandatory String of a specific length
miniJoi.length(value , limit)
miniJoi.length(value , limit , {error : new Error("This is an Error")})
Copy the code
Mandatory Maximum length of a string
miniJoi.max(value , limit)
miniJoi.max(value , limit , {error : new Error("This is an Error")})
Copy the code
Minimum length of a mandatory string
miniJoi.min(value , limit)
miniJoi.min(value , limit , {error : new Error("This is an Error")})
Copy the code
This parameter is mandatory. Chinese name
miniJoi.name(value ) miniJoi.name(value , {error : new Error("This is an Error")}) miniJoi.name(value , {pattern : XXXX}) if the Chinese name verification function provided by miniJoi does not meet the requirements of the developer, the developer can customize the regular expressionCopy the code
The value is a mandatory string and is a valid email address
miniJoi.requireEmail(value ) miniJoi.requireEmail(value , {error : New Error("This is an Error")}) use pattern minijoi.requireemail (value, {Error: New Error("This is an Error"), pattern: /^1[3456789]\d{9}$/}) If the mailbox verification function provided by miniJoi does not meet the requirements of the developer, the developer can customize the regular expressionCopy the code
The value is a mandatory string and valid phone number. For details about the mode field, see anyrule
miniJoi.requirePhone(value ) miniJoi.requirePhone(value , {error : New Error("This is an Error")}) Use the pattern mode 'strict' | | 'loose' strict said rigorous said loose loose defaults to () the most lenient, as long as it is 1 can miniJoi beginning. RequirePhone (value, {the error: new Error("This is an Error"), pattern : /^1[3456789]\d{9}$/ mode : 'strict'}) if the phone number verification function provided by miniJoi does not meet the requirements of the developer, the developer can customize the passed regular expressionCopy the code
The value is a mandatory string and a valid IP address
miniJoi.requireIP(value ) miniJoi.requireIP(value , {error : New Error("This is an Error")}) Is using the pattern version 'ipv4' | | 'ipv6 ipv4 said check only ipv4 ipv6 said only check ipv6 default at the same time check ipv4 and ipv6 miniJoi. RequireIP (value, {the error: New Error("This is an Error"), version: 'ipv6'}) If the IP verification function provided by miniJoi does not meet the requirements of the developer, the developer can customize the passed regular expressionCopy the code
A mandatory string and a valid Url
miniJoi.requireUrl(value ) miniJoi.requireUrl(value , {error : New Error("This is an Error")}) use pattern minijoi. requireUrl(value, {Error: New Error("This is an Error"), pattern: /^1[3456789]\d{9}$/}) If the Url verification function provided by miniJoi does not meet the requirements of the developer, the developer can customize the input regular expressionCopy the code
The value is a character string with a valid ID
miniJoi.requireID(value ) miniJoi.requireID(value , {error : New Error("This is an Error")}) "First" | | "second" first said check only the second generation of id said check only the second generation id card The default calibration generation and second generation at the same time miniJoi. RequireID (value, {the error: new Error("This is an Error"), pattern : /^1[3456789]\d{9}$/ generation : If the id verification function provided by miniJoi does not meet the requirements of the developer, the developer can customize the passed regular expressionCopy the code
Required number
miniJoi.requireForNum(value)
miniJoi.requireForNum(value , {error : new Error("This is an Error")})
Copy the code
Mandatory integer
miniJoi.requireForInt(value)
miniJoi.requireForInt(value , {error : new Error("This is an Error")})
Copy the code
Mandatory numeric enumeration
MiniJoi. RequireAndEnumForNum (value) miniJoi. RequireAndEnumForNum (value, [1, 2], {error: new error (" This is an error ")})Copy the code
The required number of decimal places cannot exceed the limit bit
miniJoi.requireAndPrecision(value) miniJoi.requireAndPrecision(value , limit , {error : New Error("This is an Error")}) 2.22 PASS 2.222 FAILCopy the code
Mandatory numeric range API
miniJoi.requireForRangeNum(value ,op, limit ) miniJoi.requireForRangeNum(value ,op, limit , {error : New Error (" This is an Error ")}) op has a value of gt (>) | | gte (> =) | | lt (<) | | lte (< =) such as need parameter > 0, The miniJoi. RequireForRangeNum (value, "gt", 0) represents a positive number > = 0 miniJoi. The parameters can be requireForRangeNum (value, "gte", < = 0 miniJoi. 0) parameter requireForRangeNum (value, "lte, 0) parameter < 0 miniJoi. RequireForRangeNum (value," lt ", 0) represents the negative required number range 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-o-r-o left-open-right-open '(0,100)' l-o-r-o left-open-right-close '(0,100)' l-o-r-c The miniJoi. RequireForRangeNum (value, "left - open - right - close", [0100]) miniJoi. RequireForRangeNum (value, "l - o - r - c", [0100]), such as need parameter > = 0 and the < = 100, miniJoi. RequireForRangeNum (value, "left - close - right - close", [0100]) miniJoi. RequireForRangeNum (value, "l - c - r - c", [0100]), such as need parameter 0 > and < 100, The miniJoi. RequireForRangeNum (value, "left - open - right - open", [0100]) miniJoi. RequireForRangeNum (value, "l - o -r -o", [0100]), such as need parameter > = 0 and < 100, then miniJoi requireForRangeNum (value, "left - close - right - open", [0100]) miniJoi. RequireForRangeNum (value, "l - c - r - o," [0100])Copy the code
Mandatory integer range API
miniJoi.requireForRangeInt(value ,op, limit ) miniJoi.requireForRangeInt(value ,op, limit , {error : New Error (" This is an Error ")}) op has a value of gt (>) | | gte (> =) | | lt (<) | | lte (< =) such as need parameter > 0, The miniJoi. RequireForRangeInt (value, "gt", 0) on behalf of the positive integer > = 0 miniJoi. The parameters can be requireForRangeInt (value, "gte", < = 0 miniJoi. 0) parameter requireForRangeInt (value, "lte, 0) parameter < 0 miniJoi. RequireForRangeInt (value," lt ", 0) represents the negative integer required integer range API miniJoi. RequireForRangeInt (value, op, rangeArr) miniJoi. RequireForRangeInt (value, op, rangeArr, {error : new Error("This is an Error")})Copy the code
Mandatory Boolean
miniJoi.requireForBool(value)
miniJoi.requireForBool(value , {error : new Error("This is an Error")})
Copy the code
Array is mandatory and non-empty
miniJoi.requireAndNotEmptyForArr(value)
miniJoi.requireAndNotEmptyForArr(value , {error : new Error("This is an Error")})
Copy the code
The array must be empty
miniJoi.requireAndIsEmptyForArr(value)
miniJoi.requireAndIsEmptyForArr(value , {error : new Error("This is an Error")})
Copy the code
Object Is mandatory and not empty
miniJoi.requireAndNotEmptyForObj(value)
miniJoi.requireAndNotEmptyForObj(value , {error : new Error("This is an Error")})
Copy the code
Object Mandatory can be empty
miniJoi.requireAndIsEmptyForObj(value)
miniJoi.requireAndIsEmptyForObj(value , {error : new Error("This is an Error")})
Copy the code