Comments (2)
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.
It does! Thanks for the help.
from hash_validator.
Related Issues (13)
- Append Validator Doesn't Work HOT 4
- Enumeration support HOT 4
- Needs more examples beyond simple HOT 1
- Add 'hash' type to stop validation recursion HOT 2
- Requied field bug when evaluating value is boolean "false" HOT 1
- Validators as symbol HOT 2
- How to use regexp validations? HOT 2
- Conditional and referential validations HOT 1
- Does it support many validators for same key ? HOT 1
- [enhancement] Exclusive key validation. HOT 1
- Feature Request: Union
- ArrayValidator not available when installing from RubyGems HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hash_validator.