Sha256: 8924712c82439d49f60616989fbd580ad7aaf6f7dacd97261ded25c1d17eb83d

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

# Adds ::validates and friends, and #valid? to the object.
# This is completely form-independent.
module Reform::Validation
  module ClassMethods
    def validation_groups
      @groups ||= Groups.new(validation_group_class)
    end

    # DSL.
    def validation(name=nil, options={}, &block)
      options = deprecate_validation_positional_args(name, options)
      name    = options[:name] # TODO: remove in favor of kw args in 3.0.

      heritage.record(:validation, options, &block)
      group = validation_groups.add(name, options)

      group.instance_exec(&block)
    end

    def deprecate_validation_positional_args(name, options)
      if name.is_a?(Symbol)
        warn "[Reform] Form::validation API is now: validation(name: :default, if:nil, schema:Schema). Please use keyword arguments instead of positional arguments."
        return { name: name }.merge(options)
      end

      if name.nil?
        return { name: :default }.merge(options)
      end

      { name: :default }.merge(name)
    end

    def validation_group_class
      raise NoValidationLibraryError, 'no validation library loaded. Please include a ' +
                            'validation library such as Reform::Form::Dry'
    end
  end

  def self.included(includer)
    includer.extend(ClassMethods)
  end

  NoValidationLibraryError = Class.new(RuntimeError)
end

require "reform/validation/groups"

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reform-2.3.0.rc1 lib/reform/validation.rb