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

chainable-simple-validator

v1.2.2

Published

A chain-able validator, ideal for using to validate any field, form-data or body of node based api route

Downloads

35

Readme

chainable-simple-validator

A chain-able validator plugin, ideal for using to validate any field, form-data or body of node based api routes.

Getting Started

Installation

// with npm
$ npm install chainable-simple-validator

// with yarn
$ yarn add chainable-simple-validator

Usage

Import

// CommonJS module
const validator = require("chainable-simple-validator");

// ES module
import validator from "chainable-simple-validator";

Application

// Basic type checking
const { value, errors } = validator("First Name").type("string");

// Type checking & when we need exact length
const { value, errors } = validator(01234567890).type("number").exact(11);

// With minimum & maximum boundary
const { value, errors } = validator([1, 2, 3]).type("array").min(3).max(6);

// If it's optional
const { value, errors } = validator(true).nullable().type("boolean");

// If it's an email
const { value, errors } = validator(true).type("string").isEmail();

// Or, pass a custom validator
const { value, errors } = validator(2)
  .type("number")
  .custom((el) => el % 1 === 0, "Must be a whole number");

Full example

const body = {
  name: 'Jhon Smith',
  email: '[email protected]',
  password: 'j123456s',
  termsConditions: true,
  profile: 'www.john-smith.com',
  method: 'get'
}

const { value: name, errors: nErrors } = validator(body.name).type('string')
const { value: email, errors: mailErrors } = validator(body.email).isEmail()
const { value: password, errors: passErrors } = validator(body.password).type('string').min(6).max(10).isAlphaNumeric()
const { value: tc, errors: tcErrors } = validator(body.termsConditions).nullable().type('boolean')
const { value: profile, errors: proErrors } = validator(body.profile).nullable().isURL()
const { value: method, errors: mErrors } = validator(body.method).custom(customValidator) // See below example

  if ([nErrors, mailErrors, pErrors, passErrors, tcErrors, proErrors].some((err) => err.length)) {
    console.log(error: {
        firstName: fErrors,
        email: mailErrors,
        password: passErrors,
        termsConditions: tcErrors,
        profile: proErrors,
        method: mErrors,
      })
  }

Custom Method example

const methods = ['get', 'post', 'put', 'delete']
const customValidator (el) => methods.includes(el.toLowerCase()) // This must return boolean
const { errors } = validator('GET').custom(customValidator)

Methods

| Methods | Description | | :--------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | validator | The main contructor method which accepts an argument of any valid js data type. | | nullable | Only if the value is NOT null or undefind, validators will be applicable. Apply it if the value is optional. Notice the position above, it's important. Always apply it as second method if needed. | | type | It can check any valid JS data type including Array & Object. | | min | Checks if the value has satisfied the minimum length. | | max | Checks whether the value has exceeded the maximum length or not. | | exact | Checks whether the value has matched the exact length or not. | | isEmail | Checks whether it's a valid email or not. | | isAlphaNumeric | Checks if the value contains only alpha-numeric characters. | | isAlphaNumericWithHyphenUnderscore | Checks if the value contains only alpha-numeric characters, hyphen & underscore. | | isURL | Checks whether it's a valid URL or not. | | custom | It is also possible to pass your own validator if needed. This method accepts a handler as first argument & a string (optional) as second argument which will be returned in errors array if the logic not meet. The handler must return a boolean. It is also possbile to chain multiple custom methods. |

Responses

Response is returned as an object which includes the followings:

| Responses | Description | | :-------- | :------------------------------------------------- | | value | The value passed to validator method. | | errors | Errors are retured as an array. See below example. |

Error example

[
  "Type not matched. Expected string, got number",
  "Max length must not exceed 10",
  "Min length must be 6",
  "Length must be 11",
  "This only accepts alpha-numeric value with hyphen & underscore",
  "This is not valid URL",
];

Contributing

Feel free to submit a pull request.

Authors

License

This project is licensed under the MIT License - see the LICENSE file for details