-
Notifications
You must be signed in to change notification settings - Fork 14
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
Enrich error messages #10
Comments
@francesconi Thanks for your attention!
I'm wondering when a custom error (except the message itself), in practice, is needed while doing validation? Could you give me more details? |
Let's say you have a custom error with a dynamic string
you would be able to match it with
And you could still use How would you achieve this with the current implementation? I know this is a bit more than just validating but IMHO this approach would open the door to a lot more usecases. |
Thanks for the details! How will |
Based on the previous example something like this (untested)
|
If it's just a matter of dynamic message strings, I guess maybe we can do something like this: [play] package main
import (
"fmt"
v "github.com/RussellLuo/validating/v3"
)
type Foo struct {
File string
}
func (f *Foo) Schema(whitelist []string) v.Schema {
return v.Schema{
v.F("file", f.File): v.In(whitelist...).Msg(fmt.Sprintf("file %q not found", f.File)),
}
}
func main() {
whitelist := []string{"a", "b", "c"}
f := Foo{}
errs := v.Validate(f.Schema(whitelist))
fmt.Printf("errs: %#v\n", errs)
// Output:
// errs: validating.Errors{validating.errorImpl{field:"file", kind:"INVALID", message:"file \"\" not found"}}
} |
The whole point about this is matching an error with a dynamic string
Again, how would you achieve this with just a dynamic message string? |
Actually, what I'm trying to understand is why distinguishing different errors matters, in the scenario of data validation, if all of errors essentially are of the same kind (e.g. INVLIAD means the field is invalid). In other words, does |
I've just had a look into Google API's Error Design about error details and I must admit that they would work fine for me 👍 |
Thanks for your feedback! Adding error details seems good, but will also introduce complexity. For a better understanding of your problem, could you please expand the part |
A client sends us availabilities for it's hotel rooms. When a certain room does not exists on our side
We do communicate with the OTA standard and my errors must have a code and a message accordingly ERR. |
Let me try to construct the validation flow at the server side:
|
Would be enough if one room is missing but if more rooms are missing i'll get duplicate error messages
|
If the reaction to any error is to request the client to resend all rooms, I guess the whole operation will be idempotent, i.e., the result will be the same no matter how many rooms are missing, right? As for the duplicate error messages, what's the corresponding schema definition? Does the method #10 (comment) help in this scenario? |
The only solution I see for now is using a static error for |
Per the literal description, the problem seems to be the duplicate static messages, which I think should be resolved by the method mentioned in #10 (comment). If it's not the case, I think I still didn't catch the point. Is there any minimal (and runnable) code that can reproduce the crux of the problem? I think it will be easier to find out the problem with the code, and to figure out the solution :) |
Any chance you could extend
MessageValidator
with a function where I can set a custom error instead of just a string? So in addition to.Msg(s string)
something like.Err(err error)
? My errors contain additional information which would allow me further processing after validation.The text was updated successfully, but these errors were encountered: