langalex / couch_potato

Ruby persistence layer for CouchDB.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

nested models don't get validated

phoet opened this issue · comments

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"]}

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.

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

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.

thx, will have a look into this.