Validations of subproperties in Loopback Model
I'm having trouble understanding why index and validation is not working on my Model defined programatically. Below is the setup of a simple shopping cart Model, for which I need to use a custom id which is not at the root level of the model.
var defaultSettings = {idInjection:false, strict:true};
var CartEntityModel = {
'products':{type:[Product], required:false}, // Product is defined elsewhere
'cartId':{type:String, required:true, index:true, generated:true}
}
var CartEntity = ds.define('cartEntity', CartEntityModel, defaultSettings);
var cartProperties = {
'_entity': {type:CartEntity, required:true}
}
app.model('cart', {
properties: cartProperties,
options: defaultSettings,
dataSource: 'mongodb'
});
This generates an id field at the root of the 'cart' and only validates that the _entity field is not empty. No other validation at lower levels takes place and the index property seems to be entirely ignored. What am I doing wrong here?
The structure is more complex than absolutely necessary because I need to match an existing data structure, which always has '_entity' at the root and the id's at deeper levels.
Can you try to define the index for the nested field at model level? See http://docs.strongloop.com/display/DOC/Model+definition+reference#Modeldefinitionreference-Indexes. The key should be _entity.cartId.
I don't think we do deep validation at this moment. It's worth exploring though.
I tried the below setup, defining the deeper index at the model and as an indexes option. Neither seem to have the desired effect of having a generated unique id at _entity.cartId instead of at root through id.
var CartEntityModel = {
'products':{type:[Product], required:false},
'cartId':{type:String, required:true, index:true, generated:true}
}
var CartEntity = ds.define('cartEntity', CartEntityModel, defaultSettings);
var cartProperties = {
_entity: {type:CartEntity, required:true}
}
var cartOptions = {
strict:true,
idInjection:false,
indexes: {
cartIndex: {
'_entity.cartId':{generated:true, index:true}
}
}
}
app.model('cart', {
properties: cartProperties,
options: cartOptions,
dataSource: 'mongodb'
});
app.models.cart.create({_entity:{}}, function(err, cart) {
console.log(cart);
console.log('err: ' + err);
})
The result of creating the object is as follows;
{ _entity: { products: undefined, cartId: undefined },
id: 538ed20966e2311c20192159 }
Any ideas?
Any ideas on supporting JSON Schema for validation? That would solve this in a declarative manner.
Hi @fabien, we're working on this module: Loopback JSON Schema, which adds Json Schema support to Loopback.
Currently, we don't support validation yet, but this is something that we would like to have. I'd recommend you take a look at that module and see if that may help you with anything else.
Hi @albertoleal - I found your module, and it looks interesting. Since it's based on JSON Schema, I expected it to do validation already, so it's good to hear that this is a path you'd like to follow as well. It would probably be not that difficult to integrate something like https://github.com/geraintluff/tv4 for that.
I'll keep an eye on it, thanks.
@fabien Loopback JSON Schema now supports validation.
@andreribeirocoelho I'll have a look, thanks!
@raymondfeng @fabien Is this still relevant?
I'm running into a lot off issues to validate nested models. How can this be implemented?
+1 http://json-schema.org/
Currently you can validate nested CartEntity model by manually calling _entity.isValid() method
module.exports = function (Cart) {
Cart.validate('cartEntity', validateCartEntity, {
message: 'cart entity is not valid'
});
function validateCartEntity(err) {
if (!this._entity.isValid()) err();
}
};
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.
I don't think this should be closed.