mongoose-id-validator
Provides a mongoose plugin that can be used to verify that a document which references other documents by their ID is referring to documents that actually exist.
This plugin works by performing a count query for documents in the relevant collection with the ID specified in the document being validated. Note that the validation method is only executed if the ID value has been updated. This helps prevent unnecessary queries when saving a document if the ID has not been changed.
However, please be aware that this will NOT catch any IDs that were valid at the time they were saved but the referenced document has subsequently been removed. You should have validation logic in your delete/remove code to handle this.
Usage
Install via NPM
$ npm install mongoose-id-validator
Then you can use the plugin on your schemas
var idvalidator = ; var ManufacturerSchema = name : String;var Manufacturer = mongoose; var CarSchema = name : String manufacturer : type: SchemaTypesObjectId ref: 'Manufacturer' required: true ;CarSchema;var Car = mongoose; var ford = name : 'Ford' ; ford;
You may also use declare a optional refConditions method in your schema. For example:
var OtherSchema = referencedId : type: SchemaTypesObjectId ref: 'RefSchema' refConditions: field1: 123 ;
Values of refConditions can also be functions. With this you can dynamically set refConditions. These functions have access to the context of the schema instance. An example:
var OtherSchema = refFieldMustBeTrue: true referencedId : type: SchemaTypesObjectId ref: 'RefSchema' refConditions: { return thisrefFieldMustBeTrue } ;
The referenceId value in the code above would only pass validation if the object with this ID exists AND had a property 'field1' that has the value 123. If any conditional property does not match then it would not pass validation.
You can also use this plugin to validate an array of ID references. Please note as above that the implementation runs a single count query to keep the performance impact to a minimum. Hence you will know if there is a bad ID value in the array of references but not which one it is.
An example of this is below:
var idvalidator = ; var ColourSchema = name : String;var Colour = mongoose; var CarSchema = name : String colours : type: SchemaTypesObjectId ref: 'Colour' ;CarSchema;var Car = mongoose; var red = name : 'Red' ;var blue = name : 'Blue' ; red;
Options
Model;
You can also instantiate the validator as an object if you wish to control whether it is enabled at runtime, e.g. for testing.
var IdValidator = getConstructor; var validator = ;schema; //Validation active by default. To disable:validator; //Re-enablevalidator;
Tests
To run the tests you need a local MongoDB instance available. Run with:
npm test
Issues
Please use the GitHub issue tracker to raise any problems or feature requests.
If you would like to submit a pull request with any changes you make, please feel free!
Legal
Code is Copyright (C) Campbell Software Solutions 2014 - 2017.
This module is available under terms of the LGPL V3 license. Hence you can use it in other proprietary projects but any changes to the library should be made available.