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

evalidate

v1.0.6

Published

An easy to use Object Schema Validator

Downloads

475

Readme

evalidate

CircleCI Known Vulnerabilities codecov codebeat badge HitCount npm version Maintenance Contributions welcome Gitter MIT license

A fast and easy to use Object Schema Validator

Index

Install


npm install --save evalidate

Usage

Importing

// Using require()
const evalidate = require("evalidate");

// Using ES6 import
import evalidate from "evalidate"

Defining a Schema

let schema = new evalidate.schema({
    name: evalidate.string(),
    email: evalidate.string().required().email(),
    age: evalidate.number().min(18).max(150),
    status: evalidate.string().required().in(["Active", "Deacive"]),
    address: evalidate.object({
        city: evalidate.string(),
        country: evalidate.string().required(),
    }),
    birthday: evalidate.date().required().before(new Date())
});

Custom Error Message

let schema = new evalidate.schema({
    email: evalidate.string().required("email is required!").email("Please enter a valid email address!"),
    age: evalidate.number().min(18, "Too young!").max(150, "Too old!"),
});

Validating Data

let result = schema.validate({
    name: "xxxx-xxxx",
    email: "[email protected]",
    age: 18,
    status: "Active",
    address: {
        city: "xxxx",
        country: "xxxx"
    },
    birthday: "2019-01-01"
});

if (!result.isValid) {
    console.log(result.errors);
}

Validators

Array: evalidate.array()

  • contains(value, message)

    • must contain value
    • (Optional) Custom error message
  • equals(value, message)

    • must have the same content as value
    • value must be an array
    • (Optional) Custom error message
  • required(message)

    • (Optional) Custom error message
  • size(value, message)

    • Checks the number of items in the array
    • (Optional) Custom error message

Boolean: evalidate.boolean()

  • equals(value, message)

    • must be the same as value
    • value must be boolean
    • (Optional) Custom error message
  • required(message)

    • (Optional) Custom error message

Date: evalidate.date()

  • after(value, message)

    • must be after value
    • value can be a date object, an integer value representing the number of milliseconds since January 1, 1970 or string value representing a date, specified in a format recognized by the Date.parse() method.
    • (Optional) Custom error message
  • before(value, message)

    • must be before value
    • value can be a date object, an integer value representing the number of milliseconds since January 1, 1970 or string value representing a date, specified in a format recognized by the Date.parse() method.
    • (Optional) Custom error message
  • equals(value, message)

    • must be the same as value
    • value can be a date object, an integer value representing the number of milliseconds since January 1, 1970 or string value representing a date, specified in a format recognized by the Date.parse() method.
    • (Optional) Custom error message
  • required(message)

    • (Optional) Custom error message

Number: evalidate.number()

  • equals(value, message)

    • must be the same as value
    • value must be a number
    • (Optional) Custom error message
  • integer(message)

    • must be an integer
    • (Optional) Custom error message
  • latitude(message)

    • must be a valid latitude (Between -90 and 90)
    • (Optional) Custom error message
  • longitude(message)

    • must be a valid longitude (Between -180 and 180)
    • (Optional) Custom error message
  • min(value, message)

    • must be greater than or equal to value
    • value must be a number
    • (Optional) Custom error message
  • max(value, message)

    • must be less than or equal to value
    • value must be a number
    • (Optional) Custom error message
  • required(message)

    • (Optional) Custom error message

String: evalidate.string()

  • alphanumeric(message)

    • must contain only alphanumeric characters
    • (Optional) Custom error message
  • credit_card(message)

    • must be a valid credit card number
    • (Optional) Custom error message
  • email(message)

    • must be a valid email address
    • (Optional) Custom error message
  • equals(value, message)

    • must be equalt to value
    • value must be a string
    • (Optional) Custom error message
  • fqdn(message)

    • must be a valid fully qualified domain name
    • (Optional) Custom error message
  • in(value, message)

    • must be in value
    • value must be an array
    • (Optional) Custom error message
  • isbn(message)

    • must be a valid ISBN
    • (Optional) Custom error message
  • maxlength(value, message)

    • number of characters in the input string must be less than or equal to value
    • value must be a number
    • (Optional) Custom error message
  • milength(value, message)

    • number of characters in the input string must be greater than or equal to value
    • value must be a number
    • (Optional) Custom error message
  • numeric(message)

    • must contain only numeric characters
    • (Optional) Custom error message
  • required(message)

    • (Optional) Custom error message
  • url(message)

    • must be a valid url
    • (Optional) Custom error message

License

MIT © aderabiruk