Reputation: 3906
Loopback validates data when saving data to the model. I am manipulating the data before saving in the remote method and I am getting 500 because the field is not present but I am using validatesPresenceOf()
on the field.
module.exports = function(Otp) {
Otp.validatesPresenceOf('number', {
message: 'Phone number is required',
});
Otp.send = function(number, cb) {
// Getting 500 here because number is null
if (number.toString().length === 10) {
number = '1' + number;
}
// Loopback is validating number here
Otp.create({
number: number
});
});
}
How do I validate the presence of number
before the remote method is ever getting called?
I could check for the presence in the remote field itself, but I want to use loopback's built in validations
Upvotes: 0
Views: 516
Reputation: 340
You can't use the builtin validation but You can use beforeRemote hook as defined here as described here
Otp.beforeRemote( "send", function( ctx, next) {
// put your custom validation here
if(!ctx.args.number){
return next("Your Error")
}
next();
});
Upvotes: 1