Sha256: 9e3648860e593cadc1bb7a40c7eb2adcb173a21803d1fad656508b0efe2d6a0d

Contents?: true

Size: 1.88 KB

Versions: 5

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

require "pakyow/support/class_state"
require "pakyow/support/extension"

require "pakyow/errors"

module Pakyow
  class Validator
    class Result
      def initialize(key)
        @key = key
        @errors = []
      end

      def error(validation, options)
        @errors << [validation, options]
      end

      def valid?
        @errors.empty?
      end

      def messages(type: :default)
        @errors.map { |validation, options|
          Verifier.formatted_message(
            (options[:message] || validation.message(**options)),
            type: type, key: @key
          )
        }
      end
    end

    extend Support::ClassState
    class_state :validation_objects, default: {}

    class << self
      def register_validation(validation_object, validation_name)
        @validation_objects[validation_name] = validation_object
      end

      # @api private
      def validation_object_for(validation)
        @validation_objects[validation] || raise(
          UnknownValidationError.new_with_message(validation: validation)
        )
      end
    end

    require "pakyow/validations"

    def initialize(key = nil, &block)
      @key = key
      @validations = []

      if block
        instance_eval(&block)
      end
    end

    def any?
      @validations.any?
    end

    def validate(validation_name = nil, **options)
      validation_object = if block_given?
        Validations::Inline.new(validation_name, Proc.new)
      else
        self.class.validation_object_for(validation_name)
      end

      @validations << [validation_object, options]
    end

    def call(values, context: nil)
      result = Result.new(@key)

      @validations.each do |validation, options|
        unless validation.valid?(values, key: @key, context: context, **options)
          result.error(validation, options)
        end
      end

      result
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pakyow-core-1.0.2 lib/pakyow/validator.rb
pakyow-core-1.0.1 lib/pakyow/validator.rb
pakyow-core-1.0.0 lib/pakyow/validator.rb
pakyow-core-1.0.0.rc5 lib/pakyow/validator.rb
pakyow-core-1.0.0.rc4 lib/pakyow/validator.rb