Validity style validator to ensure a property is unique within entities available in a given collection
JavaScript
Pull request Compare This branch is 8 commits behind bengourley:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
test
.gitignore
.travis.yml
Readme.md
package.json
validator.js

Readme.md

validity-unique-property

Build Status

Validity style validator to ensure a property is unique within entities available in a given collection, for instance: all users must have a unique email address.

Installation

npm install validity-unique-property

Usage

Below is a simple example for usage with schemata and save:

var validity = require('validity')
  , schemata = require('schemata')
  , save = require('save')
  , collection = save('user')
  , createUniqueValidator = require('validity-unique-property')

var schema = schemata(
    { emailAddress:
      { type: String
      , validators: { all: [ validity.email, createUniqueValidator(collection.findOne) ] }
      }
    })

API

var validate = createUniqueValidator(Function: findOne)

Create a validate function. findOne(obj, cb) should be a query function that allows the validator access to whatever persistence mechanism you are using, in order to check for the uniqueness of the given property. obj is a query object and cb is a callback function cb(err, foundObject).

validate(String:key, String:keyDisplayName, Object:object, Function:cb)

This is a validity compatible function, which in turn is used by schemata for schema validation.

The callback signature cb(err, errorMessage).

err is an Error object if something bad happened and null otherwise. errorMessage is a String if a validation error happened and undefined otherwise.

Licence

Licensed under the New BSD License