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

Extension to validate? #300

Open
SkyCrawl opened this issue May 11, 2015 · 8 comments
Open

Extension to validate? #300

SkyCrawl opened this issue May 11, 2015 · 8 comments

Comments

@SkyCrawl
Copy link

Would there be an easy way to add validations to certain declared properties or hash keys?

For example:

class Mine < ::Hashie::Dash
    include ::Hashie::Extensions::MethodAccess
    include ::Hashie::Extensions::MethodValidation

    property :title, required: true, validated: true

    def title_valid?(value)
      ...
    end
end

Perhaps even:

class Mine < ::Hashie::Dash
    include ::Hashie::Extensions::MethodAccess
    include ::Hashie::Extensions::MethodValidation

    property :title, required: true, validated_common: true

    def property_valid?(name, value)
      ...
    end
end
@michaelherold
Copy link
Member

What types of things would you want to validate? Type "validations" can be done using the property translation mixin (i.e. the behavior that Trash adds to Dash).

@SkyCrawl
Copy link
Author

Specifically, I'd like the hash classes to provide a mechanism for validating semantics (values). Using the above example:

def title_valid?(value)
    value.length < 16
end

def property_valid?(name, value)
      false if value.nil?
      value =~ /email_regex/ if name.start_with?('email-')
      ...
end

It'd be great to know that (e.g.) Dash refuses to accept (whether silently or with error - depending on what the user wants) entries that have a value of nil :).

@martinstreicher
Copy link
Contributor

I have been doing this to get validations:

      include ActiveModel::Validations

@dblock
Copy link
Member

dblock commented Aug 22, 2015

I think @martinstreicher is onto something, ActiveModel is the "golden standard" and just works out of the box. Any reason why we would still want this here?

@martinstreicher
Copy link
Contributor

This is a code snippet from a class derived from Hashie.

require 'hashie/dash'

module DSL
  module Keywords
    class Base < Hashie::Dash
      include ActiveModel::Validations

I can then do things like validate :method and valid? There is some overlap with things like Hashie::Dash require and validates_presence_of, but I can use all the rest of the common validators, plus write any of my own.

@dblock
Copy link
Member

dblock commented Aug 22, 2015

I wonder whether we should add this to the docs, what do you think @martinstreicher ?

@martinstreicher
Copy link
Contributor

I think it worthwhile. In my recent use cases, I am using Hashie::Dash as a stand-in for ActiveModel where I don’t require persistence. I use the other features of Hashie like coercion and dot access and mix in ActiveModel components to do validation, etc.

On Aug 22, 2015, at 3:43 PM, Daniel Doubrovkine (dB.) @dblockdotorg notifications@github.com wrote:

I wonder whether we should add this to the docs, what do you think @martinstreicher ?


Reply to this email directly or view it on GitHub.

@dblock
Copy link
Member

dblock commented Aug 23, 2015

I'm looking forward to a PR from @martinstreicher :)

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

No branches or pull requests

4 participants