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

constraint-validator

v1.1.7

Published

Constraint validation tool

Downloads

36

Readme

Constraints form validator

npm version Build Status codecov

This library contains list of classes that allows developers to create custom validation flows.

The main idea to have configured form with rules for each field. Each field can be validated by specific constraint.

Validation constraints inspired by Symfony Constraints.

Install

npm i constraint-validator --save

Basic usage

CommonJS module
var validator = require("constraint-validator");
var form = new validator.Form();

form
    .add('email', [
        new validator.NotBlank(),
        new validator.Email(),
    ])
    .add('password', [
        new validator.NotBlank(),
        new validator.Length({min: 6}),
    ]);
    
var errors = form.validate({
    email: '[email protected]',
    password: '123456',
});
ESM module
import { Form, NotBlank, Email, Length } from 'constraint-validator';

const form = new Form();

form
    .add('email', [
        new NotBlank(),
        new Email(),
    ])
    .add('password', [
        new NotBlank(),
        new Length({min: 6}),
    ]);

const errors = form.validate({
    email: '[email protected]',
    password: '1234567',
});
Error handing

In case of form data is not valid the errors object contains properties (related to from filed names) and array of Error objects.

{
    'email': [
        Error,
        Error,
    ],
    'password': [
        Error,
    ]   
}

Otherwise error variable will be empty object {}

Data transformers

Data transformers are used to translate the data for a field into a other format and back. The data transformers act as middleware and will be executed in the same order as they were applied.

There are 2 types of data transformers:

  • transformer - executes before validation process
  • reverseTransformers - executes after validation process

Form data transformers

import { Form, NotBlank, Email } from 'constraint-validator';

const form = new Form();

form
    .add('email', [
        new NotBlank(),
        new Email(),
    ])
    // next transformers will be applied to the form data
    .addTransformer(data => {
        data.email += '@example.com'

        return data;
    })
    .addReverseTransformer(data => {
        data.email = data.email.replace(/@example.com/, '@example.me');
        
        return data; 
    });

form.validate({email: 'email'});

console.log(form.getData());
// Output:
// {"email": "[email protected]"}

Field data transformers

import { Form, NotBlank, Email } from 'constraint-validator';

const form = new Form();

form
    .add('email', [
        new NotBlank(),
        new Email(),
    ])
    .get('email')
    // next transformers will be applied to the 'email' field only
    .addTransformer(value => value + '@example.com')
    .addReverseTransformer(value => value.replace(/@example.com/, '@example.me'));

form.validate({email: 'email'});

console.log(form.getData());
// Output:
// {"email": "[email protected]"}

Documentation

You can create custom constrains by using deep integration.

Dependencies

  • locutus - JavaScript implementation of PHP functions
  • Luxon Moment - DateTime manipulation library
  • IpAddrJs - IP address manipulation library

Notes

Q: Why not exceptions?
A: See try/catch performance test

Q: Is there same logic as symfony has?
A: No, please check documentation for each constraint

TODO

  • Provide types.d.ts for better user experience
  • CI/CD build flow (drop dist folder)
  • Proper package integration (get IDE autocomplete working better)
  • Investigate DayJS as replacements: find a way to validate timezones.