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

Confusing error messages from either #169

Open
ChrisNixTriller opened this issue Mar 29, 2022 · 1 comment
Open

Confusing error messages from either #169

ChrisNixTriller opened this issue Mar 29, 2022 · 1 comment

Comments

@ChrisNixTriller
Copy link

ChrisNixTriller commented Mar 29, 2022

node: 16.14.0
typescript: 4.5.5
computed-types: 1.11.2

      const schema = Schema.either(
        {
          foo: string.equals('X'),
          bar: array.max(0),
          baz: array.min(1),
        },
        {
          foo: string.equals('Y'),
          bar: array.min(1),
          baz: array.max(0),        
        },
      )

      const obj = { foo: 'Y', bar: [], baz: [] }

      const validator = schema.destruct()

      const [err, validatedObj] = validator(obj)

The error message above is bar: Expect array to be minimum of 1 items (actual: 0). This makes sense for the value of foo === 'Y'.

Here's a curious observation... If I use const obj = { foo: 'X', bar: [], baz: [] }, then the error changes to foo: Expect value to equal "Y". Two notes:

  1. I think that the error is harder to understand since the error reports the path foo to be wrong, when arguably it's the baz array that's not matching the schema.
  2. The error differs from the case given in the initial snippet. However, the cases are symmetric about X and Y (apart from the order in the either definition), so one might expect the errors to be similar.

Obviously, the analogous error occurs if I swap the order of the schemas in the either definition.

This is really pedantic, I know. The library is great and hopefully this is just some useful feedback for thought.

@moshest
Copy link
Member

moshest commented Apr 9, 2022

either implementation just throws one of the errors it get from its internal items. I think it may be better to throw a custom error like "Can't find a valid type matching the either statement" or something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants