From c83b3d03a201d38cc230d9c831ca1d9b66ca533b Mon Sep 17 00:00:00 2001 From: Vlad Frangu Date: Wed, 4 May 2022 19:25:30 +0300 Subject: [PATCH] fix(ObjectValidator): don't run validation on arrays (#99) --- src/validators/ObjectValidator.ts | 4 ++++ tests/validators/object.test.ts | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/validators/ObjectValidator.ts b/src/validators/ObjectValidator.ts index 04f758f5..a21f7579 100644 --- a/src/validators/ObjectValidator.ts +++ b/src/validators/ObjectValidator.ts @@ -80,6 +80,10 @@ export class ObjectValidator extends BaseValidator { 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); } diff --git a/tests/validators/object.test.ts b/tests/validators/object.test.ts index e8f53c1f..ccd8287c 100644 --- a/tests/validators/object.test.ts +++ b/tests/validators/object.test.ts @@ -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' }); });