Skip to content

Commit

Permalink
fix(ObjectValidator): don't run validation on arrays (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladfrangu committed May 4, 2022
1 parent 45fbbf1 commit c83b3d0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/validators/ObjectValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export class ObjectValidator<T extends NonNullObject> extends BaseValidator<T> {
return Result.err(new ValidationError('s.object(T)', 'Expected the value to not be null', value));
}

if (Array.isArray(value)) {
return Result.err(new ValidationError('s.object(T)', 'Expected the value to not be an array', value));
}

return this.handleStrategy(value as NonNullObject);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/validators/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ describe('ObjectValidator', () => {
expectError(() => predicate.parse(null), new ValidationError('s.object(T)', 'Expected the value to not be null', null));
});

test('GIVEN an array value THEN throws ValidationError', () => {
expectError(() => predicate.parse([]), new ValidationError('s.object(T)', 'Expected the value to not be an array', []));
});

test('GIVEN a valid object THEN returns processed object', () => {
expect(predicate.parse({ username: 'Sapphire', password: 'helloworld' })).toStrictEqual({ username: 'Sapphire', password: 'helloworld' });
});
Expand Down

0 comments on commit c83b3d0

Please sign in to comment.