Giter VIP home page Giter VIP logo

Comments (2)

jamesbrooks avatar jamesbrooks commented on July 1, 2024

Hi,

What you're after can be achieved by building a custom validator specific to your use-case, which is taking a value (being an array), and ensuring every value in that array confirmed to a pre-defined whitelist.

Taking the logic you've got from your proc something like this could do the trick (while still being-re-usable and configurable from some JSON).

class HashValidator::Validator::WhitelistValidator < HashValidator::Validator::Base
  def initialize(attribute, whitelist)
    super(attribute)
    @whitelist = whitelist
  end

  def validate(key, value, validations, errors)
    unless value.is_a?(Array) && (value - @whitelist).empty?
      errors[key] = "value must be whitelisted"
    end
  end
end

# Add the validator
events_validator = HashValidator::Validator::WhitelistValidator.new('events', ['foo', 'bar', 'baz'])
HashValidator.append_validator(events_validator)



# Simple case
validator = HashValidator.validate({ events: ['foo'] }, { events: 'events' })
validator.valid?  # => true
validator.errors  # => {}

# Complex success
validator = HashValidator.validate({ events: ['foo', 'bar', 'baz'] }, { events: 'events' })
validator.valid?  # => true
validator.errors  # => {}

# Complex failure
validator = HashValidator.validate({ events: ['foo', 'bar', 'florp'] }, { events: 'events' })
puts validator.valid?  # => false
puts validator.errors  # => {events: "value must be whitelisted"}

Here we've designed a whitelist validator that can be used for different kind of fields, in this example we only need one that we'll name events.

Does this help at all?

from hash_validator.

djpate avatar djpate commented on July 1, 2024

It does! Thanks for the help.

from hash_validator.

Related Issues (13)

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.