Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected value returned from Hanami::Validations::Namespace #223

Open
epaew opened this issue Oct 6, 2022 · 3 comments
Open

Unexpected value returned from Hanami::Validations::Namespace #223

epaew opened this issue Oct 6, 2022 · 3 comments

Comments

@epaew
Copy link

epaew commented Oct 6, 2022

Environments

  • CRuby 2.7.6
  • hanami-validations 1.3.9

Issue detail

When the validator class is defined in the module with name includes Validator, Hanami::Validations::Namespace returns unexpected value.

require 'hanami/validations'

module Validators
  class UserValidator
    include Hanami::Validations

    messages :i18n

    validations do
      required(:name) { filled? & str? }
    end
  end
end

puts Validators::UserValidator.namespace.to_s #=> s.user_validator

Expected

puts Validators::UserValidator.namespace.to_s #=> validators.user

Motivation

I'd like to define custom error messages for this validator, like below.

# config/locales/errors.en.yml
en:
  errors:
    rules:
      validators:
        user:
          rules:
            name:
              filled?: "must be non empty string"
              str?: "must be non empty string"

How to escape

1. define namespace manually

https://guides.hanamirb.org/v1.3/validations/advanced-usage/#custom-namespace

require 'hanami/validations'

module Validators
  class UserValidator
    include Hanami::Validations

    namespace :'validators/user'
  end
end

2. apply monkey patch (for Ruby >= 2.5.0)

require 'hanami/validations'

module Hanami
  module Validations
    class Namespace
      private

      def name_without_suffix
        @name.delete_suffix(SUFFIX)
      end
    end
  end
end

module Validators
  class UserValidator
    include Hanami::Validations
  end
end

puts Validators::UserValidator.namespace.to_s #=> validators.user

Now hanami-validations gem supports Ruby >= 2.3.0, so I give up to create pull request :(

@jodosha
Copy link
Member

jodosha commented Oct 24, 2022

@epaew What's the expected value for you?

@jodosha jodosha self-assigned this Oct 24, 2022
@epaew
Copy link
Author

epaew commented Oct 24, 2022

@jodosha

The namespace value I expected for Validators::UserValidator class is "validators.user".

I updated the detail of this issue.

@makketagg
Copy link

@jodosha is the issue still have?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants