Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoSign in
Socket

valid

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

valid - rubygems Package Compare versions

Comparing version
1.1.0
to
1.2.0
+35
-28
lib/validation/validator.rb

@@ -20,27 +20,21 @@ module Validation

# * a hash containing the rule as the key and it's parameters as the values
# * e.g. rule(:field, :length => {:minimum => 3, :maximum => 5})
# * e.g. rule(:field, :length => { :minimum => 3, :maximum => 5 })
# * an array combining the two previous types
def rule(field, rule)
def rule(field, definition)
field = field.to_sym
if rules[field].nil?
rules[field] = []
end
rules[field] = [] if rules[field].nil?
begin
if rule.respond_to?(:each_pair)
add_parameterized_rule(field, rule)
elsif rule.respond_to?(:each)
rule.each do |r|
if r.respond_to?(:each_pair)
add_parameterized_rule(field, r)
if definition.respond_to?(:each_pair)
add_parameterized_rules(field, definition)
elsif definition.respond_to?(:each)
definition.each do |item|
if item.respond_to?(:each_pair)
add_parameterized_rules(field, item)
else
r = Validation::Rule.const_get(camelize(r)).new
add_object_to_rule(r)
rules[field] << r
add_single_rule(field, item)
end
end
else
rule = Validation::Rule.const_get(camelize(rule)).new
add_object_to_rule(rule)
rules[field] << rule
add_single_rule(field, definition)
end

@@ -61,3 +55,3 @@ rescue NameError => e

if ! @obj.respond_to?(field)
raise InvalidKey
raise InvalidKey, "cannot validate non-existent field '#{field}'"
end

@@ -79,18 +73,31 @@

# Adds a parameterized rule to this object
def add_parameterized_rule(field, rule)
rule.each_pair do |key, value|
r = Validation::Rule.const_get(camelize(key)).new(value)
add_object_to_rule(r)
rules[field] << r
# Adds a single rule to this object
def add_single_rule(field, key_or_klass, params = nil)
klass = if key_or_klass.respond_to?(:new)
key_or_klass
else
get_rule_class_by_name(key_or_klass)
end
args = [params].compact
rule = klass.new(*args)
rule.obj = @obj if rule.respond_to?(:obj=)
rules[field] << rule
end
# Adds this validation object to a rule if it can accept it
def add_object_to_rule(rule)
if rule.respond_to?(:obj=)
rule.obj = @obj
# Adds a set of parameterized rules to this object
def add_parameterized_rules(field, rules)
rules.each_pair do |key, params|
add_single_rule(field, key, params)
end
end
# Resolves the specified rule name to a rule class
def get_rule_class_by_name(klass)
klass = camelize(klass)
Validation::Rule.const_get(klass)
rescue NameError => e
raise InvalidRule.new(e)
end
# Converts a symbol to a class name, taken from rails

@@ -97,0 +104,0 @@ def camelize(term)

module Validation
VERSION = '1.1.0'
VERSION = '1.2.0'
end
+27
-17

@@ -16,5 +16,5 @@ # Validator

validator = Validation::Validator.new(object)
validator.rule(:email, [:email, :not_empty]) # multiple rules in one line
validator.rule(:password, :not_empty) # a single rule on a line
validator.rule(:password, :length => {:minimum => 3}) # a rule that takes parameters
validator.rule(:email, [:email, :not_empty]) # multiple rules in one line
validator.rule(:password, :not_empty) # a single rule on a line
validator.rule(:password, :length => { :minimum => 3 }) # a rule that takes parameters

@@ -32,20 +32,16 @@ if validator.valid?

If you have a custom rule you need to write, just put it inside the `Validation::Rule` namespace:
If you have a custom rule you need to write, you can create a custom rule class for it:
```ruby
module Validation
module Rule
class MyCustomRule
def error_key
:my_custom_rule
end
class MyCustomRule
def error_key
:my_custom_rule
end
def valid_value?(value)
# Logic for determining the validity of the value
end
def valid_value?(value)
# Logic for determining the validity of the value
end
def params
{}
end
end
def params
{}
end

@@ -61,2 +57,16 @@ end

If you add your custom rule class to the `Validation::Rule` namespace, you can reference it using a symbol:
```ruby
validator.rule(:field, :my_custom_rule) # resolves to Validation::Rule::MyCustomRule
validator.rule(:field, :my_custom_rule => { :param => :value })
```
Otherwise, just pass in the rule class itself:
```ruby
validator.rule(:field, MyProject::CustomRule)
validator.rule(:field, MyProject::CustomRule => { :param => :value })
```
### Writing self-contained validators

@@ -63,0 +73,0 @@