validity-entity-exists

Validity style validator to ensure a separate entity exists when being added to the current entity

validity-entity-exists

Validity style validator to ensure a separate entity exists when being added to the current entity, for instance: the user must exist before adding their ID as a document author.

npm install validity-entity-exists

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

 
var validity = require('validity')
  , schemata = require('schemata')
  , save = require('save')
  , documentCollection = save('document')
  , userCollection = save('user')
  , entityExistsValidator = require('validity-entity-exists')
 
var schema = schemata(
    { author:
      { type: String
      , validators: { all: [ entityExistsValidator(userCollection.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 existence of the given property. obj is a query object and cb is a callback function cb(err, foundObject).

Optional 2nd parameter which allows you to provide the name of the property in the subsequent collection.

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