Comments (16)
@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.
@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.
@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.
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.
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.
@juanibiapina you can fix it including predicates in Child class as workaround.
from validations.
@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.
@AlfonsoUceda Thanks. This is not fixed in hanami 1.1.0 right?
from validations.
@BenBach no sorry, but I wanted to fix it maybe for 1.1.1, just I need time :P
from validations.
@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.
I'll take a look this weekend ;)
from validations.
@BenBach Can you please verify? TYVM.
from validations.
@AlfonsoUceda Thanks a lot!!
from validations.
Thanks both to use Hanami, we want to improve hanami validations because it is in our roadmap.
from validations.
Folks, thank you all. I'm closing this. I also documented the usage: hanami/hanami.github.io#418
from validations.
@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)
- [Question] Validation Object receiving key arguments as string or symbol HOT 4
- Make validations a callable object HOT 2
- Fail on incomplete expression? HOT 1
- Error if each macro is used with a single check HOT 2
- Router params treated differently in RSpec HOT 4
- Wrap rule errors by namespace HOT 4
- +key?+ is not a valid predicate name HOT 1
- Unable to use I18n w/o manually requiring dry/validations/messages/i18n
- i18n and predicate are not working well HOT 2
- dry-validation val() doesn't work for predicates HOT 1
- Support for preprocessing? HOT 1
- Add a simple a helper method to shorten validate.success? with valid? [Law of Demeter] HOT 1
- Nested Validations Not Working HOT 2
- validate blocks raise an exception about missing messages HOT 3
- Upgrade to dry-validation v1.0 or remove dependency from Hanami HOT 7
- v2.0.0.alpha1, 2.0 suggestion. HOT 3
- Updating dry-validation ? HOT 3
- hanami-validations is not ruby 3 compatible HOT 3
- Unexpected value returned from `Hanami::Validations::Namespace` HOT 3
- Missing CSRF token makes params invalid in test HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from validations.