- Created by: https://github.com/matteodem/meteor-easy-check
- Maintained by: https://github.com/Sindis/easy-check
This package makes it as simple as possible to check objects, e.g. for a Meteor.Collection.
Create a checker like that:
var PersonChecker = new EasyCheck({
'name' : 'string',
'hasCar' : {
type : 'boolean',
required : false
}
});
PersonChecker.check({ name : 'Peter' }); // returns true
PersonChecker.check({ something : 'random' }); // returns false
- Use predefined self-validating insert, update and remove functions
- Simple to create references beetween collections
- Possibility to add custom types
- Hook into all CRUD Operations + when there are Errors
You could use the above example and create secure Meteor.methods with it… or just add your Meteor.Collection as the 2nd parameter:
var CarsChecker = new EasyCheck({
'company' : 'string',
'model' : {
type : 'string',
maxLength : 255,
required : false
},
'isNew' : 'boolean'
}, CarsCollection); // CarsCollection is an instance of Meteor.Collection
// You now got following methods on your collection, with validation
CarsCollection.easyInsert(doc, [callback]);
CarsCollection.easyUpdate(selector, modifier, [options], [callback]);
CarsCollection.easyRemove(selector, [callback]);
meteor add sinda:easy-check
There's quite some options to specify the values you want to check against
{
type : 'array', // the type, such as string, number...
required : false, // is the field is required
maxLength : 255, // the maximum length
minLength : 10, // the minimum length
regex: /[\w]+/, // a regex pattern to test() against
contains : %type%, // only if array, can specify what type of values it contains
references : { // reference a collection, by a field in it
'collection' : cars, // collection which it references
'fields' : '_id' // The field which it references, default _id
}
}
- 'string'
- 'booolean'
- 'array'
- 'object'
- 'date'
- instanceof EasyCheck
You can also add an options object as a third parameter, for example:
new EasyCheck(schema, collection, options);
Following options can be configured:
{
onInsert : function (document), // act on an insert
onUpdate : function (selector, modifier, options) // act on an update
onCheckUpdatedDoc : function (document) // act on updated docs, return false = revert
onRemove : function (id) // act on a remove
onError : function (errors) // act when there were errors when checking a document
}