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

valit

v2.0.1

Published

An optimized version of Joi. A very simple input validation by creating a schema and comparing it to the given data.

Downloads

4

Readme

Valit

an optimized version of Joi, if all what you need is to check data types, min and max length, and a bunch of others then you don't need a 70KB library! Valit will be perfect for you.

install

npm i valit

import valit from valit
//or
const valit = require('valit')

Usage

1. First create your schema

const schema = {
    username: valit.createField(valit.string, {
        required: true,
        min: 5,
        max: 25,
        label: 'Username'
    }),
    email: valit.createField(valit.string, {
        required: true,
        email: true
    }),
    age: valit.createField(valit.number, {
        min: 10,
        max: 100,
    }),
    password1: valit.createField(valit.string, {
        required: true,
        match: 'password2',
    }),
    password2: valit.createField(valit.string, { required: true }),
};

the first parameter in valit.createField is the data type of that field. required

  • valit.string valit.number valit.boolean valit.object valit.function

the second parameter is an object of extra options

  • required min max label email match

Note: required and email default value is false

2. Get the data you want to validate

const data = {
    email: '[email protected]',
    age: -17,
    password1: 1234,
    password2: 'myPassword'
};

3. Validate and get errors

const errors = valit.validate(data, schema)

valit.validate will return an object of all errors found, the key will be the name of the field and the value will be the first error found for that field.

So in this example errors will look like this

{
  username: 'Username is required',
  age: 'Age must be at least 10',
  password1: 'Password1 must be a string'
}

There is a third parameter for valit.validate: returnAllErrors type: boolean, default: false.

if set to true it will return all errors found for that field in an array.

so if we did

const errors = valit.validate(data, schema, true)

The errors will look like this:

{
  username: [ 'Username is required' ],
  age: [ 'Age must be at least 10' ],
  password1: [ 'Password1 must be a string', 'Password1 must match password2' ]
}

Resources

If you have any ideas to improve the app or this documentation please open an issue on the github page.