Class: Ukiryu::Validation::Validator
- Inherits:
-
Object
- Object
- Ukiryu::Validation::Validator
- Defined in:
- lib/ukiryu/validation/validator.rb
Overview
Validates option objects using constraint-based validation
The Validator class applies a collection of constraints to an options object, ensuring all validation rules are satisfied before execution.
This is a proper OOP validator that:
-
Uses constraint objects (not procedural code)
-
Returns validation result objects (not string arrays)
-
Provides clear separation of concerns
Instance Attribute Summary collapse
-
#command_def ⇒ Object
readonly
The command definition containing validation rules.
-
#constraints ⇒ Object
readonly
Collection of constraints to apply.
-
#options ⇒ Object
readonly
The options object being validated.
Instance Method Summary collapse
-
#errors ⇒ Array<String>
Get all validation errors.
-
#initialize(options, command_def) ⇒ Validator
constructor
A new instance of Validator.
-
#valid? ⇒ Boolean
Check if validation would pass without raising errors.
-
#validate! ⇒ Boolean
Perform validation.
Constructor Details
#initialize(options, command_def) ⇒ Validator
Returns a new instance of Validator.
26 27 28 29 30 31 |
# File 'lib/ukiryu/validation/validator.rb', line 26 def initialize(, command_def) @options = @command_def = command_def @constraints = [] build_constraints end |
Instance Attribute Details
#command_def ⇒ Object (readonly)
The command definition containing validation rules
19 20 21 |
# File 'lib/ukiryu/validation/validator.rb', line 19 def command_def @command_def end |
#constraints ⇒ Object (readonly)
Collection of constraints to apply
22 23 24 |
# File 'lib/ukiryu/validation/validator.rb', line 22 def constraints @constraints end |
#options ⇒ Object (readonly)
The options object being validated
16 17 18 |
# File 'lib/ukiryu/validation/validator.rb', line 16 def @options end |
Instance Method Details
#errors ⇒ Array<String>
Get all validation errors
59 60 61 62 63 64 65 66 67 |
# File 'lib/ukiryu/validation/validator.rb', line 59 def errors errors_list = [] @constraints.each do |constraint| constraint.validate(get_constraint_value(constraint), constraint_context) rescue Validation::ValidationIssue => e errors_list << e. end errors_list end |
#valid? ⇒ Boolean
Check if validation would pass without raising errors
49 50 51 52 53 54 |
# File 'lib/ukiryu/validation/validator.rb', line 49 def valid? validate! true rescue Ukiryu::Errors::ValidationError false end |
#validate! ⇒ Boolean
Perform validation
37 38 39 40 41 42 43 44 |
# File 'lib/ukiryu/validation/validator.rb', line 37 def validate! @constraints.each do |constraint| constraint.validate(get_constraint_value(constraint), constraint_context) end true rescue Validation::ValidationIssue => e raise Ukiryu::Errors::ValidationError, e. end |