Skip to content

Elysia - Valibot: Validation Custom Error Handling #846

Description

@gensart-x

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions