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

v-rule

v1.0.9

Published

Light and flexible validatin rules for form

Downloads

69

Readme

v-rule

Light and flexible validation rules for form

Advantages:

  • Limited API, save you from complicated configuration
  • Quite flexible, able to compose any validation rule
  • Small library with no dependence

Core API

  • when(key: string, assert?: func)
  • whenNot(key: string, assert?: func)
  • expect(desc: string, assert?: func)
  • validate(key: string)

Install

npm i -S v-rule

Example - Order beer in restaurant

1

import v from 'v-rule'

const validation = v.create({
    // alternative v.expect('This is required')
    seat: v.expect('This is required', c => c['seat'] !== '')
})
const result = validation.test({ seat: '' })
// => { pass: false, messages: { seat: 'This is required' } }

2

import v from 'v-rule'

const validation = v.create({
    seat: v.expect('This is required', c => c['seat'] !== ''),
    age: v.when('drink', c => c['drink'] === 'budweiser')
    .expect('Required when drink beer')
})
const result = validation.test({ seat: '6', drink: 'budweiser', age: '' })
// => { pass: false, messages: { age: 'Required when drink beer' } }

3

import v from 'v-rule'

const validation = v.create({
    seat: v.expect('This is required', c => c['seat'] !== ''),
    age: v.when('drink', c => c['drink'] === 'budweiser')
    .expect('Required when drink beer')
    .expect('You should be older than 18', c => c['age'] > 17)
})
const result = validation.test({ seat: '6', drink: 'budweiser', age: 16 })
// => { pass: false, messages: { age: 'You should be older than 18' } }

4

import v from 'v-rule'

const validation = v.create({
    seat: v.expect('This is required', c => c['seat'] !== ''),
    age: v.when('drink', c => c['drink'] === 'budweiser')
    .expect('Required when drink beer')
    .expect('You should be older than 18', c => c['age'] > 17)
})
const result = validation.test({ seat: '6', drink: 'budweiser', age: 18 })
// => { pass: true, messages: {} }

Usage

Preset

import { preset } from 'v-rule'

const v = preset({
    required: (expect) => expect(`required`),
    min: (expect, n) => expect(`> ${n}`, c => c['$0'] > n),
    max: (expect, m) => expect(`> ${m}`, c => c['$0'] < m),
})
const validation = v.create({
    number: v.required().min(4).max(30)
})

Multi rules

const validation = v.create({
    age: [
        v.expect('required')
        .expect('should be number', c => v.isInteger(c.age)), // 1st rule
        v.when('min') // 2nd rule, only execute when 1st rule pass
        .when('max')
        .expect(
            'should between min and max',
            c => c.min < c.age && c.age < c.max
        )
    ]
})

const result = validation.test({ age: '17', min: 18, max: 50 })
// => r { pass: false, messages: { age: 'should between min and max' } }

Trigger another rule

const validation = v.create({
    beer: v.when('beer').validate('age'), // or v.validate('age')
    age: v.expect('age should be greater than 18', c => c.age > 18)
})

const context = {age: '17'}
const result = validation.test({ beer: 1 }, context)
// => { pass: false, messages: { age: 'age should be greater than 18' } }

Test all rules

v.test only tests with available values in obj whereas testAllRules will default unavailable value to {key: undefined} and test against all rules. Use v.testAllRules when submit form.

const validation = v.create({
    name: v.expect('required'),
    pwd: v.expect('required')
})
let r = validation.testAllRules({ name: 'a' })
// => { pass: false, messages: { name: '', pwd: 'required' } }