Class: Dry::Validation::Schema

Inherits:
Object
  • Object
show all
Extended by:
Configurable, Definition
Defined in:
lib/dry/validation/schema.rb,
lib/dry/validation/schema/key.rb,
lib/dry/validation/schema/attr.rb,
lib/dry/validation/schema/rule.rb,
lib/dry/validation/schema/value.rb,
lib/dry/validation/schema/definition.rb

Direct Known Subclasses

Form

Defined Under Namespace

Modules: Definition Classes: Attr, Form, Key, Result, Rule, Value

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Definition

attr, confirmation, key, optional, rule, schema, value

Constructor Details

#initialize(rules = []) ⇒ Schema

Returns a new instance of Schema.



79
80
81
82
83
84
85
86
87
# File 'lib/dry/validation/schema.rb', line 79

def initialize(rules = [])
  @rule_compiler = Logic::RuleCompiler.new(self)
  @rules = rule_compiler.(self.class.rules.map(&:to_ary) + rules.map(&:to_ary))
  @checks = self.class.checks.map(&:to_ary)
  @groups = rule_compiler.(self.class.groups.map(&:to_ary))
  @schemas = self.class.schemas.map(&:new)
  @error_compiler = self.class.error_compiler
  @hint_compiler = self.class.hint_compiler
end

Instance Attribute Details

#checksObject (readonly)

Returns the value of attribute checks.



71
72
73
# File 'lib/dry/validation/schema.rb', line 71

def checks
  @checks
end

#error_compilerObject (readonly)

Returns the value of attribute error_compiler.



75
76
77
# File 'lib/dry/validation/schema.rb', line 75

def error_compiler
  @error_compiler
end

#groupsObject (readonly)

Returns the value of attribute groups.



71
72
73
# File 'lib/dry/validation/schema.rb', line 71

def groups
  @groups
end

#hint_compilerObject (readonly)

Returns the value of attribute hint_compiler.



77
78
79
# File 'lib/dry/validation/schema.rb', line 77

def hint_compiler
  @hint_compiler
end

#rule_compilerObject (readonly)

Returns the value of attribute rule_compiler.



73
74
75
# File 'lib/dry/validation/schema.rb', line 73

def rule_compiler
  @rule_compiler
end

#rulesObject (readonly)

Returns the value of attribute rules.



71
72
73
# File 'lib/dry/validation/schema.rb', line 71

def rules
  @rules
end

#schemasObject (readonly)

Returns the value of attribute schemas.



71
72
73
# File 'lib/dry/validation/schema.rb', line 71

def schemas
  @schemas
end

Class Method Details

.checksObject



67
68
69
# File 'lib/dry/validation/schema.rb', line 67

def self.checks
  @__checks__ ||= []
end

.error_compilerObject



27
28
29
# File 'lib/dry/validation/schema.rb', line 27

def self.error_compiler
  ErrorCompiler.new(messages)
end

.groupsObject



63
64
65
# File 'lib/dry/validation/schema.rb', line 63

def self.groups
  @__groups__ ||= []
end

.hint_compilerObject



31
32
33
# File 'lib/dry/validation/schema.rb', line 31

def self.hint_compiler
  HintCompiler.new(messages, rules: rules.map(&:to_ary))
end

.messagesObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dry/validation/schema.rb', line 35

def self.messages
  default =
    case config.messages
    when :yaml then Messages.default
    when :i18n then Messages::I18n.new
    else
      fail "+#{config.messages}+ is not a valid messages identifier"
    end

  if config.messages_file && config.namespace
    default.merge(config.messages_file).namespaced(config.namespace)
  elsif config.messages_file
    default.merge(config.messages_file)
  elsif config.namespace
    default.namespaced(config.namespace)
  else
    default
  end
end

.predicatesObject



23
24
25
# File 'lib/dry/validation/schema.rb', line 23

def self.predicates
  config.predicates
end

.rulesObject



55
56
57
# File 'lib/dry/validation/schema.rb', line 55

def self.rules
  @__rules__ ||= []
end

.schemasObject



59
60
61
# File 'lib/dry/validation/schema.rb', line 59

def self.schemas
  @__schemas__ ||= []
end

Instance Method Details

#[](name) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/dry/validation/schema.rb', line 116

def [](name)
  if predicates.key?(name)
    predicates[name]
  elsif respond_to?(name)
    Logic::Predicate.new(name, &method(name))
  else
    fail ArgumentError, "+#{name}+ is not a valid predicate name"
  end
end

#call(input) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/dry/validation/schema.rb', line 89

def call(input)
  result = Validation::Result.new(rules.map { |rule| rule.(input) })

  schemas.each do |schema|
    result.merge!(schema.(input).result)
  end

  if checks.size > 0
    resolver = -> name { result[name] || self[name] }
    compiled_checks = Logic::RuleCompiler.new(resolver).(checks)

    compiled_checks.each do |rule|
      result << rule.(result)
    end
  end

  groups.each do |group|
    result.with_values(group.rules) do |values|
      result << group.(*values)
    end
  end

  errors = Error::Set.new(result.failures.map { |failure| Error.new(failure) })

  Schema::Result.new(input, result, errors, error_compiler, hint_compiler)
end

#predicatesObject



126
127
128
# File 'lib/dry/validation/schema.rb', line 126

def predicates
  self.class.predicates
end