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

Preflight validation check failed to guard for the given schema for Never() since 1.2.21 #1129

Open
mattersj opened this issue Mar 15, 2025 · 6 comments
Labels
bug Something isn't working

Comments

@mattersj
Copy link

mattersj commented Mar 15, 2025

What version of Elysia is running?

1.2.25

What platform is your computer?

Darwin 23.5.0 arm64 arm

What steps can reproduce the bug?

  1. Add a validation schema containing t.Never(), i.e.:
query: t.Object({
  test1: t.Array(t.Never(), { default: [] }), // explicit `never`, always an empty array
  test2: t.Optional(t.Never()), // optional explicit `never` - a field may not be passed
  test3: t.Array( // implicit `never` in case of an empty union
    t.Union([some data].map((value) => t.Literal(value)),
    { default: [] },
  ),
}),
  1. Send a request to your endpoint.
  2. Observe a 'Preflight validation check failed to guard for the given schema' error.

What is the expected behavior?

  • test1 should always be an empty array with no errors unless a value is explicitly passed;
  • test2 should behave the same way except the value itself is never;
  • test3 should produce either an array containing a union of literals or just never[] when there's no data for literals.

What do you see instead?

A 'Preflight validation check failed to guard for the given schema' error occurs if a schema references never in any way, either directly or indirectly.

Additional information

It has been broken since 1.2.21.
1.2.20 worked fine and conformed to the expected behavior.

Have you try removing the node_modules and bun.lockb and try again yet?

No response

@mattersj mattersj added the bug Something isn't working label Mar 15, 2025
@hisamafahri
Copy link
Contributor

You mean like this?

  query: t.Object({
    test1: t.Array(t.Never(), { default: [] }),
    test2: t.Optional(t.Never()),
    test3: t.Array(
      t.Union(
        ["123"].map((value) => t.Literal(value)),
        { default: [] },
      ),
    ),
  }),

@mattersj
Copy link
Author

@hisamafahri Yeah, a t.Object wrapper is obviously there, I just forgot to mention it. Just added it to my original example to avoid confusion, thanks.

@hisamafahri
Copy link
Contributor

The test3 on its own should be fine.

But this is a typebox issue. And it's kinda make sense why this error happens (even though the Typebox's error message not really descriptive).

Why? Because you use Typescript's never. The never indicates the values that will never occur (and even throws an exception).

What's your actual intention on those type here?

@mattersj
Copy link
Author

@hisamafahri

And it's kinda make sense why this error happens

This error only happens in elysia ^1.2.21, it doesn't happen in <= 1.2.20 or in TypeBox itself. So it's not a TypeBox issue in that case.

The never indicates the values that will never occur

Exactly, and it literally means "a field may not be passed at all", so if you don't pass anything to this field then you're fine and you should have no errors.

What's your actual intention on those type here?

I have something similar to test3 in my app code and it worked fine prior to 1.2.21. So if I have some data to build literals from, then it should be an array of those literals, but if I don't have any data it should just produce never[], and I can freely pass an empty array (or nothing at all) to this field without any errors.

@hisamafahri
Copy link
Contributor

hisamafahri commented Mar 19, 2025

I have something similar to test3 in my app code and it worked fine prior to 1.2.21. So if I have some data to build literals from, then it should be an array of those literals, but if I don't have any data it should just produce never[], and I can freely pass an empty array (or nothing at all) to this field without any errors.

I still am not sure why you want never[] (instead of []). 🤔

But yeah confirm that it breaks on 1.2.21 🙆‍♂

@mattersj
Copy link
Author

I still am not sure why you want never[] (instead of []).

[] is an empty tuple and it would work too, but most likely you'll have to do it manually by calling a t.Tuple([]). It's a little more verbose compared to t.Never() which is produced automatically from an empty array.

t.Array(
  t.Union(data.map((value) => t.Literal(value))),
  { default: [] },
),

is cleaner than

data.length > 0
  ? t.Array(
    t.Union(data.map((value) => t.Literal(value))),
    { default: [] },
  ),
  : t.Tuple([]);

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

2 participants