npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

validate_l

v1.1.1

Published

Lite weight validation package inspired from laravel validation.

Downloads

5

Readme

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']

ends_with: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']

missing_if:anotherfield,value,...

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

'age': ['missing_if:birthdate']

missing_unless:anotherfield,value

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

'age': ['missing_unless:name']

missing_with: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']

missing_with_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']

not_in: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']

required_if: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']

required_unless: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']

required_with: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']

required_with_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']

required_without: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']

required_without_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']

starts_with: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']