Class: Operations::Contract

Inherits:
Dry::Validation::Contract
  • Object
show all
Defined in:
lib/operations/contract.rb

Overview

Just a base contract with i18n set up and a bunch of useful macro.

Defined Under Namespace

Classes: MessagesResolver

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ensure_presence(context_key, field: "#{context_key}_id", optional: false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/operations/contract.rb', line 24

def self.ensure_presence(context_key, field: "#{context_key}_id", optional: false)
  rule do |context:|
    next if context[context_key] || schema_error?(field)

    if key?(field)
      key(field).failure(:not_found)
    elsif !optional
      key(field).failure(:key?)
    end
  end
end

.inherited(subclass) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/operations/contract.rb', line 10

def self.inherited(subclass)
  super

  return unless subclass.name

  namespace = subclass.name.underscore.split("/")[0..-2].join("/")
  subclass.config.messages.namespace = namespace
end

.prepend_ruleObject



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

def self.prepend_rule(...)
  rule(...)
  rules.unshift(rules.pop)
end

Instance Method Details

#call(input, **initial_context) ⇒ Object



36
37
38
# File 'lib/operations/contract.rb', line 36

def call(input, **initial_context)
  super(input, initial_context)
end