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

Validators don't see custom I18n errors #129

Closed
jarosluv opened this issue Jun 6, 2017 · 16 comments
Closed

Validators don't see custom I18n errors #129

jarosluv opened this issue Jun 6, 2017 · 16 comments
Assignees
Labels

Comments

@jarosluv
Copy link

@jarosluv jarosluv commented Jun 6, 2017

There is problem with validation and custom errors.

Please, see gist: https://gist.github.com/jarosluv/b6868e13736fc4e1be1321a933adca92

It's error raised when using Schema in controller, but all is okay when calling Schema directly.

@BenBach

This comment has been minimized.

Copy link

@BenBach BenBach commented Jul 28, 2017

@jarosluv Were you able to solve this? I have the exact same issue. The customer error messages of my customer predicates aren't found

@jarosluv

This comment has been minimized.

Copy link
Author

@jarosluv jarosluv commented Jul 31, 2017

@BenBach

Hello, Ben!

With help of @flash-gordon, it was possible to get workaround:

module Web::Controllers::Campaigns
  class Create
    ...
    params do
      configure do
        config.messages = :i18n
      end

      required(:campaign).schema(CampaignSchema)
    end
    ...
  end
end

CampaignSchema = Dry::Validation.Form do
  required(:title).filled

  validate correct_title?: :title do |title|
    TitleHandler.call(title)
  end
end
@juanibiapina

This comment has been minimized.

Copy link

@juanibiapina juanibiapina commented Aug 2, 2017

I got the same problem with this setup:

module MyPredicates
  self.messages_path = "yaml with message for `whatever` predicate"

  predicate :whatever? do |current|
    current == "whatever"
  end
end

class Parent
  include Hanami::Validations::Form

  validations do
    required(:child).schema(Child)
  end
end

class Child
  include Hanami::Validations::Form

  predicates MyPredicates

  validations do
    required(:field).filled(:whatever?)
  end
end
Dry::Validation::MissingMessageError:
       message for whatever? was not found

The fix then becomes including predicates MyPredicates on the Parent. Even though predicates are not used there, messages seem to be read from that level.

@AlfonsoUceda

This comment has been minimized.

Copy link
Member

@AlfonsoUceda AlfonsoUceda commented Aug 15, 2017

@juanibiapina you can fix it including predicates in Child class as workaround.

@AlfonsoUceda

This comment has been minimized.

Copy link
Member

@AlfonsoUceda AlfonsoUceda commented Aug 15, 2017

@jarosluv thank you for reporting this and sorry for the late answer, we are working in the fix because the workaround is ok but it isn't good if you have to add in every action you use that schema.

@BenBach

This comment has been minimized.

Copy link

@BenBach BenBach commented Oct 26, 2017

@AlfonsoUceda Thanks. This is not fixed in hanami 1.1.0 right?

@AlfonsoUceda

This comment has been minimized.

Copy link
Member

@AlfonsoUceda AlfonsoUceda commented Oct 26, 2017

@BenBach no sorry, but I wanted to fix it maybe for 1.1.1, just I need time :P

@BenBach

This comment has been minimized.

Copy link

@BenBach BenBach commented Oct 26, 2017

@AlfonsoUceda No problem at all.

I am just wondering how to do a workaround:

# frozen_string_literal: true

require 'hanami/validations'

module MyCompany::Predicates
  include Hanami::Validations::Predicates

  self.messages_path = 'config/errors.yml'

  predicate(:uuid?) do |input|
    !/[0-9a-f]{8}-
    [0-9a-f]{4}-
    [0-9a-f]{4}-
    [0-9a-f]{4}-
    [0-9a-f]{12}/x.match(input).nil?
  end
end

module Api::Controllers::Customers
  class Destroy
    include Api::Action


    params do
      predicates MyCompany::Predicates

      required(:id).filled(:str?, :uuid?)
    end
end

Do you maybe have a hint?

My specs fail right now with:

Dry::Validation::MissingMessageError:
       message for uuid? was not found
@AlfonsoUceda

This comment has been minimized.

Copy link
Member

@AlfonsoUceda AlfonsoUceda commented Oct 27, 2017

I'll take a look this weekend ;)

@AlfonsoUceda

This comment has been minimized.

Copy link
Member

@AlfonsoUceda AlfonsoUceda commented Oct 29, 2017

@BenBach Almost hehe, that message is because custom predicates need custom error messages, so you need to create a validator class and use the predicate inside the validator. The problem is because we have to improve that API.

module MyPredicates
  include Hanami::Validations::Predicates

  self.messages_path = 'config/errors.yml'

  predicate(:uuid?) do |input|
    !/[0-9a-f]{8}-
    [0-9a-f]{4}-
    [0-9a-f]{4}-
    [0-9a-f]{4}-
    [0-9a-f]{12}/x.match(input).nil?
  end
end

class CustomValidator
  include Hanami::Validations
  predicates MyPredicates

  validations do
    required(:id).filled(:uuid?)
  end
end

module Web::Controllers::Home
  class Index
    include Web::Action

    params Signup

    def call(params)
      result = params.validate
    end
  end
end

Adapt it to your code and tell me if it works.

@jodosha

This comment has been minimized.

Copy link
Member

@jodosha jodosha commented Nov 20, 2017

@BenBach Can you please verify? TYVM.

@jodosha jodosha added the waiting label Nov 20, 2017
@rodrigopk

This comment has been minimized.

Copy link

@rodrigopk rodrigopk commented Nov 24, 2017

@jodosha @AlfonsoUceda Hi, I work on the same company as @BenBach.
I tested the solution proposed by @AlfonsoUceda, and it works like a charm.
Thanks for the help!

@BenBach

This comment has been minimized.

Copy link

@BenBach BenBach commented Nov 24, 2017

@AlfonsoUceda Thanks a lot!!

@AlfonsoUceda

This comment has been minimized.

Copy link
Member

@AlfonsoUceda AlfonsoUceda commented Nov 24, 2017

Thanks both to use Hanami, we want to improve hanami validations because it is in our roadmap.

@jodosha

This comment has been minimized.

Copy link
Member

@jodosha jodosha commented Nov 27, 2017

Folks, thank you all. I'm closing this. I also documented the usage: hanami/hanami.github.io#418

@jodosha jodosha closed this Nov 27, 2017
@jodosha jodosha added invalid and removed bug waiting labels Nov 27, 2017
@xvonabur

This comment has been minimized.

Copy link

@xvonabur xvonabur commented Jan 7, 2018

@jodosha Hi! Just my two cents. There is still issue with predicates and i18n. config.message_file is working fine, but config.messages = :i18n is not. The only way to use custom predicates with i18n errors is to add

params do
  configure do
    config.messages = :i18n
  end
end

to controller (as mentioned above). I'm on 1.1.0 right now. Is it true for master branch? If so, I can send a PR to hanami docs to mention it, if you wish. I've lost so much time to figure it out, so I want to help others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.