Class: Dry::Validation::Schema

Inherits:
Object
  • Object
show all
Extended by:
Configurable, PredicateRegistry::PredicateDetector, TypeSpecs
Includes:
StructClassBuilder
Defined in:
lib/dry/validation/schema.rb,
lib/dry/validation/schema/dsl.rb,
lib/dry/validation/schema/key.rb,
lib/dry/validation/schema/rule.rb,
lib/dry/validation/schema/check.rb,
lib/dry/validation/schema/value.rb,
lib/dry/validation/extensions/struct.rb,
lib/dry/validation/schema/deprecated.rb,
lib/dry/validation/schema/class_interface.rb

Direct Known Subclasses

JSON, Params

Defined Under Namespace

Modules: StructClassBuilder, StructNode Classes: Check, DSL, JSON, Key, Params, Rule, Value

Constant Summary collapse

Form =
Schema::Params
NOOP_INPUT_PROCESSOR =
-> input { input }
DEFAULT_PROCESSOR_MAP =
{
  sanitizer: InputProcessorCompiler::Sanitizer.new,
  json: InputProcessorCompiler::JSON.new,
  params: InputProcessorCompiler::Params.new,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TypeSpecs

build_array_type, build_hash_type, build_sum_type, build_type_map, extended, lookup_type, resolve_spec

Methods included from PredicateRegistry::PredicateDetector

method_added

Methods included from StructClassBuilder

#create_class

Constructor Details

#initialize(rules, options) ⇒ Schema

Returns a new instance of Schema.



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

def initialize(rules, options)
  @type_map = self.class.type_map
  @config = self.class.config
  @predicates = options.fetch(:predicate_registry).bind(self)
  @rule_compiler = SchemaCompiler.new(predicates, options)
  @message_compiler = options.fetch(:message_compiler)
  @input_processor = options[:input_processor]

  @input_rule = rule_compiler.visit(config.input_rule.(predicates)) if config.input_rule

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

  @executor = Executor.new do |steps|
    steps << ProcessInput.new(input_processor) if input_processor
    steps << ApplyInputRule.new(input_rule) if input_rule
    steps << ApplyRules.new(@rules)
    steps << ApplyChecks.new(@checks) if @checks.any?
  end

  freeze
end

Instance Attribute Details

#checksObject (readonly)

Returns the value of attribute checks.



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

def checks
  @checks
end

#configObject (readonly)

Returns the value of attribute config.



21
22
23
# File 'lib/dry/validation/schema.rb', line 21

def config
  @config
end

#executorObject (readonly)

Returns the value of attribute executor.



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

def executor
  @executor
end

#input_processorObject (readonly)

Returns the value of attribute input_processor.



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

def input_processor
  @input_processor
end

#input_ruleObject (readonly)

Returns the value of attribute input_rule.



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

def input_rule
  @input_rule
end

#message_compilerObject (readonly)

Returns the value of attribute message_compiler.



35
36
37
# File 'lib/dry/validation/schema.rb', line 35

def message_compiler
  @message_compiler
end

#optionsObject (readonly)

Returns the value of attribute options.



37
38
39
# File 'lib/dry/validation/schema.rb', line 37

def options
  @options
end

#predicatesObject (readonly)

Returns the value of attribute predicates.



29
30
31
# File 'lib/dry/validation/schema.rb', line 29

def predicates
  @predicates
end

#rule_compilerObject (readonly)

Returns the value of attribute rule_compiler.



33
34
35
# File 'lib/dry/validation/schema.rb', line 33

def rule_compiler
  @rule_compiler
end

#rulesObject (readonly)

Returns the value of attribute rules.



25
26
27
# File 'lib/dry/validation/schema.rb', line 25

def rules
  @rules
end

#type_mapObject (readonly)

Returns the value of attribute type_map.



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

def type_map
  @type_map
end

Class Method Details

.cloneObject



107
108
109
110
111
112
# File 'lib/dry/validation/schema/class_interface.rb', line 107

def self.clone
  klass = Class.new(self)
  klass.config.rules = []
  klass.config.registry = registry
  klass
end

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/dry/validation/schema/class_interface.rb', line 91

def self.create_class(target, other = nil, &block)
  klass =
    if other.is_a?(self)
      Class.new(other.class)
    elsif other.respond_to?(:schema) && other.schema.is_a?(self)
      Class.new(other.schema.class)
    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



148
149
150
151
152
153
154
155
# File 'lib/dry/validation/schema/class_interface.rb', line 148

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



165
166
167
168
169
170
# File 'lib/dry/validation/schema/class_interface.rb', line 165

def self.default_options
  @default_options ||= { predicate_registry: registry,
    message_compiler: message_compiler,
    input_processor: input_processor,
    checks: config.checks }
end

.define(options = {}, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dry/validation/schema/class_interface.rb', line 41

def self.define(options = {}, &block)
  source = options.fetch(:schema_class)
  config = source.config

  dsl_ext = config.dsl_extensions

  options = options.merge(rules: options[:rules].dup) if options.key?(:rules)
  dsl = Schema::Value.new(options.merge(registry: source.registry))
  dsl_ext.__send__(:extend_object, dsl) if dsl_ext
  dsl.predicates(options[:predicates]) if options.key?(:predicates)
  dsl.instance_exec(&block) if block

  target = dsl.schema_class

  if config.input
    config.input_rule = -> predicates {
      Schema::Value.new(registry: predicates).infer_predicates(Array(target.config.input)).to_ast
    }
  end

  rules = target.config.rules + (options.fetch(:rules, []) + dsl.rules)

  target.configure do |cfg|
    cfg.rules = rules
    cfg.checks = cfg.checks + dsl.checks
    cfg.path = dsl.path
    cfg.type_map = target.build_type_map(dsl.type_map) if cfg.type_specs
  end

  target
end

.define!(options = {}, &block) ⇒ Object



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

def self.define!(options = {}, &block)
  define(schema_class: self, &block)
end

.inherited(klass) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
# File 'lib/dry/validation/schema/class_interface.rb', line 172

def self.inherited(klass)
  super

  klass.config.options = klass.config.options.dup

  if registry && self != Schema
    klass.config.registry = registry.new(self)
  else
    klass.set_registry!
  end
end

.input_processorObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/dry/validation/schema/deprecated.rb', line 6

def self.input_processor
  @input_processor ||=
    begin
      if type_map.is_a?(Dry::Types::Safe) && config.input_processor != :noop
        type_map
      elsif type_map.size > 0 && config.input_processor != :noop
        build_hash_type(type_map)
      elsif input_processor_compiler
        input_processor_compiler.(rule_ast)
      else
        NOOP_INPUT_PROCESSOR
      end
    end
end

.input_processor_ast(type) ⇒ Object



21
22
23
# File 'lib/dry/validation/schema/deprecated.rb', line 21

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

.input_processor_compilerObject



25
26
27
# File 'lib/dry/validation/schema/deprecated.rb', line 25

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

.message_compilerObject



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

def self.message_compiler
  @message_compiler ||= MessageCompiler.new(messages)
end

.messagesObject



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/dry/validation/schema/class_interface.rb', line 134

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



37
38
39
# File 'lib/dry/validation/schema/class_interface.rb', line 37

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

.option(name, default = nil) ⇒ Object



86
87
88
89
# File 'lib/dry/validation/schema/class_interface.rb', line 86

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

.optionsObject



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

def self.options
  config.options
end

.predicates(predicate_set = nil) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/dry/validation/schema/class_interface.rb', line 77

def self.predicates(predicate_set = nil)
  if predicate_set
    config.predicates = predicate_set
    set_registry!
  else
    config.predicates
  end
end

.registryObject



118
119
120
# File 'lib/dry/validation/schema/class_interface.rb', line 118

def self.registry
  config.registry
end

.rule_astObject



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

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

.rulesObject



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

def self.rules
  config.rules
end

.set_registry!Object



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

def self.set_registry!
  config.registry = PredicateRegistry[config.predicates]
end

.to_astObject



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

def self.to_ast
  [:schema, self]
end

.type_mapObject



122
123
124
# File 'lib/dry/validation/schema/class_interface.rb', line 122

def self.type_map
  config.type_map
end

Instance Method Details

#arityObject



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

def arity
  1
end

#astObject Also known as: to_ast



88
89
90
# File 'lib/dry/validation/schema.rb', line 88

def ast(*)
  self.class.to_ast
end

#call(input) ⇒ Object



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

def call(input)
  output, result = executor.(input)
  Result.new(output, result, message_compiler, config.path)
end

#curry(*curry_args) ⇒ Object



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

def curry(*curry_args)
  -> *args { call(*(curry_args + args)) }
end

#to_procObject



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

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

#with(new_options) ⇒ Object



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

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