Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

nested models don't get validated #47

Closed
phoet opened this Issue Dec 6, 2011 · 4 comments

Comments

Projects
None yet
2 participants
Contributor

phoet commented Dec 6, 2011

we are currently switching from couchrest_model to couch_potato. while doing this our tests failed because couch_potato is not validating 'nested' models. since this is working with the other framework i am not sure if this complies to the contract of active-model validation or not. i would expect that errors on nested models should also fail the validation of the parent and have their errors collected.

here is an example:

class Nested
  include CouchPotato::Persistence

  property :val
  validates :val, :presence => true
end


class Model
  include CouchPotato::Persistence

  property :val
  property :nested, :type => Nested

  validates :val, :presence => true
end

m = Model.new
puts m.valid?
puts m.errors.messages
m.nested = Nested.new
puts m.valid?
puts m.errors.messages
puts m.nested.valid?
puts m.nested.errors.messages
m.val = 'name'
puts m.valid?
puts m.errors.messages
puts m.nested.valid?
puts m.nested.errors.messages

output:

false
{:val=>["can't be blank"]}
false
{:val=>["can't be blank"]}
false
{:val=>["can't be blank"]}
true
{}
false
{:val=>["can't be blank"]}
Owner

langalex commented Feb 7, 2012

yes couch potato has never done that. it was created before active_model so I didn't have a chance to look at it before implementing anything :) I've only had a few cases so far where I wanted this and I then added a custom validation that would check the nested model's validity.I guess it wouldn't be too hard to create a module that would automate that, though I don't have any plans to add that myself. If something like this was to become part of the library I guess it should be optional as not to break existing code.

Contributor

phoet commented Feb 7, 2012

can you give me some pointers where to hook into this? i did not really find out how CouchPotato::Validation::WithActiveModel is working...

Owner

langalex commented Feb 7, 2012

well, you add a class method validate_nested, which would add a custom validation. that would then walk the properties of the instance by called properties on the class and see if there are any nested models and wether they are valid.

Contributor

phoet commented Feb 7, 2012

thx, will have a look into this.

@phoet phoet closed this Jan 6, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment