Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

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
Closed

nested models don't get validated #47

phoet opened this issue Dec 6, 2011 · 4 comments

Comments

@phoet
Copy link
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"]}
@langalex
Copy link
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.

@phoet
Copy link
Contributor Author

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...

@langalex
Copy link
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.

@phoet
Copy link
Contributor Author

phoet commented Feb 7, 2012

thx, will have a look into this.

@phoet phoet closed this as completed Jan 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants