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

Custom error messages for required properties in Hashie::Dash subclasses #232

Closed
wants to merge 2 commits into from

Conversation

pbalaban
Copy link
Contributor

@pbalaban pbalaban commented Oct 7, 2014

No description provided.


required_option = options.delete(:required)
if required_option
fail(KeyError, 'Only :message key supported.') if required_option.is_a?(::Hash) && !required_option[:message]
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be an ArgumentError. Also maybe future-proof it by comparing .keys instead of reaching into :message.

@dblock
Copy link
Member

dblock commented Oct 7, 2014

I like it. See my comment above, otherwise good to merge.

@pbalaban
Copy link
Contributor Author

pbalaban commented Oct 7, 2014

What do you think about this change:

fail(ArgumentError, 'Only :message key supported.') if required_option.is_a?(::Hash) && !required_option.keys.eql?([:message])

If you approve, then I can change specs and update PR.

@dblock
Copy link
Member

dblock commented Oct 7, 2014

Good.


return option if option.is_a?(String)
return option[self] if option.is_a?(Proc)
return option[:message] if option.is_a?(::Hash)
Copy link
Member

Choose a reason for hiding this comment

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

I'm not a fan of all of this type-checking, though it's pretty prevalent throughout the codebase. Is the extra flexibility of allowing Strings, Procs, and Hashes as the required option worth decreasing the readability of the code that handles it?

Personally, I like the original suggested implementation in #98.

class Credentials < Hashie::Dash
  property :req_attr, :required =>  true, :message => "is required for the credentials hash"
end

This is pretty clear and wouldn't require all of the type-checking, which would make the code more readable.

@dblock
Copy link
Member

dblock commented Oct 9, 2014

I think i have to agree with @michaelherold, it seems like not nesting anything inside required is less ambiguous. What do you think @Joss?

@dblock
Copy link
Member

dblock commented Oct 9, 2014

Closed in favor of #233, thanks for your cooperation @Joss.

@dblock dblock closed this Oct 9, 2014
@pbalaban pbalaban deleted the issue-98 branch October 24, 2014 16:34
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

Successfully merging this pull request may close these issues.

None yet

3 participants