Class: Kumi::Core::Domain::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/kumi/core/domain/validator.rb

Class Method Summary collapse

Class Method Details

.analyze_domain(_field, domain) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/kumi/core/domain/validator.rb', line 57

def self.analyze_domain(_field, domain)
  case domain
  when Range
    RangeAnalyzer.analyze(domain)
  when Array
    EnumAnalyzer.analyze(domain)
  when Proc
    {
      type: :custom,
      description: "Custom constraint function",
      sample_values: [],
      invalid_samples: []
    }
  else
    {
      type: :unknown,
      constraint: domain,
      sample_values: [],
      invalid_samples: []
    }
  end
end

.create_violation(field, value, domain) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/kumi/core/domain/validator.rb', line 48

def self.create_violation(field, value, domain)
  {
    field: field,
    value: value,
    domain: domain,
    message: ViolationFormatter.format_message(field, value, domain)
  }
end

.extract_domain_metadata(input_meta) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kumi/core/domain/validator.rb', line 35

def self.(input_meta)
   = {}

  input_meta.each do |field, meta|
    domain = meta[:domain]
    next unless domain

    [field] = analyze_domain(field, domain)
  end

  
end

.validate_context(context, input_meta) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/kumi/core/domain/validator.rb', line 22

def self.validate_context(context, input_meta)
  violations = []

  context.each do |field, value|
    meta = input_meta[field]
    next unless meta&.dig(:domain)

    violations << create_violation(field, value, meta[:domain]) unless validate_field(field, value, meta[:domain])
  end

  violations
end

.validate_field(_field_name, value, domain) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/kumi/core/domain/validator.rb', line 7

def self.validate_field(_field_name, value, domain)
  return true if domain.nil?

  case domain
  when Range
    domain.cover?(value)
  when Array
    domain.include?(value)
  when Proc
    domain.call(value)
  else
    true
  end
end