This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

hachiware_validator

1.0.3 • Public • Published

hachiware server

hachiware_validator

GitHub license GitHub package.json version GitHub top language author Nakatsuji Masato made in japan

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 rule value(type) args 1 args 2 Overview
required any - - Required items.
Returns an error if the value is empty or null, etc.
requireIf any Conditionally required items
Required when the value of the specified item reaches the specified value
confirmed any - If the specified item does not contain the same value,
it is judged as an error.
alphaNumeric string 〇※1 - Only single-byte alphanumerical characters are allowed
However, if the character specified by specifying the option value is included, it is allowed.
numeric string 〇※1 - Only half-width numbers are allowed
However, if the character specified by specifying the option value is included, it is allowed.
length string (or number)) - Error judgment if it is not a specified length character string,
minLength string (or number) - Error judgment if less than the specified length character string,
maxLength string (or number) - Error judgment when the specified length is longer than the character string,
betweenLength string (or number) Error judgment if out of range of specified length character string.
value number - If it is not the specified value, error judgment.
minValue number - Error judgment if less than the specified value.
maxValue number - If it is more than the specified value, it is judged as an error.
betweenValue number Error judgment if outside the specified numerical range.
selectedCount Array - Error judgment for a list of numbers other than the specified value.
minSelectedCount Array - Error judgment if the list is less than the specified number
maxSelectedCount Array - Error judgment when the list is more than the specified number.
betweenSelectedCount Array If the list is out of the specified number, error judgment.
like string - Error judgment if the specified character string does not exist.
any string (or number) - Error judgment if the value does not exist in the default value list.
date string - - Error judgment for values other than date format.
minDate string -
minDateToday string 〇※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.
maxDate string - Error judgment if the value is after the specified date.
maxDateToday string 〇※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.
betweenDate string Error judgment if outside the specified date range.
time string - - Error judgment if the value does not appear in the time format.
minTime string - Error judgment if the time is earlier than the specified time.
maxTime string - Error judgment if the time is later than the specified time.
betweenTime string Error judgment if outside the specified time range.
isInt string - - Error judgment if it is not in the form of an integer value.
isBool string - - Error judgment if not Boolean.
isEmail string - - Error judgment if not in email format.
isTel string - - Error judgment if not in phone number format.
isIp string - - Error judgment if not in IP address format.
isUrl string - - Error judgment if not in URL format.
regex string - Error judgment if it is not the specified regular expression.
isJpZip string - - Error judgment if not in Japanese postal code format.
isJpKatakana string 〇※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.
isJpHiragana string 〇※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

Package Sidebar

Install

npm i hachiware_validator

Weekly Downloads

1

Version

1.0.3

License

MIT

Unpacked Size

68 kB

Total Files

9

Last publish

Collaborators

  • nakatsuji_npm19