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

@dprovodnikov/validation

v1.2.6

Published

Declarative validation library for javascript objects.

Downloads

15

Readme

Declarative object validation

Build Status

Install

$ npm install @dprovodnikov/validation

Usage example

Here's a typical login form data structure:

const form = {
  login: '',
  password: '',
  repeatPassword: '',
};

Rules declaration

Let's say we have a set of constraints to apply to the fields of this form:

  • Login cannot be empty
  • Password should contain at least one uppercase letter
  • Password should be at least 7 chars long
  • Password should match repeatPassword

These are common rules pretty much every login form uses. Now we need to declare these rules.

import { notEmpty, hasUpperLetter, longerThen, matches } from '@dprovodnikov/validation';

const rules = {
  login: [notEmpty('Login cannot be empty')],
  password: [
    hasUpperLetter('Password should contain at least one uppercase letter'),
    longerThen(7, 'Password should be at least 7 chars long'),
  ],
  repeatPassword: [
    matches('password', 'Password should match repeatPassword'),
  ],
};

The library provides a couple of basic rules that are fairly common:

| Rule | Description | |:----:|:-----------:| | notEmpty | Makes sure the given value is not empty | | hasUpperLetter | Makes sure the given value contains an uppercase letter | | longerThen | Makes sure the given value has a length of more than specified | | matches | Makes sure the given value matches the specified field of the form | | emailFormat | Makes sure the given value matches the regexp (integrated in the lib) | | urlFormat | Makes sure the given value matches the regexp (integrated in the lib) | | shorterThan | Makes sure the given value is shorter than the specified value |

If you need any other rule you can easily implement one using simple api. Here's what a rule looks like:

const rule = message => (value, form) => {
  // You need to return a string.
  // This string represents the validation error message.
  // If the output is empty - the value satisfies the rule and therefore is considered valid.
  let output = '';

  // here you have access to the value this rule was assigned to.
  // the entire form object is also accessible from here for more complex and flexible validation logic

  return output;
};

Here's the implementation of the matches rule that is provided by the library:

export const matches = (comparisonField, message) => (value, fields) => {
  if (value !== fields[comparisonField]) {
    return message;
  }

  return '';
};

As simple as that.

Rules application

In order to create a validator you need to apply the rules you declared earlier.

import { applyRules } from '@dprovodnikov/validation';

const rules = {
  field: [notEmpty('Field should not be empty')],
};

const validate = applyRules(rules);

const form = {
  field: '',
};

validate('all', form); // { field: 'Field should not be empty' }
validate('field', form); // 'Field should not be empty'

The output of the validator is a structure where keys are field names and values are error messages. If a field has multiple rules applied to it and more than one of them fail - the output will contain the first one that failed.

There is a tool to easily check if the report contains any error messages:

import { hasError } from '@dprovodnikov/validation';

const report = validate(form);

if (hasError(report)) {
  ...
}

There's also a method to extract the first error in the output;

import { hasError, getFirstError } from '@dprovodnikov/validation';

const report = validate(form);

if (hasError(report)) {
  const errorMessage = getFirstError(report);
  ...
}

The list of methods:

| Method | Input | Output | Description | |:------:|:-----:|:------:|:-----------:| | applyRules | rules | validator function | Takes in rules and returns a validator | | hasError | validation report | boolean | Takes in a validation report and returns wheather it contains an error on not | | getFirstError | validation report | string | Takes in a validation report and returns the first error |

Build

Make sure tests pass

$ npm run test

Install dependencies

$ npm install

Build from sources

$ npm run build

The output will appear in the dist folder in the project's root.

License

MIT