A mixin for mdg:validated-method
to throw errors on specified conditions before the run
function.
meteor add ziarno:restrict-mixin
As a mixin:
SomeCollection.methods.someMethod = new ValidateMethod({
name,
mixins: [RestrictMixin],
restrictions: [
{
condition: function (args) {
return !this.userId;
},
error: function (args) {
return new Meteor.Error(this.name + '.unauthorized', 'You must be logged in');
}
}
],
validate,
run: function (args) {}
});
You can also create mixins:
var isLoggedInMixin = RestrictMixin.createMixin({
condition: function (args) {
return !this.userId;
},
error: function (args) {
return new Meteor.Error(this.name + '.unauthorized', 'You must be logged in');
}
});
SomeCollection.methods.someMethod = new ValidateMethod({
name,
mixins: [isLoggedInMixin],
validate,
run: function (args) {
//will not run if !this.userId
}
});
Notes:
restrictions
can be an array or object (also as value passed tocreateMethod
)condition
anderror
can be functions or objectscondition
anderror
functions are called in the correct context (this
is the same as in therun
method) and get passed the same arguments (which isargs
passed to the method)
If you want to run a restriction only in the simulation or only on the server, use the env
field:
SomeCollection.methods.someMethod = new ValidateMethod({
name,
mixins: [RestrictMixin],
restrictions: [
{
condition: function (args) {
return !Meteor.users.find(args.someUserId);
},
error: function (args) {
return new Meteor.Error(this.name + '.notFound', 'User not found');
},
env: 'server-only' //this restricion will *not* run if this.isSimulation
}
],
validate,
run: function (args) {}
});
- Possible
env
values:'server-only'
'simulation-only'
- Runs on both environments if
env
is not specified
Extra: usage with ProvideMixin
There might be situations where you want to find an object in the DB
and use it for error triggering - but you shouldn't make a DB query for each
error.
That's where ProvideMixin
comes in handy
- it adds additional arguments to the
condition()
,error()
, andrun()
functions.
SomeCollection.methods.someMethod = new ValidateMethod({
name,
mixins: [RestrictMixin, ProvideMixin],
provide: function (args) {
return SomeOtherCollection.findOne(args.someOtherCollectionId);
},
restrictions: [
{
condition: function (args, someOtherCollectionItem) {
return someOtherCollectionItem.owner !== this.userId;
},
error: function (args, someOtherCollectionItem) {
return new Meteor.Error(this.name + '.unauthorized', 'Only owners can do stuff');
},
}
],
validate,
run: function (args, someOtherCollectionItem) {
//...
}
});