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

valerian

v0.2.0

Published

Easy Full Featured JS Validation Library.

Downloads

5

Readme

Valerian: JS Validation Library · npm version Build Status GitHub license

Usage

import 'valerian/bootstrap'; // must be done once

import { Email, Integer, IsString } from 'valerian/rules';
import Validator from 'valerian';

// 1. Get your data
const data = { name: 'Tony', age: 'Tony', email: 'not an email' };

// 2. Set some rules
const rules = {
  name: ['string'], // rules can use string constructors
  age: [(new Integer()).between(18, 100)], // or class constructors
  email: ['required', 'string|min:3', 'email'],
};

// 3. Make a validator
const val = new Validator(data, rules);

// 4. Test your data!
val.passes(); // false
val.fails(); // true

// 5. Show some error messages

val.error.first('age') // 'The age field must be somewhere between 18 and 100.'
val.error.first('name') // null
val.error.all('age') // ['The age field must be somewhere between 18 and 100.']
val.error.all('name') // null
val.error.all()
// {
//   age: ['The age field must be somewhere between 18 and 100.'],
//   email: ['The email field must be a valid email address.'],
// }

val.error.hasMessages('name') // false
val.error.hasMessages('age') // true

// To throw an exception:
val.validate(true); // throws ValidationError - it has all the methods available to val.error (e.first('age'))

Rules

The following rules are available

Numeric

import { Numeric } from 'valerian/rules';

// Both numeric and number resolve to this
const rules = { number: ['numeric', 'number']};

new Numeric();

(new Numeric()).min(1).max(5);
const rules = { number: ['number|min:1']};

(new Numeric()).between(1, 5);
const rules = { number: ['number|between:1-5']};

Integer

import { Integer } from 'valerian/rules';

const rules = { number: ['integer', 'int']};

new Integer();

(new Integer()).min(1).max(5);
const rules = { number: ['integer|min:1']};

(new Integer()).between(1, 5);
const rules = { number: ['integer|between:1-5']};

IsString

import { IsString } from 'valerian/rules';

const rules = { value: ['is_string', 'string'] };

new IsString(); // defaults to .min(1)

(new IsString()).min(10).max(50); // set the required string lengths
const rules = { value: ['string|min:10'] };

(new IsString()).between(10, 50); // shorthand for both
const rules = { value: ['string|between:10-50'] };

(new IsString()).emptiable(); // sets min length to null

Email

By default, uses the JS regex from http://emailregex.com/

import { Email } from 'valerian/rules';

const rules = { value: ['email'] };

new Email();

(new Email).useStrict(false); // do a weak email check - uses /\S+@\S+/

Url

Must be a valid URL. Very naive regex at the moment.

import { Url } from 'valerian/rules';

const rules = { value: ['url'] };

new Url();

Matches

This field must match the field passed in here. For example: name: [new String(), new Matches('name_check')] - the name and name_check fields must match

import { Matches } from 'valerian/rules';

const rules = { value: ['matches|field_name'] };

new Matches('name');

Confirmed

The field must match a field with its own name with _confirmation appended. For example if you used it on, password: [new Confirmed()], there must be a password and password_confirmation field, and they must match each other.

import { Confirmed } from 'valerian/rules';

const rules = { value: ['confirmed'] };

new Confirmed();

TypeOf

Checks against the typeof operator

import { TypeOf } from 'valerian/rules';

const rules = { value: ['typeof|string'] };

new TypeOf('string');

InstanceOf

You must provide a class to check against

import { InstanceOf } from 'valerian/rules';

const rules = { value: ['instance_of|classname'] };

new InstanceOf(Date);

IsDateString

Checks that it is a valid date with Date.parse()

import { IsDateString } from 'valerian/rules';

const rules = { value: ['is_date_string', 'date_string'] };

new IsDateString();

OneOf

Must be one of the supplied values

import { OneOf } from 'valerian/rules';

const rules = { value: ['one_of:1,2,3', 'in:3,4,5'] };

new OneOf(['apple', 'banana']);

Optional

Marks a field as optional. This gets added automatically if you don't have any "required" rules in a rule set.

import { Optional } from 'valerian/rules';

const rules = { value: ['optional'] };

new Optional();

Required

import { Required } from 'valerian/rules';

const rules = { value: ['required'] };

new Required();

RequiredWith

import { RequiredWith } from 'valerian/rules';

const rules = { value: ['required_with:fieldname'] };

new RequiredWith('other_field');

RequiredWithout

import { RequiredWithout } from 'valerian/rules';

const rules = { value: ['required_without'] };

new RequiredWithout('other_field');

Using your own strings

import 'valerian/bootstrap';

import { setStrings, mergeStrings } from 'valerian';

// This will add any new strings, overriding old ones where necessary.
mergeStrings({
    'email': 'The :name field must be a valid email address.',
});

// This will remove all current strings, and use this as the only strings list
setStrings({
    'email': 'The :name field must be a valid email address.',
});

If a string translation is not found, the key will be displayed instead:

val.error.message('age'); // 'integer/between'
val.error.message('email'); // 'email'

Custom Rules

Rules are simple JS Classes. The must have two methods as a minimum:

class IsMonkey {
  validate(value, field, validator) {
    // value is the value to validate. Most of the time, you will only need this.
    // field is the field name that is being validated.
    // validator is the validator class that contains all the data and rules under validation.

    return value === 'monkey';
  }

  error() {
    // The key used to get the error message.
    // Note that if the key is not found in the available strings, this key will 
    // be displayed, so if you are not bothered about using the strings functionality,
    // you can just put your error message here.

    return 'monkey';
  }
}

mergeStrings({monkey: ':name is not a monkey.'});

new Validator({}, {monkey: [new IsMonkey()]});

Other

This approach to validation is STRONGLY influenced by Laravel's validation library for PHP.