Class: Dry::Validation::Schema

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

Direct Known Subclasses

Form

Defined Under Namespace

Classes: Attr, Check, DSL, Form, Key, Rule, Value

Constant Summary collapse

NOOP_INPUT_PROCESSOR =
-> input { input }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules, options) ⇒ Schema

Returns a new instance of Schema.



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/dry/validation/schema.rb', line 161

def initialize(rules, options)
  @rule_compiler = SchemaCompiler.new(self)
  @error_compiler = options.fetch(:error_compiler)
  @hint_compiler = options.fetch(:hint_compiler)
  @predicates = options.fetch(:predicates)
  @input_processor = options.fetch(:input_processor, NOOP_INPUT_PROCESSOR)

  initialize_options(options)
  initialize_rules(rules)
  initialize_checks(options.fetch(:checks, []))

  freeze
end

Instance Attribute Details

#checksObject (readonly)

Returns the value of attribute checks.



147
148
149
# File 'lib/dry/validation/schema.rb', line 147

def checks
  @checks
end

#error_compilerObject (readonly)

Returns the value of attribute error_compiler.



155
156
157
# File 'lib/dry/validation/schema.rb', line 155

def error_compiler
  @error_compiler
end

#hint_compilerObject (readonly)

Returns the value of attribute hint_compiler.



157
158
159
# File 'lib/dry/validation/schema.rb', line 157

def hint_compiler
  @hint_compiler
end

#input_processorObject (readonly)

Returns the value of attribute input_processor.



151
152
153
# File 'lib/dry/validation/schema.rb', line 151

def input_processor
  @input_processor
end

#optionsObject (readonly)

Returns the value of attribute options.



159
160
161
# File 'lib/dry/validation/schema.rb', line 159

def options
  @options
end

#predicatesObject (readonly)

Returns the value of attribute predicates.



149
150
151
# File 'lib/dry/validation/schema.rb', line 149

def predicates
  @predicates
end

#rule_compilerObject (readonly)

Returns the value of attribute rule_compiler.



153
154
155
# File 'lib/dry/validation/schema.rb', line 153

def rule_compiler
  @rule_compiler
end

#rulesObject (readonly)

Returns the value of attribute rules.



145
146
147
# File 'lib/dry/validation/schema.rb', line 145

def rules
  @rules
end

Class Method Details

.create_class(target, other = nil, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dry/validation/schema.rb', line 48

def self.create_class(target, other = nil, &block)
  klass =
    if other
      Class.new(other.class)
    else
      Validation.Schema(target.schema_class, parent: target, build: false, &block)
    end

  klass.config.path = [target.name] if other
  klass.config.input_processor = :noop

  klass
end

.default_messagesObject



97
98
99
100
101
102
103
104
# File 'lib/dry/validation/schema.rb', line 97

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

.default_optionsObject



137
138
139
140
141
142
143
# File 'lib/dry/validation/schema.rb', line 137

def self.default_options
  { predicates: predicates,
    error_compiler: error_compiler,
    hint_compiler: hint_compiler,
    input_processor: input_processor,
    checks: config.checks }
end

.error_compilerObject



106
107
108
# File 'lib/dry/validation/schema.rb', line 106

def self.error_compiler
  @error_compiler ||= ErrorCompiler.new(messages)
end

.hint_compilerObject



110
111
112
# File 'lib/dry/validation/schema.rb', line 110

def self.hint_compiler
  @hint_compiler ||= HintCompiler.new(messages, rules: rule_ast)
end

.inherited(klass) ⇒ Object



39
40
41
42
# File 'lib/dry/validation/schema.rb', line 39

def self.inherited(klass)
  super
  klass.setting :options, {}
end

.input_processorObject



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

def self.input_processor
  @input_processor ||=
    begin
      if input_processor_compiler
        input_processor_compiler.(rule_ast)
      else
        NOOP_INPUT_PROCESSOR
      end
    end
end

.input_processor_ast(type) ⇒ Object



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

def self.input_processor_ast(type)
  config.input_processor_map.fetch(type).schema_ast(rule_ast)
end

.input_processor_compilerObject



129
130
131
# File 'lib/dry/validation/schema.rb', line 129

def self.input_processor_compiler
  @input_processor_comp ||= config.input_processor_map[config.input_processor]
end

.messagesObject



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dry/validation/schema.rb', line 83

def self.messages
  default = default_messages

  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

.new(rules = config.rules, **options) ⇒ Object



44
45
46
# File 'lib/dry/validation/schema.rb', line 44

def self.new(rules = config.rules, **options)
  super(rules, default_options.merge(options))
end

.option(name, default = nil) ⇒ Object



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

def self.option(name, default = nil)
  attr_reader(*name)
  options.update(name => default)
end

.optionsObject



79
80
81
# File 'lib/dry/validation/schema.rb', line 79

def self.options
  config.options
end

.predicatesObject



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

def self.predicates
  config.predicates
end

.rule_astObject



133
134
135
# File 'lib/dry/validation/schema.rb', line 133

def self.rule_ast
  @rule_ast ||= config.rules.flat_map(&:rules).map(&:to_ast)
end

.rulesObject



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

def self.rules
  config.rules
end

.to_astObject



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

def self.to_ast
  [:schema, self]
end

Instance Method Details

#[](name) ⇒ Object



184
185
186
187
188
189
190
191
192
# File 'lib/dry/validation/schema.rb', line 184

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

#call(input) ⇒ Object



179
180
181
182
# File 'lib/dry/validation/schema.rb', line 179

def call(input)
  processed_input = input_processor[input]
  Result.new(processed_input, apply(processed_input), error_compiler, hint_compiler)
end

#with(new_options) ⇒ Object



175
176
177
# File 'lib/dry/validation/schema.rb', line 175

def with(new_options)
  self.class.new(self.class.rules, options.merge(new_options))
end