Skip to content

Commit dfb7a13

Browse files
committed
fix: check type constructor arguments
1 parent c9b7b36 commit dfb7a13

26 files changed

+97
-8
lines changed

src/types/ArrayType.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Type from './Type'
1+
import Type, { assertIsType } from './Type'
22
import Validation, { IdentifierPath } from '../Validation'
33

44
import {
@@ -18,6 +18,7 @@ export default class ArrayType<T> extends Type<Array<T>> {
1818

1919
constructor(elementType: Type<T>) {
2020
super()
21+
assertIsType(elementType, 'elementType')
2122
this.elementType = elementType
2223
}
2324

src/types/BooleanLiteralType.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ export default class BooleanLiteralType<
77

88
constructor(value: T) {
99
super(value)
10+
if (typeof value !== 'boolean') {
11+
throw new Error(`value must be a boolean`)
12+
}
1013
}
1114
}

src/types/IntersectionType.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Type from './Type'
1+
import Type, { assertIsType } from './Type'
22
import Validation, { IdentifierPath } from '../Validation'
33
import RuntimeTypeErrorItem from '../errorReporting/RuntimeTypeErrorItem'
44
import ObjectType from './ObjectType'
@@ -12,6 +12,9 @@ export default class IntersectionType<T> extends Type<T> {
1212

1313
constructor(types: Type<any>[]) {
1414
super()
15+
for (let i = 0; i < types.length; i++) {
16+
assertIsType(types[i], `types[${i}]`)
17+
}
1518
this.types = types
1619
}
1720

src/types/MergedObjectType.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Type from './Type'
1+
import Type, { assertIsType } from './Type'
22
import ObjectType from './ObjectType'
33
import ObjectTypeProperty from './ObjectTypeProperty'
44
import Validation, { IdentifierPath } from '../Validation'
@@ -12,6 +12,9 @@ export default class MergedObjectType<T extends {}> extends Type<T> {
1212

1313
constructor(objects: Type<T>[], exact = true) {
1414
super()
15+
for (let i = 0; i < objects.length; i++) {
16+
assertIsType(objects[i], `objects[${i}]`)
17+
}
1518
this.objects = objects
1619
this.exact = exact
1720
}

src/types/NumericLiteralType.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export default class NumericLiteralType<
77

88
constructor(value: T) {
99
super(value)
10+
if (typeof value !== 'number') {
11+
throw new Error(`value must be a number`)
12+
}
1013
}
1114

1215
toString(): string {

src/types/ObjectType.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ export default class ObjectType<T extends {}> extends Type<T> {
2525
exact = true
2626
) {
2727
super()
28+
for (let i = 0; i < properties.length; i++) {
29+
if (!(properties[i] instanceof ObjectTypeProperty)) {
30+
throw new Error(
31+
`properties[${i}] must be an instance of ObjectTypeProperty`
32+
)
33+
}
34+
}
2835
this.properties = properties
2936
this.exact = exact
3037
properties.forEach(prop => (prop.__objectType = this))

src/types/ObjectTypeProperty.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Type from './Type'
1+
import Type, { assertIsType } from './Type'
22
import Validation, { IdentifierPath } from '../Validation'
33
import RuntimeTypeErrorItem from '../errorReporting/RuntimeTypeErrorItem'
44
import MissingPropertyErrorItem from '../errorReporting/MissingPropertyErrorItem'
@@ -16,6 +16,15 @@ export default class ObjectTypeProperty<
1616

1717
constructor(key: K, value: Type<V>, optional: boolean) {
1818
super()
19+
switch (typeof key) {
20+
case 'number':
21+
case 'string':
22+
case 'symbol':
23+
break
24+
default:
25+
throw new Error('key must be a number, string or symbol')
26+
}
27+
assertIsType(value, 'value')
1928
this.key = key
2029
this.value = value
2130
this.optional = optional

src/types/RecordType.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Type from './Type'
1+
import Type, { assertIsType } from './Type'
22
import Validation, { IdentifierPath } from '../Validation'
33

44
import {
@@ -23,6 +23,8 @@ export default class RecordType<
2323

2424
constructor(key: Type<K>, value: Type<V>) {
2525
super()
26+
assertIsType(key, 'key')
27+
assertIsType(value, 'value')
2628
this.key = key
2729
this.value = value
2830
}

src/types/StringLiteralType.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export default class StringLiteralType<
77

88
constructor(value: T) {
99
super(value)
10+
if (typeof value !== 'string') {
11+
throw new Error(`value must be a string`)
12+
}
1013
}
1114

1215
toString(): string {

src/types/SymbolLiteralType.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ export default class SymbolLiteralType<
77

88
constructor(value: T) {
99
super(value)
10+
if (typeof value !== 'symbol') {
11+
throw new Error(`value must be symbol`)
12+
}
1013
}
1114
}

0 commit comments

Comments
 (0)