I have a validation schema like this (i'm using valibot):
export const RegistrationSchema = {
body: v.object({
name: v.pipe(
v.string(),
v.trim(),
v.maxLength(100),
v.description('Name of the account')
),
email: v.pipe(
v.string(),
v.trim(),
v.maxLength(100),
v.email(),
v.description('Email address of the account')
),
username: v.pipe(
v.string(),
v.trim(),
v.toLowerCase(),
v.maxLength(30),
v.regex(/^[a-z0-9_-]+$/),
v.description('Username of the account, will be used for login authentication')
),
password: v.pipe(
v.string(),
v.description('Password of the account, will be used for login authentication')
)
})
}
and this is what it should look like in my onError method:
({ code, set, error }) => {
switch (code) {
case 'VALIDATION':
set.status = 422;
// todo: stuck here
return error.all;
case 'NOT_FOUND':
set.status = 404;
return {
success: false,
message: 'path is not found. maybe you lost/typo/something?'
}
default:
set.status = 500;
return {
success: false,
message: 'internal error occurred, be back later'
}
}
};
the case is i want to output the validation result. when i returning the error instance, i got this:
{
"type": "validation",
"on": "body",
"property": {
"type": "object",
"origin": "key",
"input": {},
"key": "name"
},
"message": "Invalid key: Expected \"name\" but received undefined",
"found": {},
"errors": [
{
"kind": "schema",
"type": "object",
"expected": "\"name\"",
"received": "undefined",
"message": "Invalid key: Expected \"name\" but received undefined",
"path": [
{
"type": "object",
"origin": "key",
"input": {},
"key": "name"
}
]
},
{
"kind": "schema",
"type": "object",
"expected": "\"email\"",
"received": "undefined",
"message": "Invalid key: Expected \"email\" but received undefined",
"path": [
{
"type": "object",
"origin": "key",
"input": {},
"key": "email"
}
]
},
{
"kind": "schema",
"type": "object",
"expected": "\"username\"",
"received": "undefined",
"message": "Invalid key: Expected \"username\" but received undefined",
"path": [
{
"type": "object",
"origin": "key",
"input": {},
"key": "username"
}
]
},
{
"kind": "schema",
"type": "object",
"expected": "\"password\"",
"received": "undefined",
"message": "Invalid key: Expected \"password\" but received undefined",
"path": [
{
"type": "object",
"origin": "key",
"input": {},
"key": "password"
}
]
}
]
}
i need that error.errors for me to parse it to my own json response format. but in the onError, when i try to return error.errors, it doesn't exist (though i also got an error in typescript though). it just returning the same thing above.
I tried to return error.all but the response doesn't meet my needs, which like below:
[
{
"summary": "Invalid key: Expected \"name\" but received undefined",
"path": "[object Object]",
"message": "Invalid key: Expected \"name\" but received undefined",
"value": {}
},
{
"summary": "Invalid key: Expected \"email\" but received undefined",
"path": "[object Object]",
"message": "Invalid key: Expected \"email\" but received undefined",
"value": {}
},
{
"summary": "Invalid key: Expected \"username\" but received undefined",
"path": "[object Object]",
"message": "Invalid key: Expected \"username\" but received undefined",
"value": {}
},
{
"summary": "Invalid key: Expected \"password\" but received undefined",
"path": "[object Object]",
"message": "Invalid key: Expected \"password\" but received undefined",
"value": {}
}
]
any notice or help might be appreciated, or if you guys have any other better approach. i just started Elysia with my new project, because it seems promising with performance too.
thanks, regards.
I have a validation schema like this (i'm using valibot):
and this is what it should look like in my
onErrormethod:the case is i want to output the validation result. when i returning the error instance, i got this:
{ "type": "validation", "on": "body", "property": { "type": "object", "origin": "key", "input": {}, "key": "name" }, "message": "Invalid key: Expected \"name\" but received undefined", "found": {}, "errors": [ { "kind": "schema", "type": "object", "expected": "\"name\"", "received": "undefined", "message": "Invalid key: Expected \"name\" but received undefined", "path": [ { "type": "object", "origin": "key", "input": {}, "key": "name" } ] }, { "kind": "schema", "type": "object", "expected": "\"email\"", "received": "undefined", "message": "Invalid key: Expected \"email\" but received undefined", "path": [ { "type": "object", "origin": "key", "input": {}, "key": "email" } ] }, { "kind": "schema", "type": "object", "expected": "\"username\"", "received": "undefined", "message": "Invalid key: Expected \"username\" but received undefined", "path": [ { "type": "object", "origin": "key", "input": {}, "key": "username" } ] }, { "kind": "schema", "type": "object", "expected": "\"password\"", "received": "undefined", "message": "Invalid key: Expected \"password\" but received undefined", "path": [ { "type": "object", "origin": "key", "input": {}, "key": "password" } ] } ] }i need that
error.errorsfor me to parse it to my own json response format. but in theonError, when i try to returnerror.errors, it doesn't exist (though i also got an error in typescript though). it just returning the same thing above.I tried to return
error.allbut the response doesn't meet my needs, which like below:[ { "summary": "Invalid key: Expected \"name\" but received undefined", "path": "[object Object]", "message": "Invalid key: Expected \"name\" but received undefined", "value": {} }, { "summary": "Invalid key: Expected \"email\" but received undefined", "path": "[object Object]", "message": "Invalid key: Expected \"email\" but received undefined", "value": {} }, { "summary": "Invalid key: Expected \"username\" but received undefined", "path": "[object Object]", "message": "Invalid key: Expected \"username\" but received undefined", "value": {} }, { "summary": "Invalid key: Expected \"password\" but received undefined", "path": "[object Object]", "message": "Invalid key: Expected \"password\" but received undefined", "value": {} } ]any notice or help might be appreciated, or if you guys have any other better approach. i just started Elysia with my new project, because it seems promising with performance too.
thanks, regards.