Skip to content

Commit

Permalink
refactor(type): return all errors when validating array
Browse files Browse the repository at this point in the history
Instead of just first as previously #565 (comment)
  • Loading branch information
lionelhorn committed May 14, 2024
1 parent d89e3d3 commit 4f12c65
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 4f12c65

Please sign in to comment.