GithubHelp home page GithubHelp logo

Comments (16)

AlfonsoUceda avatar AlfonsoUceda commented on May 29, 2024 2

@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.

from validations.

rodrigopk avatar rodrigopk commented on May 29, 2024 2

@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!

from validations.

BenBach avatar BenBach commented on May 29, 2024

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

from validations.

jarosluv avatar jarosluv commented on May 29, 2024

@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

from validations.

juanibiapina avatar juanibiapina commented on May 29, 2024

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.

from validations.

AlfonsoUceda avatar AlfonsoUceda commented on May 29, 2024

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

from validations.

AlfonsoUceda avatar AlfonsoUceda commented on May 29, 2024

@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.

from validations.

BenBach avatar BenBach commented on May 29, 2024

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

from validations.

AlfonsoUceda avatar AlfonsoUceda commented on May 29, 2024

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

from validations.

BenBach avatar BenBach commented on May 29, 2024

@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

from validations.

AlfonsoUceda avatar AlfonsoUceda commented on May 29, 2024

I'll take a look this weekend ;)

from validations.

jodosha avatar jodosha commented on May 29, 2024

@BenBach Can you please verify? TYVM.

from validations.

BenBach avatar BenBach commented on May 29, 2024

@AlfonsoUceda Thanks a lot!!

from validations.

AlfonsoUceda avatar AlfonsoUceda commented on May 29, 2024

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

from validations.

jodosha avatar jodosha commented on May 29, 2024

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

from validations.

xvonabur avatar xvonabur commented on May 29, 2024

@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.

from validations.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.