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

ya-validator

v0.1.2

Published

Yet another validator

Downloads

4

Readme

ya-validator === Yet Another Validator

What is the difference?

Not really much. Common validation flow involves config a validator and then validate data. By add some functional flavour, ya-validator make config and customization easier.

Create validate function

Basic Usage

import Validate from 'ya-validator';
import MustBeEmail from 'ya-validator/lib/mustBeEmail';

// Created a validate function to validate data's email field.
const validate = Validate({email: MustBeEmail()});
const data = {email:''}

// Once validated data, errors contains error messages of a invalid field
const errors = validate(data);

// errors should be
// {email: ['not email']}

Validate field with multiple validators

A field can be validated by multiple validator functions in sequence.

import Validate from 'ya-validator';
import MustBeEmail from 'ya-validator/lib/mustBeEmail';
import MustPresent from 'ya-validator/lib/mustPresent';

const required = MustPresent();
const email = MustBeEmail();

const errors = Validate({email: [required(), email]})({email:''});

// errors should be
// {email: ['required', 'not email']}

Validator can be chained

In many cases we don't need run through all the validator functions if one is failed. For example, if a email field is required and must be in correct email format. We don't need to validate the email format if the field is blank. Fortunately, the validator is just a function and it can be chained up.

The build in MustBePresent validator is doing so. It will ignore chained validator if data field is blank.

import Validate from 'ya-validator';
import MustBeEmail from 'ya-validator/lib/mustBeEmail';
import MustPresent from 'ya-validator/lib/mustPresent';

const required = MustPresent();
const email = MustBeEmail();
const validate = Validate({email: required(email)})

let errors = validate({email:''});
// errors should be
// {email: ['required']}

errors = validate({email:'invalid'});
// errors should be
// {email: ['not email']}

Customize error message

A validator is just a simple function. The build in validator builder can be customized to fit more generic usecases.

For example

import Validate from 'ya-validator';
import MustPresent from 'ya-validator/lib/mustPresent';
import MustBeEmail from 'ya-validator/lib/mustBeEmail';
import MustMatchField from 'ya-validator/lib/mustMatchField';

const required = MustPresent({error: 'can not be blank'});
const email = MustBeEmail({error: (field)=>`${field} is not an email address`});
const match = MustMatchField({field: 'password', error: (field, other)=>`${field} must match ${other}`});

const validate = Validate({
  email: required(email),
  password: required(),
  passwordMatch: required(match)
})

const errors = validate({
  email: 'incorret email',
  password: '',
  passwordMatch: 'not match',
})

// errors should be
// {
//   email: ['email is not an email address'],
//   password: ['can not be blank'],
//   passwordMatch: ['passwordMatch must match password']
// }

Create validator

Validator is just a function that check the data field. For example the internal MustBeEmail validator builder is very simple.

import isEmail from 'validator/lib/isEmail';

export default (opts={error: 'not email'})=> (data, field, check)=>{
  check(isEmail(data[field]))(opts.error)
}

It created an email validator that will check if the email is valid. The third argument check is a memo function that will resolve error message to the final result if check is failed.