Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Make sure your data is sound! A validation library for Node.js.
JavaScript
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
test
.gitignore Initial commit with the first string() and integer() types and tests
.travis.yml Remove node v0.6 support
LICENSE.md Change everything over to chilts
README.md Add ability to check for a particular value
package.json
sound.js Add ability to check for a particular value

README.md

sound.js - make sure your data is sound!

Build Status

Synopsis

var schema = {
    username : sound().isString().toLowerCase().trim().required().matches(/^[a-z0-9]{4,16}$/),
    password : sound().isString().required().minLen(8).maxLen(100),
    email    : sound().isString().required().isEmailAddress(),
    logins   : sound().isInteger().required().min(0),
    awesome  : sound().isString().default('yes').toBoolean().isBoolean(),
    url      : sound().isString().isUrl(), // optional
    isAdmin  : sound().isString().required().toBoolean().isBoolean(),
    dob      : sound().isDate(), // accepts 'yyyy-mm-dd'
    agree    : sound().isString().toBoolean().is(true), // make sure they tick T&C's
};

var params = {
    username : 'chilts',
    password : 'abcdefgh',
    email : 'me@example.com',
    agree : 'on', // will convert to true
};

var out = sound.validate(params, schema);
if ( out.ok ) {
    // everything validated ... params contained in `out.val`
}
else {
    // something failed validation ... errors contained in `out.err`
}

If anything fails validation, then err will be an object with keys set to each field for each error. Only one error per field in the schema will be reported.

If no failures are detected, err is just {}. You need to check out.ok to see if validation passed or failed.

var out = sound.validate(params1, schema)
if ( !out.ok ) {
    // something failed, check the keys in `out.err.*`
    ...
    return;
}

// all ok

An error may look like:

{
    password: 'Password should be at least 8 characters'
    logins: 'Logins should be of type integer',
    pi: 'PI should be of type float',
    date: 'Date should be of type date'
}

Other Validation Libraries

There are other validation libraries out there, but they all give me errors which are arrays. To me, this is useless. I don't want to list all of the errors at the top of a form, nor do I know which order those errors are in if I want to pick and choose. I want the err to be an object with the only keys set to be to ones that fail.

Currently 'sound' only reports the first error it encounters with each field and it is likely to stay this way (due to problem and overlap required to keep trying every test even if the first one has failed).

Author

Written by Andrew Chilton - Blog - Twitter.

License

MIT - http://chilts.mit-license.org/2013/

(Ends)

Something went wrong with that request. Please try again.