badrap/valita

Is there any way to get all issues and not the only first one?

Closed this issue · 2 comments

    const testData = {};

    const testSchema = v.object({
        test: v.string(),
        test2: v.string()
    });

    testSchema.parse(testData);

The error only speaks of one problem but actually two fields are missing:

ValitaError: missing_key at . (missing key \"test\")

There is an .issues property on every error object thrown which holds an array of the encountered issues. That works for type mismatches, but it seems like missing keys errors are handled differently by bailing out immediately.

The newly released v0.0.24 collects all missing values and unrecognized keys.

  • Missing value errors from objects are now all collected:

    const t = v.object({ a: v.string(), b: v.string() });
    t.parse({});
    // ValitaError: missing_value at .b (missing value) (+ 1 other issue)
  • When in strict mode (which is the default) all unrecognized keys in an object are collected into one error:

    const t = v.object({});
    t.parse({ x: 1, y: 2 });
    // ValitaError: unrecognized_keys at . (unrecognized keys "x" and "y")