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

easy-validator

v0.2.0

Published

Simple object validator for NodeJS

Downloads

1

Readme

easy-validator

Simple object validator for NodeJS (as Symfony Annotation validation system)

Status

Build Status

Installation

$ npm install easy-validator

To run the unit tests with mocha from your project :

npm test

Validating Objects

Sample of simple validating a not null string from an object :

var _ = require('underscore');
var eValidator = require('easy-validator');

//Creating sample object
var objectToValidate = {
  name: "Foo",
  email: "[email protected]",
  age: 33
};

//Create the constraint collection (The name property should pass the 2 validations , NotNull and String type
var constraintCollection = eValidator.Assert({
  "name":  ['@Assert:NotNull()', '@Assert:Type("string")'], // <-- Array of asserts for each property required to validate
  "email": ['@Assert:Email()'],
  "age":   ['@Assert:Range(min=18,max=99)']
});

//Validate the object, and retrieve the list of errors
violationList = constraintCollection.perform.validate(objectToValidate);

//You can see all errors on the violationsList (array)
_.each(violationList, function(violation){
  console.log("Property ["+violation.propertyPath+"] Error ["+violation.message+"]");
});

The same validation can be achieved wit object definition, instead string definition :

//String definition of the validators
var constraintCollection = eValidator.Assert({
  "name":  ['@Assert:NotNull()', '@Assert:Type("string")'], // <-- Array of asserts for each property required to validate
  "email": ['@Assert:Email()'],
  "age":   ['@Assert:Range(min=18,max=99)']
});
//The same object defined with object by each property
var constraintCollection = eValidator.Assert({
  "name":  {"assert":'NotNull'},
  "email": {"assert": 'Email'},
  "age":   {"assert": 'Range', "min":18, "max":99}
});

List of Asserts

@Assert:Email() For string values

Forces a value to be a valid email

Sample :

var constraintCollection = eValidator.Assert({
  email_address: ['@Assert:Email()']
})
No configuration
@Assert:NotNull()

Forces a value to be not null value, and to be defined. Will add an error to violation list if null or undefined

No configuration
@Assert:Null()

Forces a value to be null value, or be undefined. Will add an error to violation list if not null or defined

No configuration
@Assert:Range() For numeric values

Forces a value to be defined in a range of numbers. Should be between min and max configurations if defined. Both configuration 'min' and 'max' are optional configurations.

Sample :

var constraintCollection = eValidator.Assert({
  name: ['@Assert:Range(min=18,max=99)'] //Should be between 18 and 99 the value to be matched
})
@Assert:Type()

Forces a value to be null value, or be undefined. Will add an error to violation list if not null or defined

Allowed configuration of type (default configured to 'string')

Sample for array type

var constraintCollection = eValidator.Assert({
  name: ['@Assert:Type("array")']
})

Allowed types :

  • string
  • array
  • object
  • boolean
  • numeric

TODO

Keep implementing Asserts implemented on Symfony2 current validation system

http://symfony.com/doc/current/book/validation.html