loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Validations of subproperties in Loopback Model

Open bartolkaruza opened this issue 12 years ago • 14 comments

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.

bartolkaruza avatar Jun 03 '14 13:06 bartolkaruza

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.

raymondfeng avatar Jun 03 '14 14:06 raymondfeng

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?

bartolkaruza avatar Jun 04 '14 08:06 bartolkaruza

Any ideas on supporting JSON Schema for validation? That would solve this in a declarative manner.

fabien avatar Jun 07 '14 14:06 fabien

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.

albertoleal avatar Jun 09 '14 12:06 albertoleal

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 avatar Jun 09 '14 12:06 fabien

@fabien Loopback JSON Schema now supports validation.

andreribeirocoelho avatar Jun 30 '14 17:06 andreribeirocoelho

@andreribeirocoelho I'll have a look, thanks!

fabien avatar Jun 30 '14 17:06 fabien

@raymondfeng @fabien Is this still relevant?

superkhau avatar Feb 26 '15 05:02 superkhau

I'm running into a lot off issues to validate nested models. How can this be implemented?

seanmangar avatar Dec 11 '15 12:12 seanmangar

+1 http://json-schema.org/

listepo avatar Dec 17 '15 15:12 listepo

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();
  }
};

zulfatilyasov avatar Nov 22 '16 11:11 zulfatilyasov

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.

stale[bot] avatar Aug 23 '17 16:08 stale[bot]

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.

stale[bot] avatar Sep 06 '17 16:09 stale[bot]

I don't think this should be closed.

Tronix117 avatar Sep 22 '17 10:09 Tronix117