Failure to load Dry::Schema::MessageCompiler inside a module

104 Views Asked by At

I am having the following problem when using the dry-schema gem.

When I call Dry::Schema.Params in my console, the constant loads normally, as shown in the image:

enter image description here

However, I am trying to call it within a module, and in this module, the following error message appears. (On this example, trying to call the constant on the initialize method, but it happens anywhere I call)

enter image description here

This is the full file code, just in case:

module Validators
  module Adapters
    class DrySchemaValidatorAdapter
      attr_reader :entity

      def initialize(entity:)
        @entity = entity
      end

      def validate(params:, caller_method:)
        validation_schema = schema(caller_method:).call(params.to_h)
        Structs::ValidationResponseStruct.new(!validation.failure?, validation.errors.to_h)
      end

      private

      def schema(caller_method:)
        "Schemas::#{entity}::#{caller_method.upcase}".constantize
      end
    end
  end
end

This line "Schemas::#{entity}::#{caller_method.upcase}".constantize should return a Dry::Schema.Params block, and the 'constantization' works as intended, just so you know what I'm doing here.

I have already tried using require, but it didn't help.

Thank you in advance.

I've tried to use require, without success.

What I want: To be able to use the gem inside modules/classes.

UPDATE: Found a solution

I've extended the module with the schemas during the adapter instantiation, which seems to solve the problem.

def initialize(entity:)
    @entity = entity
    extend "Schemas::#{entity}".constantize
end

I don't know why it solves the problem nor why the problem occurred in the first place, however.

1

There are 1 best solutions below

0
On

UPDATE: Found a solution

I've extended the module with the schemas during the adapter instantiation, which seems to solve the problem.

def initialize(entity:)
    @entity = entity
    extend "Schemas::#{entity}".constantize
end

I don't know why it solves the problem nor why the problem occurred in the first place, however.