Class: SmartCore::Validator::Contract

Inherits:
Object
  • Object
show all
Includes:
Initializer, SmartCore::Validator::ClassState::InitializeMixin
Defined in:
lib/smart_core/validator/contract.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SmartCore::Validator::ClassState::InitializeMixin

add_delegators_to, included

Class Method Details

.call(data_to_validate, **kwargs) ⇒ Object

NOTE: In Ruby 2.6.x need to initialize instance without arguments if they not expected.



19
20
21
22
# File 'lib/smart_core/validator/contract.rb', line 19

def call(data_to_validate, **kwargs)
  instance = kwargs.empty? ? new : new(**kwargs)
  instance.call(data_to_validate)
end

.inherited(subclass) ⇒ Object



9
10
11
12
# File 'lib/smart_core/validator/contract.rb', line 9

def inherited(subclass)
  super
  subclass.__state_container__ = __state_container__.dup
end

.paramObject

Raises:

  • (NotImplementedError)


14
15
16
# File 'lib/smart_core/validator/contract.rb', line 14

def param(*)
  raise NotImplementedError, 'params are not permitted to use in validators'
end

.rule(checked_value, &block) ⇒ Object

TODO: Delegate all this logic to the Rules.



29
30
31
# File 'lib/smart_core/validator/contract.rb', line 29

def rule(checked_value, &block)
  rules << Rule.new(checked_value, block)
end

.rule_for_each(*checked_values, &block) ⇒ Object



33
34
35
# File 'lib/smart_core/validator/contract.rb', line 33

def rule_for_each(*checked_values, &block)
  checked_values.each { |checked_value| rule(checked_value, &block) }
end

.schema(&block) ⇒ Object



24
25
26
# File 'lib/smart_core/validator/contract.rb', line 24

def schema(&block)
  schema_class.schema(&block)
end

Instance Method Details

#call(data_to_validate) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/smart_core/validator/contract.rb', line 38

def call(data_to_validate)
  self.data = Utils.deeply_symbolize_freeze(data_to_validate)
  self.errors_controller = ErrorsController.resolve_from(settings.errors_managing_type)
  self.rule_execution_context = build_rule_execution_context

  result = build_schema_instance.validate(data)
  errors_controller.process_smart_schema_result!(result)
  return build_result if errors_controller.validation_fails?

  check_with_rules!
  build_result
end