Module: Dry::Validation::Rails::Helpers

Included in:
Validator
Defined in:
lib/dry/validation/rails/helpers.rb

Constant Summary collapse

SUPPORTED_VALIDATORS =
[
  Dry::Validation::Contract,
  Dry::Schema.Params,
  Dry::Schema::JSON
].freeze

Instance Method Summary collapse

Instance Method Details

#default_schema_for(record, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/dry/validation/rails/helpers.rb', line 13

def default_schema_for(record, options = {})
  prefix = options.fetch(:schema_prefix, Dry::Validation::Rails.configuration.default_schema_prefix).to_s
  suffix = options.fetch(:schema_suffix, Dry::Validation::Rails.configuration.default_schema_suffix).to_s

  begin
    "#{prefix}#{record.class.name}#{suffix}".classify.constantize
  rescue StandardError
    raise Dry::Validation::Rails::SchemaNotFound, "Schema not found for #{record.class.name}"
  end
end

#is_dry_schema?(schema) ⇒ Boolean

rubocop:disable Naming/PredicateName

Returns:

  • (Boolean)


47
48
49
# File 'lib/dry/validation/rails/helpers.rb', line 47

def is_dry_schema?(schema)
  schema.is_a?(Dry::Schema::Params) || schema.is_a?(Dry::Schema::JSON)
end

#is_dry_validation_contract?(schema) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/dry/validation/rails/helpers.rb', line 51

def is_dry_validation_contract?(schema)
  schema.respond_to?(:ancestors) && schema.ancestors.include?(Dry::Validation::Contract)
end

#need_to_validate?(record, options) ⇒ Boolean

rubocop:enable Layout/LineLength

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
# File 'lib/dry/validation/rails/helpers.rb', line 36

def need_to_validate?(record, options)
  return true if options[:on].blank? || options[:on] == :all

  record.new_record? if options[:on] == :create

  record.persisted? if options[:on] == :update

  raise ArgumentError, "Invalid value for :on option: #{options[:on]}"
end

#pass_record_to_contract?(options) ⇒ Boolean

rubocop:enable Naming/PredicateName

Returns:

  • (Boolean)


56
57
58
59
60
61
62
# File 'lib/dry/validation/rails/helpers.rb', line 56

def pass_record_to_contract?(options)
  value = options[:pass_record_to_contract]

  return true if boolean?(value) || value.is_a?(Hash)

  false
end

#record_key_for_passing_to_contract(options) ⇒ Object



64
65
66
67
68
# File 'lib/dry/validation/rails/helpers.rb', line 64

def record_key_for_passing_to_contract(options)
  return :record if boolean?(options[:pass_record_to_contract])

  options.fetch(:pass_record_to_contract, {}).fetch(:as, :record)
end

#skip_validation?(options, record) ⇒ Boolean

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dry/validation/rails/helpers.rb', line 71

def skip_validation?(options, record)
  if_val = options[:if]
  unless_val = options[:unless]

  if if_val.present?
    return false unless if_val.is_a?(Symbol) || if_val.is_a?(Proc)
    return false if if_val.is_a?(Symbol) && record.respond_to?(if_val) && record.send(if_val)
    return false if if_val.is_a?(Proc) && if_val.call(record)
  end

  if unless_val.present?
    return false unless unless_val.is_a?(Symbol) || unless_val.is_a?(Proc)
    return false if unless_val.is_a?(Symbol) && record.respond_to?(unless_val) && !record.send(unless_val)
    return false if unless_val.is_a?(Proc) && !unless_val.call(record)
  end

  false
end

#valid_schema?(schema) ⇒ Boolean

rubocop:disable Layout/LineLength

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
# File 'lib/dry/validation/rails/helpers.rb', line 25

def valid_schema?(schema)
  if schema.respond_to?(:ancestors)
    return schema.ancestors.any? do |ancestor|
             SUPPORTED_VALIDATORS.include?(ancestor)
           end
  end

  schema.is_a?(Dry::Validation::Contract) || schema.is_a?(Dry::Schema::Params) || schema.is_a?(Dry::Schema::JSON)
end