Class: Dry::Validation::Rails::Validator

Inherits:
ActiveModel::Validator
  • Object
show all
Includes:
Helpers
Defined in:
lib/dry/validation/rails/validator.rb

Constant Summary

Constants included from Helpers

Helpers::SUPPORTED_VALIDATORS

Instance Method Summary collapse

Methods included from Helpers

#default_schema_for, #is_dry_schema?, #is_dry_validation_contract?, #need_to_validate?, #pass_record_to_contract?, #record_key_for_passing_to_contract, #skip_validation?, #valid_schema?

Instance Method Details

#validate(record) ⇒ Object



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

def validate(record)
  return if skip_validation?(options, record)

  schema = options.fetch(:schema, default_schema_for(record, options))

  unless valid_schema?(schema)
    raise ArgumentError,
          'Schema must be a Dry::Schema::Params or Dry::Schema::JSON or Dry::Validation::Contract'
  end

  validate_schema(record, schema, options) if need_to_validate?(record, options)
end

#validate_schema(record, schema, options) ⇒ Object



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

def validate_schema(record, schema, options)
  if is_dry_validation_contract?(schema)
    validate_dry_validation_contract(record, schema, options)
  elsif is_dry_schema?(schema)
    validate_dry_schema(record, schema, options)
  else
    raise ArgumentError,
          'Schema must be a Dry::Schema::Params or Dry::Schema::JSON or Dry::Validation::Contract'
  end
end