Reputation: 299
I'm stuck at the validation process of my API. I want to validate multiple objects before storing them into the DB. I've created a function that I can call in my remote method in order to handle the validation.
The function below is working as excepted only the Account validation is causing trouble. My Loopback API crashed if the account object is not valid. Very weird because I do exactly the same with the other 2 models?
I've tried many things but nothing is working. Hopefully someone can point me in the right direction.
async function validate() {
vehicle.isValid(function(valid) {
if (!valid)
throw (vehicle.errors);
});
repairJob.isValid(function(valid) {
if (!valid){
console.log(repairJob.errors);
throw (repairJob.errors);
}
});
account.isValid(function(valid) {
if (!valid){
console.log(account.errors);
throw (account.errors);
}
});
}
Output from console:
Browse your REST API at http://localhost:3000/explorer Errors { password: [ 'can\'t be blank' ], email: [ 'can\'t be blank', 'can\'t be blank' ] }
/Users/xxx/Development/xxx/xxx-api-v2/common/models/job.js:105 throw (account.errors); ^ [object Object] [nodemon] app crashed - waiting for file changes before starting...
Upvotes: 0
Views: 655
Reputation: 261
In your account.json, you might have mentioned "base": "User"
, you inherited account model from user model.As you can see below is the user.json where email and password properities are made required:true
and so the loopback throws the error.You can change this in account.json by making email and password required:false
if you dont want these two fields to be mandatory.Orelse check your request whether these two fileds have value.
Upvotes: 2
Reputation: 1648
According to documentation (https://loopback.io/doc/en/lb3/Validating-model-data.html) you can use isValid
method
To invoke the validation constraints explicitly
The same loopback will do automatically each time model instance is created or updated.
As I understand the account
model has set "base": "User"
and you can't have a user without a password or email.
Can you add how account
look like before calling isValid
function?
Upvotes: 0