1.0.3 • Published 2 years ago

hachiware_validator v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

hachiware_validator

Input validation for Node.js.


# How do you use this?

First, install the npm package with the following command.

npm i hachiware_validator

All you have to do is add the package require code to index.js etc. and you're ready to go.

const validator = require("hachiware_validator");

Be sure to prepare an initialized object for validation.

const validator = require("hachiware_validator");

var Validate = new validator();

# Input data validation

To validate the input data based on the specified rule, first write the following code.

const validator = require("hachiware_validator");

var Validate = new validator();

var data ={};   // <= Input data

var rules = {
    name: [
        "required",
    ],
    value: [
        "required",
        "maxLength|100",
    ],
};  // <= Validation rules to apply>

var result = Validate.verify(data, rules);  // <= Perform validation check

console.log(result);

In the verify method, set the input data and validation rule settings in the arguments.
The result of validation is returned as a return value, the above code outputs it to the console.

The result of the return value is output including the error message only when the error judgment in each validation occurs.
Therefore, if there is no error judgment (= normal), it will be an empty object.

It is also possible to implement a preset rule name or customized validation as a callback in the rule setting.
See Click here for details on various rules and Click here for customized validation.


# Validation rule settings

Describe the rules given for each column as follows.

First of all, the simplest implementation.

var rules = {
    name: [
        "required",
    ],
    value: [
        "required",
        "maxLength|100",
    ],
};

The item of the object is used as the column name, and the rule name or callback of the preset to be applied in the array value is set to the value.
(In the above case, required and maxLength (100 characters) are associated with each of the name and value items.)

maxlength has a character limit (upper limit), but if you want to hit the rule for the specified value like this, Separate the text with |.

It is also possible to set up customized validation using callbacks instead of preset rule names.
The input value is passed as an argument.

If the return value is true, it is regarded as through, and if it is false, it is regarded as an error.

value2: [
    function(value){

        if(value == 1 || value == 3){
            return true;
        }

        return false;
    },
],

If you want to set strict rules, you can also do the following.

var rules = {
    name: [
        {
            rule:"required",
        },
    ]
    value: [
        {
            rule:"required",
        },
        {
            rule:["maxLength", 100],
        },
    ],
};

In the above case, you can further customize the error judgment message.

var rules = {
    name: [
        {
            rule:"required",
            message: "name not entered",
        },
    ]
    value: [
        {
            rule:"required",
            message: "value is not entered",
        },
        {
            rule:["maxLength", 100],
            message: "Enter within 100 characters",
        },
    ],
};

# List of preset rules

Below is a list of preset rules

  • 1 is optional.
preset rulevalue(type)args 1args 2Overview
requiredany--Required items.Returns an error if the value is empty or null, etc.
requireIfanyConditionally required itemsRequired when the value of the specified item reaches the specified value
confirmedany-If the specified item does not contain the same value, it is judged as an error.
alphaNumericstring〇※1-Only single-byte alphanumerical characters are allowedHowever, if the character specified by specifying the option value is included, it is allowed.
numericstring〇※1-Only half-width numbers are allowedHowever, if the character specified by specifying the option value is included, it is allowed.
lengthstring (or number))-Error judgment if it is not a specified length character string,
minLengthstring (or number)-Error judgment if less than the specified length character string,
maxLengthstring (or number)-Error judgment when the specified length is longer than the character string,
betweenLengthstring (or number)Error judgment if out of range of specified length character string.
valuenumber-If it is not the specified value, error judgment.
minValuenumber-Error judgment if less than the specified value.
maxValuenumber-If it is more than the specified value, it is judged as an error.
betweenValuenumberError judgment if outside the specified numerical range.
selectedCountArray-Error judgment for a list of numbers other than the specified value.
minSelectedCountArray-Error judgment if the list is less than the specified number
maxSelectedCountArray-Error judgment when the list is more than the specified number.
betweenSelectedCountArrayIf the list is out of the specified number, error judgment.
likestring-Error judgment if the specified character string does not exist.
anystring (or number)-Error judgment if the value does not exist in the default value list.
datestring--Error judgment for values other than date format.
minDatestring-Error judgment if the value is earlier than the specified date.
minDateTodaystring〇※1-Error judgment if the value is earlier than the current date.It is also possible to judge by the date n days after the current day with the argument.
maxDatestring-Error judgment if the value is after the specified date.
maxDateTodaystring〇※1-Error judgment when the value is later than the current date.It is also possible to judge by the date n days after the current day with the argument.
betweenDatestringError judgment if outside the specified date range.
timestring--Error judgment if the value does not appear in the time format.
minTimestring-Error judgment if the time is earlier than the specified time.
maxTimestring-Error judgment if the time is later than the specified time.
betweenTimestringError judgment if outside the specified time range.
isIntstring--Error judgment if it is not in the form of an integer value.
isBoolstring--Error judgment if not Boolean.
isEmailstring--Error judgment if not in email format.
isTelstring--Error judgment if not in phone number format.
isIpstring--Error judgment if not in IP address format.
isUrlstring--Error judgment if not in URL format.
regexstring-Error judgment if it is not the specified regular expression.
isJpZipstring--Error judgment if not in Japanese postal code format.
isJpKatakanastring〇※1-Error judgment if not in Japanese full-width katakana format.However, if the character specified by specifying the option value is included, it is allowed.
isJpHiraganastring〇※1-Error judgment if it is not in Japanese full-width hiragana format.However, if the character specified by specifying the option value is included, it is allowed.

- required

Required items.
Returns an error if the value is empty or null, etc.

name:[
    "required",
],

- requireIf

Conditionally required items.
Required when the value of the specified item reaches the specified value

In the following cases, it is required when mode becomes 1.

value1:[
    "requiredIf|mode,1",
],

- confirmed

If the specified item does not contain the same value,
it is judged as an error.

Mainly used for re-entering passwords, etc.

password:[
    "confirmed|password_2",
],

- alphaNumeric

Only single-byte alphanumerical characters are allowed.
However, if the character specified by specifying the option value is included, it is allowed.

First of all, if you normally allow only half-width alphanumerical characters, specify as follows.
No arguments.

username:[
    "alphaNumeric",
],

In the case of this rule, if you use half-width spaces or other special characters (+,-, =, etc.), an error will be judged.

To enable permission even if some special characters are included, list only the permitted characters as arguments as shown below.

username:[
    "alphaNumeric| ,+,-,=",
],

This will allow half-width spaces and special characters (+, -, =).

- numeric

Only single-byte numeric characters are allowed.
However, if the character specified by specifying the option value is included, it is allowed.

First of all, if you normally allow only half-width numeric characters, specify as follows.
No arguments.

tel:[
    "numeric",
],

In the case of this rule, if you use half-width spaces or other special characters (+,-, =, etc.), an error will be judged.

To enable permission even if some special characters are included, list only the permitted characters as arguments as shown below.

tel:[
    "numeric| ,+,-,=",
],

This will allow half-width spaces and special characters (+, -, =).

- length

Judges an error if the string length of the value is not the specified value.
Be sure to set the specified length in the argument.

In the following cases, only a 20-character string is allowed.

code:[
    "length|20",
],

- minLength

If the length of the value string is less than the specified value, an error is determined.
Be sure to set the specified length for the argument.

In the following cases, only strings of 5 or more characters are allowed.

code:[
    "minLength|5",
],

- maxLength

If the length of the value string is long than the specified value, an error is determined.
Be sure to set the specified length for the argument.

In the following cases, only strings of 50 characters or less are allowed.

code:[
    "maxLength|50",
],

- betweenLength

If the length of the value string is outside the specified range, an error is determined.
Be sure to set the specified range (minimum value and maximum value) for the argument.

In the following cases, only strings of 5 to 50 characters are allowed.

code:[
    "betweenLength|5,50",
],

- value

If the value is not the same as the specified value, an error is judged.
Be sure to set the specified value in the argument.

Only 200 numbers (or strings) are allowed in the following cases.

number:[
    "value|200",
],

- minValue

If the value is smaller than the specified value, an error is judged.
Be sure to set the specified value in the argument.

Only numbers (or strings) greater than or equal to 10 are allowed in the following cases

number:[
    "minValue|10",
],

- maxValue

If the value is larger than the specified value, an error is judged.
Be sure to set the specified value in the argument.

Only numbers (or strings) less than or equal to 200 are allowed in the following cases

number:[
    "maxValue|200",
],

- betweenValue

If the value is not within the specified range, an error is judged.
Be sure to set the specified values (minimum value and maximum value) in the argument.

Only numbers (or strings) in the range 10-200 are allowed in the following cases

number:[
    "maxValue|200",
],

- selectedCount

Array of values If the number of values is other than the specified value, an error is judged.
Be sure to set the specified value in the argument.

In the following cases, it is allowed only when the number of array values of values is four.

selects:[
    "selectedCound|4",
],

- minSelectedCount

If the number of values is less than the specified value, an error is judged.
Be sure to set the specified value in the argument.

In the following cases, it is allowed only when the number of array values of values is 3 or more.

selects:[
    "minSelectedCound|3",
],

- maxSelectedCount

If the number of values is greater than the specified value, an error is judged.
Be sure to set the specified value in the argument.

In the following cases, it is allowed only when the number of array values of values is 8 or less.

selects:[
    "maxSelectedCound|8",
],

- betweenSelectedCount

Array of values If the number of values is not within the specified value range, an error is judged.
Be sure to set the specified values (Minimum number and maximum number) in the argument.

In the following cases, it is allowed only when the number of array values of values is between 3 and 8.

selects:[
    "betweenSelectedCount|3,8",
],

^ like

If the specified value is not included in the value, an error is determined.
The value is a string or a number. (Excluding array values)

In the following cases, it is allowed only when the value (string) contains "have to".

memo:{
    "like:have to",
},

- any

If the value is not included in the specified value (list) candidates, an error is judged.
It is used for the purpose of preventing undefined values from being entered in status etc.

In the following cases, allow only if the value is one of "apple, orange, kiwi".

favorite_food:{
    "any:apple,orange,kiwi",
},

- date

Comming Soon...!

- minDate

Comming Soon...!

- minDateToday

Comming Soon...!

- maxDate

Comming Soon...!

- maxDateToday

Comming Soon...!

- betweenTime

Comming Soon...!

- betweenDate

Comming Soon...!

- time

Comming Soon...!

- minTime

Comming Soon...!

- maxTime

Comming Soon...!

- betweenTime

Comming Soon...!

- isInt

Comming Soon...!

- isBool

Comming Soon...!

- isEmail

Comming Soon...!

- isTel

Comming Soon...!

- isIp

Comming Soon...!

- isUrl

Comming Soon...!

- regex

Comming Soon...!

- isJpZip

Comming Soon...!

- isJpKatakana

Comming Soon...!

- isJpHiragana

Comming Soon...!


# Passing the input data of the rule setting value by reference

Apart from setting the rule setting value fixedly, it can also be changed dynamically by referencing the input data.

To refer to the input data value, prefix it with @ and specify the column name where the input data is located.

For example, if you specify as below, the value of nameMaxLen will be the set value of maxLength on the input data.

name: [
    "maxLength|@nameMaxLen",
],

This pass by reference is available for almost all preset rules.


hachiware_validator

License : MIT License.
Author : Nakatsuji Masato
HP URL : https://hachiware-js.com/
GitHub : https://github.com/masatonakatsuji2021/hachiware_validator
npm : https://www.npmjs.com/package/hachiware_validator