Module: VanillaValidator
- Extended by:
- VanillaValidator, Helpers
- Included in:
- VanillaValidator
- Defined in:
- lib/vanilla_validator.rb,
lib/vanilla_validator/rule.rb,
lib/vanilla_validator/result.rb,
lib/vanilla_validator/helpers.rb,
lib/vanilla_validator/railtie.rb,
lib/vanilla_validator/version.rb,
lib/vanilla_validator/rules/eq.rb,
lib/vanilla_validator/rules/in.rb,
lib/vanilla_validator/rules/gte.rb,
lib/vanilla_validator/rules/max.rb,
lib/vanilla_validator/rules/min.rb,
lib/vanilla_validator/rules/url.rb,
lib/vanilla_validator/rules/date.rb,
lib/vanilla_validator/rules/like.rb,
lib/vanilla_validator/rule_parser.rb,
lib/vanilla_validator/rules/after.rb,
lib/vanilla_validator/rules/alpha.rb,
lib/vanilla_validator/rules/email.rb,
lib/vanilla_validator/rules/falsy.rb,
lib/vanilla_validator/rules/before.rb,
lib/vanilla_validator/rules/boolean.rb,
lib/vanilla_validator/rules/numeric.rb,
lib/vanilla_validator/rules/required.rb,
lib/vanilla_validator/rules/base_rule.rb,
lib/vanilla_validator/value_extractor.rb,
lib/vanilla_validator/rules/block_rule.rb,
lib/vanilla_validator/rules/other_rule.rb,
lib/vanilla_validator/rules/required_if.rb,
lib/vanilla_validator/rules/after_or_equal.rb,
lib/vanilla_validator/rules/before_or_equal.rb
Overview
VanillaValidator is a Ruby module that provides validation functionality.
Defined Under Namespace
Modules: Helpers, Rules, ValidationExtensions Classes: Railtie, Result, Rule, RuleParser, ValueExtractor
Constant Summary collapse
- VERSION =
"0.4.9"
Instance Method Summary collapse
- #raw_input ⇒ Object
-
#validate(input, contract, options = {}) ⇒ Object
Public: Validate input data against a contract.
-
#validate!(input, contract) ⇒ Object
Public: Validate input data against a contract, stopping on the first failure.
Instance Method Details
#raw_input ⇒ Object
85 86 87 |
# File 'lib/vanilla_validator.rb', line 85 def raw_input @raw_input end |
#validate(input, contract, options = {}) ⇒ Object
Public: Validate input data against a contract.
input - The input data to be validated. contract - A set of validation rules defined as a Hash. options - A Hash of additional options for validation (optional).
NOTE: The @raw_input instance variable is used in nested rules to prevent excessive parameter passing.
Returns a Result object containing the validated attributes and any errors.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/vanilla_validator.rb', line 34 def validate(input, contract, = {}) input = input.with_indifferent_access @raw_input = input.dup errors = {} validated = deep_clone_input(input) stop_on_first_failure = [:stop_on_first_failure] contract.each do |attribute, term| attribute = attribute.to_s value = ValueExtractor.get(input, attribute) rules = RuleParser.parse(term) initialized_rules = rules.map do |rule| initialize_rule(attribute, value, rule) end invalid_rules = initialized_rules.reject(&:valid?) if invalid_rules.empty? data_set(validated, attribute, value) else invalid_rules.each do |result| (errors[attribute] ||= []) << result. end if stop_on_first_failure validated_attributes = delete_missing_values(validated) return Result.new(validated_attributes, errors) end end end # Remove any missing or invalid attributes from the validated data. validated_attributes = delete_missing_values(validated) Result.new(validated_attributes, errors) end |
#validate!(input, contract) ⇒ Object
Public: Validate input data against a contract, stopping on the first failure.
input - The input data to validate. contract - A contract specifying validation rules for input data.
Returns: A Result object containing validated data and error messages.
81 82 83 |
# File 'lib/vanilla_validator.rb', line 81 def validate!(input, contract) validate(input, contract, stop_on_first_failure: true) end |