Class: Ukiryu::Validation::Validator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(options, command_def) ⇒ Validator

Returns a new instance of Validator.

Parameters:

  • options (Object)

    the options object to validate

  • command_def (Hash)

    the command definition



26
27
28
29
30
31
# File 'lib/ukiryu/validation/validator.rb', line 26

def initialize(options, command_def)
  @options = options
  @command_def = command_def
  @constraints = []
  build_constraints
end

Instance Attribute Details

#command_defObject (readonly)

The command definition containing validation rules



19
20
21
# File 'lib/ukiryu/validation/validator.rb', line 19

def command_def
  @command_def
end

#constraintsObject (readonly)

Collection of constraints to apply



22
23
24
# File 'lib/ukiryu/validation/validator.rb', line 22

def constraints
  @constraints
end

#optionsObject (readonly)

The options object being validated



16
17
18
# File 'lib/ukiryu/validation/validator.rb', line 16

def options
  @options
end

Instance Method Details

#errorsArray<String>

Get all validation errors

Returns:

  • (Array<String>)

    list of error messages



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.message
  end
  errors_list
end

#valid?Boolean

Check if validation would pass without raising errors

Returns:

  • (Boolean)

    true if valid, false otherwise



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

Returns:

  • (Boolean)

    true if validation passes

Raises:

  • (ValidationError)

    if validation fails



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.message
end