Class: Karafka::Core::Contractable::Contract
- Inherits:
-
Object
- Object
- Karafka::Core::Contractable::Contract
- Extended by:
- Karafka::Core::Configurable
- Defined in:
- lib/karafka/core/contractable/contract.rb
Overview
Note:
This contract does NOT support rules inheritance as it was never needed in Karafka
Base contract for all the contracts that check data format
Class Attribute Summary collapse
-
.rules ⇒ Array<Rule>
readonly
All the validation rules defined for a given contract.
Class Method Summary collapse
-
.nested(path, &block) ⇒ Object
Allows for definition of a scope/namespace for nested validations.
- .optional(*keys, &block) ⇒ Object
-
.required(*keys, &block) ⇒ Object
Defines a rule for a required field (required means, that will automatically create an error if missing).
- .virtual(&block) ⇒ Object
Instance Method Summary collapse
-
#call(data) ⇒ Result
Runs the validation.
-
#validate!(data, error_class) ⇒ Boolean
True.
Methods included from Karafka::Core::Configurable
Class Attribute Details
.rules ⇒ Array<Rule> (readonly)
Returns all the validation rules defined for a given contract.
25 26 27 |
# File 'lib/karafka/core/contractable/contract.rb', line 25 def rules @rules end |
Class Method Details
.nested(path, &block) ⇒ Object
Allows for definition of a scope/namespace for nested validations
36 37 38 39 40 41 |
# File 'lib/karafka/core/contractable/contract.rb', line 36 def nested(path, &block) init_accu @nested << path instance_eval(&block) @nested.pop end |
.optional(*keys, &block) ⇒ Object
55 56 57 58 |
# File 'lib/karafka/core/contractable/contract.rb', line 55 def optional(*keys, &block) init_accu @rules << Rule.new(@nested + keys, :optional, block).freeze end |
.required(*keys, &block) ⇒ Object
Defines a rule for a required field (required means, that will automatically create an error if missing)
48 49 50 51 |
# File 'lib/karafka/core/contractable/contract.rb', line 48 def required(*keys, &block) init_accu @rules << Rule.new(@nested + keys, :required, block).freeze end |
.virtual(&block) ⇒ Object
Note:
Virtual rules have different result expectations. Please see contracts or specs for details.
64 65 66 67 |
# File 'lib/karafka/core/contractable/contract.rb', line 64 def virtual(&block) init_accu @rules << Rule.new([], :virtual, block).freeze end |
Instance Method Details
#call(data) ⇒ Result
Runs the validation
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/karafka/core/contractable/contract.rb', line 82 def call(data) errors = [] self.class.rules.each do |rule| case rule.type when :required validate_required(data, rule, errors) when :optional validate_optional(data, rule, errors) when :virtual validate_virtual(data, rule, errors) end end Result.new(errors, self) end |
#validate!(data, error_class) ⇒ Boolean
Returns true.
104 105 106 107 108 109 110 |
# File 'lib/karafka/core/contractable/contract.rb', line 104 def validate!(data, error_class) result = call(data) return true if result.success? raise error_class, result.errors end |