1.1.1 • Published 1 year ago

validate_l v1.1.1

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Validate_L

validation package for express framework inspired from Laravel. the main reason for it, is to simplify the way validation rules are written.

Writing The Validation Logic

validation logic can be written anywhere as long as req and res objects are provided with the fields object.

const {validate} = require('validte_l');

async (req, res, next) => {
    let response = await validate(req, res, {
        name: ['required', 'string', 'max:255'],
        email: ['required', 'email'],
        birthdate: ['required', 'date']
    });
}

Warning
req should contain a "body" which should contain the request body.

Working With Error Messages

Custom Messages For Specific Attributes

You may customize the error messages used for specified attribute and rule

let fields = {
    name: ['required', 'string', 'max:255'],
    email: ['required', 'email'],
    birthdate: ['required', 'date']
};

let customMessages = {
    'name.required': ':item need to be existed in the request to register the person.'
}

let response = await validate(req, res, fields, customMessages);

now the message for the name will be changed if the name attribute in not found in the request body.

:item will be replaced with field name, so the message will be "name need to be existed in the request to register the person".

Available Validation Rules

After (Date) Alpha Alpha Numeric Array Before (Date) Boolean Confirmed Date Email Ends With In Max Min Missing Missing If Missing Unless Missing With Missing With All Not In Nullable Number Regular Expression Required Required If Required Unless Required With Required With All Required Without Required Without All Starts With URL

after:date

The field under validation must be a value after a given date. instance:

'start_date': ['required', date', after:2022/01/01']

alpha

The field under validation must be entirely Unicode alphabetic characters contained in \p{L} and \p{M}.

'username': ['alpha']

alpha_num

The field under validation must be entirely Unicode alpha-numeric characters contained in \p{L}, \p{M}, and \p{N}.

'username': ['alpha_num']

array

The field under validation must be a VALID array.

'user': ['array']

before:date

The field under validation must be a value preceding the given date.

'birthdate': ['date', 'before:2000/01/01']

boolean

The field under validation must be able to be cast as a boolean. Accepted input are true, false, 1, 0, "1", and "0".

'accepted': ['boolean']

confirmed

The field under validation must have a matching field of {field}_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.

'password': ['confirmed']

date

The field under validation must be a valid Date

'birthdate': ['date']

email

The field under validation must be formatted as an email address.

'email': ['email']

endswith:_foo,bar,...

The field under validation must end with one of the given values.

'email': ['ends_with:.com,.net']

in:foo,bar,...

The field under validation must be included in the given list of values.

'user_type': ['in:admin,supervisor']

max:value

The field under validation must be less than or equal to a maximum value. Strings, numerics and array.

'username': ['required', 'string' 'max:20'] // username should not exeed 20 chars as length
'age': ['required', 'numeric', 'max:20'] // age should not be grater than 20

min:value

The field under validation must have a minimum value. Strings, numerics and arrays.

'username': ['required', 'string' 'min:20'] // username should not be lss than 20 chars as length
'age': ['required', 'numeric', 'min:20'] // age should not be less than 20

missing

The field under validation must not be present in the input data.

'age': ['missing']

missingif:_anotherfield,value,...

The field under validation must not be present if the anotherfield field is equal to any value.

'age': ['missing_if:birthdate']

missingunless:_anotherfield,value

The field under validation must not be present unless the anotherfield field is equal to any value.

'age': ['missing_unless:name']

missingwith:_foo,bar,...

The field under validation must not be present only if any of the other specified fields are present.

'age': ['missing_with:name,birthdate']

missingwith_all:_foo,bar,...

The field under validation must not be present only if all of the other specified fields are present.

'age': ['missing_with_all:name,birthdate']

notin:_foo,bar,...

The field under validation must not be included in the given list of values.

'user_type': ['not_in:admin,supervisor']

nullable

The field under validation may be null or not existed in the request body.

'description': ['nullable']

number

The field under validation must be a valid number.

'age': ['required', 'number', 'min:20'] 

regex:pattern

The field under validation must match the given regular expression.

'formula': ['required', 'regex:[1-9]'] 

required

The field under validation must be present in the input data and not empty.

'age': ['required']

requiredif:_anotherfield,value,...

The field under validation must be present and not empty if the anotherfield field is equal to any value.

// age is required if name field is existed and not empty
'age': ['required_if:name']

// age is required if name field is existed and has value of 'jane'
'age': ['required_if:name,jane']

requiredunless:_anotherfield,value,...

The field under validation must be present and not empty unless the anotherfield field is equal to any value. This also means anotherfield must be present in the request data unless value is null. If value is null (required_unless:name,null), the field under validation will be required unless the comparison field is null or the comparison field is missing from the request data.

// age is required unless name field is existed and not empty
'age': ['required_if:name']

// age is required unless name field is existed and has value of 'jane'
'age': ['required_if:name,jane']

requiredwith:_foo,bar,...

The field under validation must be present and not empty only if any of the other specified fields are present and not empty.

'age': ['required_if:name,email']

requiredwith_all:_foo,bar,...

The field under validation must be present and not empty only if all of the other specified fields are present and not empty.

'age': ['required_if:name,email,password']

requiredwithout:_foo,bar,...

The field under validation must be present and not empty only when any of the other specified fields are empty or not present.

age': ['required_if:birthdate']

requiredwithout_all:_foo,bar,...

The field under validation must be present and not empty only when all of the other specified fields are empty or not present.

age': ['required_if:birthdate,year_of_birth']

startswith:_foo,bar,...

The field under validation must start with one of the given values.

'url': ['strats_with:www,api']

string

The field under validation must be a string.

'username': ['required', 'string']

url

The field under validation must be a valid URL.

'url': ['url', strats_with:www,api']
1.1.1

1 year ago

1.1.0

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago