Skip to content

Commit

Permalink
fix: Include schema name in error message.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Sep 21, 2021
1 parent 822f8da commit 7dac519
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 7 additions & 0 deletions optimal/src/optimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function optimal<Schemas extends object>(
});
} catch (error: unknown) {
let invalid: OptimalError;
let prefix = '';

if (error instanceof OptimalError) {
invalid = error;
Expand All @@ -58,10 +59,16 @@ export function optimal<Schemas extends object>(

if (options.name) {
invalid.schema = options.name;
prefix = options.name;
}

if (options.file) {
invalid.file = options.file;
prefix = prefix ? `${prefix} (${options.file})` : options.file;
}

if (prefix) {
invalid.message = `${prefix}: ${invalid.message}`;
}

throw invalid;
Expand Down
2 changes: 1 addition & 1 deletion optimal/tests/optimal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ describe('Optimal', () => {
entry: 123,
});
}).toThrowErrorMatchingInlineSnapshot(`
"The following validations have failed:
"FooBar: The following validations have failed:
- Invalid field \\"entry\\" with value 123. Value must be one of: string, array<string>, object<string | array<string>>, function."
`);
});
Expand Down

0 comments on commit 7dac519

Please sign in to comment.