validity-unique-property

Validity style validator to ensure a property is unique within entities available in a given collection

validity-unique-property

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.

npm install validity-unique-property

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) ] }
      }
    })

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).

There are 2 possible options to this validity function. idProperty (which defaults to _id) and keys which is an array used to perform multi property validation (defaults to []) i.e only validating against the key this validator is used on.

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.

Licensed under the New BSD License