NOTE: This is kind of a duplicate of Loopback validation on Properties who's types are other Models but I am re-asking because a lot of changes were made in Loopback 2
I have a Model (Vehicle) that has properties that are object with some data that needs to be validated.
Here is a sample vehicle
{
details : {
year : 2007,
make: 3333,
....
},
options : {
hasAirbags: true,
powerWindows : false,
....
}
....
}
I have defined my models as such
"vehicleDetails": {
"properties": {
"year": {
"type": "string",
"required": true
}
},
"public": true,
"dataSource": "db",
"plural": "addresses"
},
"Vehicle": {
"properties": {
"options": {
"type": "vehicleOptions"
},
"details":{
"type": "vehicleDetails"
}
},
"public": true,
"dataSource": "db",
"plural": "locations"
}
However, when I create a vehicle. it doesn't check to see if the details object I pass is valid. Relationship between vehicle and vehicleDetails is one-to-one, so I don't want to separate them with relationships.
I found this little work around, but it seems like it should be simpler than this. I am trying to validate that the address
property of an account is a valid Address
model.
Account.validate('address', function (err) {
var Address = Account.app.models.Address;
var address = new Address(this.address);
address.isValid(function (valid) {
if (valid === false) {
err();
}
});
}, {
message: 'Address is not valid'
});