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

@killara/validation

v2.2.4

Published

A validator for HTTP request parameters.

Downloads

9

Readme

validation

A validator for HTTP request parameters.

npm Travis branch Codecov branch David deps Known Vulnerabilities npm download

Installation

npm i @killara/validation -S

Features

  • It supports three kinds of rule declarations. String inline rules, object rules and array rules.
  • It supports async validation rule. For example, unique validation for only specific record in database table.
  • It supports custom rules
  • It supports custom error messages, or it uses default messages.

Usage

const Validation = require('@killara/validation');
const validation = new Validation();

const values = {
  username: 'admins',
  password: 'abcdef',
  sex: 'male',
};

const rules = {
  username: 'required|alpha:6',
  password: {
    required: true,
    regexp: /^[a-z]{6,18}$/,
  },
  sex: [ 'male', 'female' ],
};

const messages = {
  'username.alpha': 'The field must be entirely alphabetic characters with the length of ${len}'
  'sex.in': 'The field should be included in the list of ${ _items_.join(", ") }'
};

const errors = await validation.validate(values, rules, messages);

if (!errors) {
  // all validations passed
} else {
  // we got an array of errors
}

Rule

  • accepted

    • string style: field: 'accepted'
    • object style: field: { accepted: true }
  • alpha

    • string style: field: 'alpha:6' or field: 'alpha:len=6'
    • object style: field: { alpha: { len: 6 } }
  • alphanum

    • string style: field: 'alphanum:6' or field: 'alphanum:len=6'
    • object style: field: { alphanum: { len: 6 } }
  • confirmed

    Rule confirmed: the field need to have the same value as the value that be filled by field_confirmed. We can change field_confirmed to any names with confirmed:"custom"

    • string style: field: 'confirmed' or field: 'confirmed"custom_field_name"'
    • object style: field: { confirmed: { len: 6 } }
  • date

    • string style: field: 'date'
    • object style: field: { date: true }
  • datetime

    • string style: field: 'datetime'
    • object style: field: { datetime: true }
  • time

    • string style: field: 'time'
    • object style: field: { time: true }
  • email

    • string style: field: 'email:true'
    • object style: field: { email: true }
  • in

    • array style: field: [ 'basketball', 'football' ]
    • object style: field: { in: [ 'basketball', 'football' ] }
  • money

    • string style: field: 'money' or field: 'money:0' field: 'money:2' (default)
    • object style: field: { money: { decimal: true } } or field: { money: { decimal: 0 } } or field: { money: { decimal: 2 } }
  • numeric

    • string style: field: 'numeric:6' or field: 'numeric:len=6'
    • object style: field: { numeric: { len: 6 } }
  • regexp

    • string style: field: 'regexp:"^123456$"'
    • object style: field: { regexp: new RegExp(/abc/, 'i') } or field: { regexp: /^[0-9a-zA-z]{8,16}$/ }
  • required

    • string style: field: 'required' or field: 'required:true'
    • object style: field: { required: true }

API

  • #constructor(options?: object)
    • Initialize with options (cant include options properties)
  • #async validate(params: object, rules?: object, messages?: object)
    • Validate params
  • #addRule(name: string, ruleFunc: ruleFunc: (field: string) => (context: object) => (params: object) => bool)
    • Add custom rule
  • #addMessage(name: string, message: string)
    • Add custom message

License

MIT