-
Notifications
You must be signed in to change notification settings - Fork 30
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
Improve ToString
implementation on garde::Errors
#26
Comments
It's already possible to do this via #[derive(Debug, Serialize, Deserialize, Validate)]
struct Person {
#[garde(ascii, length(min = 3, max = 25))]
username: String,
#[garde(length(min = 15))]
password: String,
}
let value = Person { username: "a".into(), password: "b".into() };
match value.validate(&()) {
Ok(()) => {}
Err(e) => {
println!("{:?}", e.flatten());
}
} As for implementing |
But thats the debug impl, which contains information about the underling implementation. I don't feel confident exposing that kind of information to the public
I think the best would be to emulate the output of other validation libraries, such as zod. Flatten is what zod uses // Deno source code
import { z } from "https://deno.land/x/zod/mod.ts";
const scheme = const scheme = z.object({
username: z.string(),
age: z.number().min(18)
})
scheme.safeParse({username: 1, age: 10})
/*
{
success: false,
error: ZodError: [
{
"code": "invalid_type",
"expected": "string",
"received": "number",
"path": [
"username"
],
"message": "Expected string, received number"
},
{
"code": "too_small",
"minimum": 18,
"type": "number",
"inclusive": true,
"exact": false,
"message": "Number must be greater than or equal to 18",
"path": [
"age"
]
}
]
at handleResult (https://deno.land/x/[email protected]/types.ts:94:19)
at ZodObject.safeParse (https://deno.land/x/[email protected]/types.ts:231:12)
at <anonymous>:2:8
}
*/ |
That was just an example. errors.flatten().into_iter().map(|(p, e)| format!("{p}: {e}")).collect().join('\n') Does that not work for you?
That does look like flattened output. I think this ties into #7 and #3 a bit. Ideally each rule would have a unique (machine-readable) error code, and you'd be able to provide your own, just like you could provide a custom error message. I don't know if it's best to serialize the constraint parameters (e.g. the |
It does. Maybe we could add this to the current
Maybe an enum, like [
{
// ...
"metadata": {
"kind": "length",
"min": 10
}
},
// ...
] |
Looks great. Thanks! |
Description
Current
ToString
implementation lacks a lot of details.Current output
For example, consider the following scheme:
When fed with the following payload (JSON format), the current implementation only shows the first error message, instead of listing all errors
Expected behaviour
List all errors on some human readable form, like this
Also, provide a
serde::Serialize
implementation so we could provide JSON responses on REST APIs (related to #12)The text was updated successfully, but these errors were encountered: