Skip to content
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

Elysia is overriding the summary property with my custom validation message #1041

Open
jasperdunn opened this issue Jan 30, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@jasperdunn
Copy link

jasperdunn commented Jan 30, 2025

What version of Elysia is running?

1.2.0

What platform is your computer?

macOS

What steps can reproduce the bug?

// typebox-error-messages.ts

import {
  DefaultErrorFunction,
  SetErrorFunction,
  ValueErrorType,
} from '@sinclair/typebox/errors';

SetErrorFunction((error) => {
  if (error.errorType === ValueErrorType.StringFormat) {
    if (error.schema['format'] === 'email') {
      return 'Please enter a valid email address.';
    }
  }

  return DefaultErrorFunction(error);
});
// Response data from the endpoint after an invalid request payload
{
  . . . .
  "summary": "Property 'email' should be Please enter a valid email address",
  "property": "/email",
  "message": "Please enter a valid email address.",
  . . . .
}

What is the expected behavior?

// Response data from the endpoint after an invalid request payload
{
  . . . .
  "summary": "Property 'email' should be email",
  "property": "/email",
  "message": "Please enter a valid email address.",
  . . . .
}

I don't think summary makes sense here really anyway since it's contextually incorrect, but this is the default behaviour for an email. Is this an Elysia property and not part of TypeBox?

Regardless, why is elysia adding it's own validation error data anyway? Wouldn't it be best to keep things simple and utilize TypeBox to it's full potential and/or let users customize the error themselves?

The code in the link within "Additional information" below feels completely redundant to me.

Additional information

Here is the code overriding the Sinclair TypeBox custom error message behaviour:

: `Property '${property}' should be ${format}`

@jasperdunn jasperdunn added the bug Something isn't working label Jan 30, 2025
@jasperdunn
Copy link
Author

jasperdunn commented Jan 30, 2025

elysia/src/error.ts

Lines 130 to 143 in 1ca0ef1

case 50:
// Expected string to match 'email' format
const quoteIndex = message.indexOf("'")!
const format = message.slice(
quoteIndex + 1,
message.indexOf("'", quoteIndex + 1)
)
return {
...error,
summary: isRoot
? `Value should be an email`
: `Property '${property}' should be ${format}`
}

This code in particular, why are we assuming that a formatted string is an email if the path is ""?

I vote to scrap that entire function tbh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant