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
- #default_schema_for(record, options = {}) ⇒ Object
-
#is_dry_schema?(schema) ⇒ Boolean
rubocop:disable Naming/PredicateName.
- #is_dry_validation_contract?(schema) ⇒ Boolean
-
#need_to_validate?(record, options) ⇒ Boolean
rubocop:enable Layout/LineLength.
-
#pass_record_to_contract?(options) ⇒ Boolean
rubocop:enable Naming/PredicateName.
- #record_key_for_passing_to_contract(options) ⇒ Object
-
#skip_validation?(options, record) ⇒ Boolean
rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
-
#valid_schema?(schema) ⇒ Boolean
rubocop:disable Layout/LineLength.
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, = {}) prefix = .fetch(:schema_prefix, Dry::Validation::Rails.configuration.default_schema_prefix).to_s suffix = .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
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
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
36 37 38 39 40 41 42 43 44 |
# File 'lib/dry/validation/rails/helpers.rb', line 36 def need_to_validate?(record, ) return true if [:on].blank? || [:on] == :all record.new_record? if [:on] == :create record.persisted? if [:on] == :update raise ArgumentError, "Invalid value for :on option: #{[:on]}" end |
#pass_record_to_contract?(options) ⇒ Boolean
rubocop:enable Naming/PredicateName
56 57 58 59 60 61 62 |
# File 'lib/dry/validation/rails/helpers.rb', line 56 def pass_record_to_contract?() value = [: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() return :record if boolean?([:pass_record_to_contract]) .fetch(:pass_record_to_contract, {}).fetch(:as, :record) end |
#skip_validation?(options, record) ⇒ Boolean
rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
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?(, record) if_val = [:if] unless_val = [: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
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 |