Skip to content

Commit

Permalink
Limit multiple errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed May 15, 2023
1 parent d7c5322 commit 60e9ccd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
25 changes: 23 additions & 2 deletions lib/logger/utils.spec.ts
Expand Up @@ -84,9 +84,30 @@ describe('logger/utils', () => {
'Expected array, received number'
);

expect(prepareIssues(z.string().array(), [42, 'foo', 42])).toEqual({
'0': 'Expected string, received number',
expect(
prepareIssues(z.string().array(), ['foo', 'bar', 42, 42, 42, 42, 42])
).toEqual({
'2': 'Expected string, received number',
'3': 'Expected string, received number',
'4': 'Expected string, received number',
___: '... 2 more',
});

expect(
prepareIssues(z.record(z.string()), {
foo: 'foo',
bar: 'bar',
key1: 42,
key2: 42,
key3: 42,
key4: 42,
key5: 42,
})
).toEqual({
key1: 'Expected string, received number',
key2: 'Expected string, received number',
key3: 'Expected string, received number',
___: '... 2 more',
});

expect(
Expand Down
8 changes: 7 additions & 1 deletion lib/logger/utils.ts
Expand Up @@ -79,12 +79,18 @@ export function prepareZodIssues(input: unknown): ZodShortenedIssue {
}

const output: Record<string, ZodShortenedIssue> = {};
for (const [key, value] of Object.entries(input)) {
const entries = Object.entries(input);
for (const [key, value] of entries.slice(0, 3)) {
const child = prepareZodIssues(value);
if (child !== null) {
output[key] = child;
}
}

if (entries.length > 3) {
output['___'] = `... ${entries.length - 3} more`;
}

return output;
}

Expand Down

0 comments on commit 60e9ccd

Please sign in to comment.