valibot: Unhelpful parsing error

If you omit mandatory attributes or provide string instead of object, you get an unhelpful message:

Even when you have Uncaught ValiError: Invalid email, it should state what’s the problem with email.

> import {email, minLength, array, literal, number, object, optional, parse, string, Output} from 'https://deno.land/x/valibot/mod.ts';
undefined
> const LoginSchema = object({
  email: string([email()]),
  password: string([minLength(8)]),
});
undefined
> parse(LoginSchema, { });

Uncaught ValiError: Invalid type
    at parse (https://deno.land/x/valibot@v0.26.0/src/methods/parse/parse.ts:20:11)
    at <anonymous>:1:22
> parse(LoginSchema, {email:'', password:'' });

Uncaught ValiError: Invalid email
    at parse (https://deno.land/x/valibot@v0.26.0/src/methods/parse/parse.ts:20:11)
    at <anonymous>:1:22
> parse(LoginSchema, '{}');

Uncaught ValiError: Invalid type
    at parse (https://deno.land/x/valibot@v0.26.0/src/methods/parse/parse.ts:20:11)
    at <anonymous>:1:22
> 

About this issue

  • Original URL
  • State: closed
  • Created 5 months ago
  • Comments: 19 (10 by maintainers)

Most upvoted comments

Cool, that global setting will solve my scenario.

As for l10n, I see it as generating error messages is a 2 step process (step 1: describe the error, step2: forumlate fluent English/German/… text from the error), and since I don’t need l10n, I am happy with an arbitrary json object, which describes validation errors. That’s why I came up with the idea of a custom toString implementation of the exception. The exception should not be localized, because it is for the developer.

I plan to improve the error messages. You can find more info in #397.

Yes, this is possible with our non-exception-based API safeParse. Read more about parsing data here: https://valibot.dev/guides/parse-data/