Sha256: 345052b9695d7da2c0cf8fe86320b4b6ab17f1ca7fd1cb3847f9d59fc1de90a0

Contents?: true

Size: 1.17 KB

Versions: 12

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

module LightServiceExt
  class ApplicationContract < Dry::Validation::Contract
    register_macro(:email) do
      key.failure('must be a valid email') unless Regex.match?(:email, value)
    end

    module InstanceMethods
      unless respond_to?(:keys)
        def keys
          return [] if schema.nil?

          schema&.rules&.keys || []
        end
      end

      unless respond_to?(:t)
        def t(key, base_path: "errors", **opts)
          self.class.t(key, base_path: base_path, **opts)
        end
      end
    end

    module ClassMethods
      unless respond_to?(:keys)
        def keys
          return [] if schema.nil?

          schema&.rules&.keys || []
        end
      end

      unless respond_to?(:t)
        def t(key, base_path: "errors", **opts)
          scope = opts[:scope] || ""
          path = [base_path, scope, key.to_s].join(".")

          message = messages.translate(path)
          return message if message.exclude?("%{")

          (message % opts.except(:scope))
        end
      end
    end

    def self.inherited(base)
      super
      base.include InstanceMethods
      base.extend ClassMethods
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
light-service-ext-0.1.11 lib/light-service-ext/application_contract.rb
light-service-ext-0.1.10 lib/light-service-ext/application_contract.rb
light-service-ext-0.1.9 lib/light-service-ext/application_contract.rb
light-service-ext-0.1.8 lib/light-service-ext/application_contract.rb
light-service-ext-0.1.7 lib/light-service-ext/application_contract.rb
light-service-ext-0.1.6 lib/light-service-ext/application_contract.rb
light-service-ext-0.1.5 lib/light-service-ext/application_contract.rb
light-service-ext-0.1.4 lib/light-service-ext/application_contract.rb
light-service-ext-0.1.3 lib/light-service-ext/application_contract.rb
light-service-ext-0.1.2 lib/light-service-ext/application_contract.rb
light-service-ext-0.1.1 lib/light-service-ext/application_contract.rb
light-service-ext-0.1.0 lib/light-service-ext/application_contract.rb