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 Validations Resolved #88

Closed
wants to merge 12 commits into from
Closed

Conversation

jacekbrozek
Copy link

Hey there!

I've tried Lotus for one of my backend services (suits perfect, btw) and I faced the problem with nested objects' validation.

I want to keep the information about the object (like validations) in separate classes (so DSL for nesting objects was not satisfying for me). Let me show you an example:

class Payment
  include Lotus::Validations

  attribute :recipient_account,  type: RecipientAccount
end

class RecipientAccount
  include Lotus::Validations

  attribute :iban, presence: true
  attribute :bic,  presence: true
end

Even if the coercion works fine for this example, there's no info about nested object validation, so I had to hack it this way:

class Payment
  include Lotus::Validations

  attribute :recipient_account,  type: RecipientAccount

  def valid?
    [ recipient_account ].all?(&:valid?) # I have more nested attributes
    # and here goes the check for the rest of primitive values
  end
end

Looks ugly, huh? (and changing the framework's default behaviour (in most cases) tells you that you shouldn't have been using framework at all)

I've resolved it in my fork (more details and examples in tests and ReadMe) and now sending the PR for review. Let me know if that might be helpful for any of Lotus' users.

P.S. This PR includes nested coercion and validations for Array[YourClass] - it was useful for me the way it is, maybe should inside a collection wrapper (didn't need that).

@khasinski
Copy link

👍

2 similar comments
@patrykfischer
Copy link

👍

@justi
Copy link

justi commented Jan 19, 2016

👍

@madsheep
Copy link

🐑

@ja999
Copy link

ja999 commented Jan 19, 2016

🍻

@runlevel5
Copy link
Member

@jac33k can you please rebase?

@jacekbrozek
Copy link
Author

@joneslee85 done, I've noticed slight decrease of coverage, because of one (not used) method for NestedAttributes class - should I be worried or was it expected?

@@ -442,6 +442,30 @@ error.name # => "address.street"
error.attribute_name # => "street"
```

Also you can use validations nested in Hanami Validations object.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would avoid using subject 'you' here if possible

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason? Just curious ;)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validations can be nested in Hanami::Validations objects
^
statement

Also you can use validations nested in Hanami Validations object
^
action

@runlevel5
Copy link
Member

cc @jodosha @AlfonsoUceda

@jodosha
Copy link
Member

jodosha commented May 16, 2016

Fixed by #100

@jodosha jodosha closed this May 16, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants