GithubHelp home page GithubHelp logo

Comments (7)

caalberts avatar caalberts commented on June 22, 2024 2

Starting with values sounds like a good idea. I'm not sure how large is the need for a type validation compared to predefined values.

I think a PR for the above would be super valuable and solve 99% of the scenarios.

💯

from hashie.

jch avatar jch commented on June 22, 2024

That sounds like a good custom subclass. Would you be interested in trying to implement it and submit a pull request?

from hashie.

Spaceghost avatar Spaceghost commented on June 22, 2024

I'll take a peek into it.

from hashie.

dblock avatar dblock commented on June 22, 2024

Bump.

from hashie.

dblock avatar dblock commented on June 22, 2024

I think this can be a new feature for Dash

property :gender, values: [:male, :female, :meat_popsicle]

from hashie.

caalberts avatar caalberts commented on June 22, 2024

Should this be implemented as a validation instead? It would serve the 2 examples above, but also open to other validations in the future.

property :gender, validate: { within: [:male, :female, :meat_popsicle] }
property :age, validate: { within: age_range }
property :hair_color, validate: { with: ->(colour) { hex_color? }  }

from hashie.

dblock avatar dblock commented on June 22, 2024

Feel like an overkill because validate serves no other purpose than wrap with vs. within vs. something else and creates unnecessary mental overhead. For the first two we can make it equally predictable with a single keyword, values:.

property :gender, values: [:male, :female, :meat_popsicle]
property :age, values: 1..150 # a Range

I think a PR for the above would be super valuable and solve 99% of the scenarios.

The other half of this problem is that you don't always want values, but types. So I'd want

property :possible, type: Boolean
property :offset, type: Integer

The third, color, gets is a bit tricky, I think I'd want something more OO, which lets me define higher level classes, and would borrow from Grape, which is battle tested on this type of checks, extensibility, etc. It could look like this:

class Color
  attr_reader :value

  def initialize(color)
    @value = color
  end

  def self.parse(value)
    fail 'Invalid color' unless hex_color?(value)
    new(value)
  end

  def hex_color?(value)
   # ...
  end
end

property :hair_color, type: Color

Thinking out loud we would really want the entire coercion, types, checks system that is implemented in Grape using dry-types. Maybe as a separate gem even.

Thoughts?

from hashie.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.