Skip to content

Commit

Permalink
feat(type): return all errors when validating array (#568)
Browse files Browse the repository at this point in the history
Instead of just first as previously #565 (comment)
  • Loading branch information
lionelhorn authored May 14, 2024
1 parent d89e3d3 commit c2ca10e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
1 change: 0 additions & 1 deletion packages/type/src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1502,7 +1502,6 @@ export function typeGuardArray(elementType: Type, state: TemplateState) {
${v} = iterableSize(${state.accessor}) === 0;
for (const ${item} of ${state.accessor}) {
${executeTemplates(state.fork(v, item).extendPath(new RuntimeCode(i)), elementType)}
if (!${v}) break;
${i}++;
}
} else if (${state.isValidation()}) {
Expand Down
14 changes: 14 additions & 0 deletions packages/type/tests/validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,17 @@ test('speed', () => {
// but inline vs ref must not be slower than 2.5 times.
// expect(ratio).toBeLessThan(2.5);
});

test('array with multiple errors', () => {
interface Skill {
level: number & Positive;
}

const errors = validate<Array<Skill>>([{level: -1}, {level: -1}]);
console.log(errors);

expect(errors).toEqual([
{code: 'positive', message: 'Number needs to be positive', path: '0.level', value: -1},
{code: 'positive', message: 'Number needs to be positive', path: '1.level', value: -1}
]);
});

0 comments on commit c2ca10e

Please sign in to comment.