Skip to content

Commit

Permalink
Make JSON object validation more permissive
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospassos committed Apr 20, 2020
1 parent e0ffcc6 commit 2e92b8f
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/validation/jsonType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ function isJsonObject(value: unknown): value is JsonObject {
}

function isPlainObject(value: unknown): value is object {
return typeof value === 'object' // exclude primitives
&& value !== null // exclude nulls
&& value.constructor === Object // exclude instances (Array, DOM, ...)
&& Object.prototype.toString.call(value) === '[object Object]'; // exclude build-in like Math
return Object.prototype.toString.call(value) === '[object Object]';
}

type JsonObjectDefinition = {
Expand Down
1 change: 1 addition & 0 deletions test/validation/jsonType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe('A JSON object type', () => {
[['foo', 'bar'], false],
[{foo: 'bar'}, true],
[new Object('foo'), false],
[new Object(), true],
])('should determine whether the type of a given value is valid', (value: any, expected: boolean) => {
expect(new JsonObjectType().isValidType(value)).toBe(expected);
});
Expand Down

0 comments on commit 2e92b8f

Please sign in to comment.