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, JSON

Defined Under Namespace

Classes: Attr, Check, DSL, Form, JSON, 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.



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/dry/validation/schema.rb', line 166

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.



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

def checks
  @checks
end

#error_compilerObject (readonly)

Returns the value of attribute error_compiler.



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

def error_compiler
  @error_compiler
end

#hint_compilerObject (readonly)

Returns the value of attribute hint_compiler.



162
163
164
# File 'lib/dry/validation/schema.rb', line 162

def hint_compiler
  @hint_compiler
end

#input_processorObject (readonly)

Returns the value of attribute input_processor.



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

def input_processor
  @input_processor
end

#optionsObject (readonly)

Returns the value of attribute options.



164
165
166
# File 'lib/dry/validation/schema.rb', line 164

def options
  @options
end

#predicatesObject (readonly)

Returns the value of attribute predicates.



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

def predicates
  @predicates
end

#rule_compilerObject (readonly)

Returns the value of attribute rule_compiler.



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

def rule_compiler
  @rule_compiler
end

#rulesObject (readonly)

Returns the value of attribute rules.



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

def rules
  @rules
end

Class Method Details

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



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

def self.create_class(target, other = nil, &block)
  klass =
    if other.is_a?(self)
      Class.new(other.class)
    elsif other.is_a?(Class) && other < Types::Struct
      Validation.Schema(parent: target, build: false) do
        other.schema.each { |attr, type| key(attr).required(type) }
      end
    else
      Validation.Schema(target.schema_class, parent: target, build: false, &block)
    end

  klass.config.path = target.path if other
  klass.config.input_processor = :noop

  klass
end

.default_messagesObject



102
103
104
105
106
107
108
109
# File 'lib/dry/validation/schema.rb', line 102

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



142
143
144
145
146
147
148
# File 'lib/dry/validation/schema.rb', line 142

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

.error_compilerObject



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

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

.hint_compilerObject



115
116
117
# File 'lib/dry/validation/schema.rb', line 115

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

.inherited(klass) ⇒ Object



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

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

.input_processorObject



119
120
121
122
123
124
125
126
127
128
# File 'lib/dry/validation/schema.rb', line 119

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



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

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

.input_processor_compilerObject



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

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

.messagesObject



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dry/validation/schema.rb', line 88

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



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

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

.option(name, default = nil) ⇒ Object



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

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

.optionsObject



84
85
86
# File 'lib/dry/validation/schema.rb', line 84

def self.options
  config.options
end

.predicatesObject



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

def self.predicates
  config.predicates
end

.rule_astObject



138
139
140
# File 'lib/dry/validation/schema.rb', line 138

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

.rulesObject



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

def self.rules
  config.rules
end

.to_astObject



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

def self.to_ast
  [:schema, self]
end

Instance Method Details

#[](name) ⇒ Object



189
190
191
192
193
194
195
196
197
# File 'lib/dry/validation/schema.rb', line 189

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



184
185
186
187
# File 'lib/dry/validation/schema.rb', line 184

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

#curry(*args) ⇒ Object



199
200
201
# File 'lib/dry/validation/schema.rb', line 199

def curry(*args)
  to_proc.curry.(*args)
end

#to_procObject



203
204
205
# File 'lib/dry/validation/schema.rb', line 203

def to_proc
  -> input { self.call(input) }
end

#with(new_options) ⇒ Object



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

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