How would you recommend as the best approach / UX for sharing with a customer the REST API errors in their nested JSON body? Say for example they are to send an object with 30 fields, but they only send 24 fields, and of the 24, 3 are unknown field names, and 5 are the incorrect data type. How would you like to see that error?
For example, if I have a data type sort of like this:
{
a: number
b: {
x: number
y: boolean
}
}
And I try to validate with this data:
{
a: 'foo'
}
I get this set of errors with zod:
[
{
"code": "invalid_type",
"expected": "number",
"received": "string",
"path": [
"a"
],
"message": "Expected number, received string"
},
{
"code": "invalid_type",
"expected": "object",
"received": "undefined",
"path": [
"b"
],
"message": "Required"
}
]
If I give it:
{ a: 'foo', b: { x: true } }
I get:
[
{
"code": "invalid_type",
"expected": "number",
"received": "string",
"path": [
"a"
],
"message": "Expected number, received string"
},
{
"code": "invalid_type",
"expected": "number",
"received": "boolean",
"path": [
"b",
"x"
],
"message": "Expected number, received boolean"
},
{
"code": "invalid_type",
"expected": "boolean",
"received": "undefined",
"path": [
"b",
"y"
],
"message": "Required"
}
]
Is this as good as it gets? Is there a better way to gather and present the errors in JSON form like this? I like this, but it for some reason still feels hard to understand and you have to really think about it to see the problem. Maybe there is a simpler way.