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

korrekt

v2.2.4

Published

Async validation

Downloads

660

Readme

korrekt

Asynchronous validation library.

Build Status Coverage Status

TOC

Installation

npm i korrekt --save

Usage

Out of the box

const v = require('korrekt')

const validator = v.create({
	name: v.length({ min: 3 }),
	email: v.email(),
	skype: v.all(
		v.length({ min: 3 }),
		v.match(/\w+/),
	),
	phone: v.when(it => !it.skype, v.all(
		v.match(/\d+/),
		v.length({ min: 9, max: 9 }),
	))
})

validator({ name: 'me', email: 'me;[email protected]', skype: 'abba' })
	.then(validatedObject => console.log(validatedObject))
	.catch(v.ValidationError, error => console.error(error.result))

// { name: { message: 'must be longer', meta: { min: 3 } }, email: { message: 'must be an email' } }

Custom rules

const v = require('korrekt')

v.register('same', function (field) {
	return function (value, _, instance) {
		if (instance[field] != value) {
			return `must be same as ${field}`
		}
	}
})

const validator = v.create({
	name: v.length({ min: 3, max: 10 }),
	password: v.length({ min: 3, max: 40 }),
	password_confirmation: v.same('password'),
})

validator({ name: 'me123456789', password: '1', password_confirmation: '2' })
	.then(validatedObject => console.log(validatedObject))
	.catch(v.ValidationError, error => console.error(error.fields))

// { name: { message: 'must be shorter', meta: { max: 10 } }, password: { message: 'must be longer', meta: { min: 3 } }, password_confirmation: { message: 'must be same as  password' } }

Reference

List of methods

create(rule)

Creates validator function.

register(name, rule, [overwrite])

Registers custom rule. Rule parameter here is actually the rule builder, accepting options and custom message as arguments. By default register throws exception if rule with same name already exists, but you can specify true as 3rd argument to explicitly overwrite existing rule.

List of rules

Rule | Description --- | --- required(optionalNestedRule) | Requires value to be present (not undefined or null). Executes optionalNestedRule, if specified. length({ min, max, exactly }) | Verifies value has length and it is between specified boundaries (if any). integer({ min, max }) | Verifies value is an integer and it is between specified boundaries (if any). number({ min, max }) | Verifies value is a number (integer or real) and it is between specified boundaries (if any). string({ min, max, exactly }) | Verifies value is a string and it's length is between specified boundaries (if any). match(regex) | Verifies value matches regex. enum(option1, option2, ...) | Verifies value is equal to one of specified options. email() | Verifies value is an email (has @ inside). when(predicate: instance => boolean, rule) | Verifies value is valid according to rule, but verification is done only if predicate returns true. all(rule1, rule2, ...) | Verifies value is valid according to each rule from rules array. any(rule1, rile2, ...) | Verifies value is valid according to at least one rule from rules array. array(rule, { min, max, exactly }) | Verifies value is an array and each item of it is valid according to rule. Also checks array length if at least one boundary is specified. object({ name: rule }) | Verifies value is an object and checks whether its fields are valid according to rules. function({ min, max, exactly }) | Verifies value is a function and checks whether its arity falls between specified boundaries (if any).

License

MIT