Skip to content

Commit cf2599b

Browse files
committed
feat: add RuntimeTypeErrorItem.messageAtPath() method
1 parent 5c59862 commit cf2599b

File tree

7 files changed

+29
-25
lines changed

7 files changed

+29
-25
lines changed

src/errorReporting/InvalidKeyTypeErrorItem.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Type from '../types/Type'
2-
import { IdentifierPath, stringifyPath } from '../Validation'
2+
import { IdentifierPath } from '../Validation'
33
import { keyToString } from './keyToString'
44
import RuntimeTypeErrorItem from './RuntimeTypeErrorItem'
55

@@ -20,8 +20,8 @@ export default class InvalidKeyTypeErrorItem extends RuntimeTypeErrorItem {
2020
this.expectedKeyType = expectedKeyType
2121
}
2222

23-
toString(): string {
24-
return `${stringifyPath(this.path)} has key of invalid type: ${keyToString(
23+
messageAtPath(): string {
24+
return `has key of invalid type: ${keyToString(
2525
this.key
2626
)}\n\nEach key must be ${this.expectedKeyType.toString({
2727
formatForMustBe: true,

src/errorReporting/InvalidLengthErrorItem.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ export default class InvalidLengthErrorItem extends RuntimeTypeErrorItem {
1818
this.expectedLength = expectedLength
1919
}
2020

21+
messageAtPath(): string {
22+
return `length must be ${this.expectedLength}, instead it is ${this.valueAtPath.length}`
23+
}
24+
2125
toString(): string {
22-
return `${stringifyPath(this.path)}.length must be ${
23-
this.expectedLength
24-
}, instead it is ${this.valueAtPath.length}`
26+
return `${stringifyPath(this.path)}.${this.messageAtPath()}`
2527
}
2628
}

src/errorReporting/InvalidTypeErrorItem.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Type from '../types/Type'
2-
import { IdentifierPath, stringifyPath } from '../Validation'
2+
import { IdentifierPath } from '../Validation'
33
import RuntimeTypeErrorItem from './RuntimeTypeErrorItem'
44

55
export default class InvalidTypeErrorItem extends RuntimeTypeErrorItem {
@@ -13,9 +13,9 @@ export default class InvalidTypeErrorItem extends RuntimeTypeErrorItem {
1313
super(path, valueAtPath, expectedTypeAtPath)
1414
}
1515

16-
toString(): string {
17-
return `${stringifyPath(
18-
this.path
19-
)} must be ${this.expectedTypeAtPath.toString({ formatForMustBe: true })}`
16+
messageAtPath(): string {
17+
return `must be ${this.expectedTypeAtPath.toString({
18+
formatForMustBe: true,
19+
})}`
2020
}
2121
}

src/errorReporting/MissingPropertyErrorItem.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Type from '../types/Type'
22
import ObjectTypeProperty from '../types/ObjectTypeProperty'
33
import RuntimeTypeErrorItem from './RuntimeTypeErrorItem'
4-
import { IdentifierPath, stringifyPath } from '../Validation'
4+
import { IdentifierPath } from '../Validation'
55
import { keyToString } from './keyToString'
66

77
export default class MissingPropertyErrorItem extends RuntimeTypeErrorItem {
@@ -18,10 +18,8 @@ export default class MissingPropertyErrorItem extends RuntimeTypeErrorItem {
1818
this.propertyType = propertyType
1919
}
2020

21-
toString(): string {
22-
return `${stringifyPath(
23-
this.path
24-
)} is missing required property ${keyToString(
21+
messageAtPath(): string {
22+
return `is missing required property ${keyToString(
2523
this.propertyType.key
2624
)}, which must be ${this.propertyType.value.toString({
2725
formatForMustBe: true,

src/errorReporting/RuntimeTypeErrorItem.ts

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

44
export default abstract class RuntimeTypeErrorItem {
@@ -19,4 +19,10 @@ export default abstract class RuntimeTypeErrorItem {
1919
this.expectedTypeAtPath = expectedTypeAtPath
2020
this.depth = depth
2121
}
22+
23+
abstract messageAtPath(): string
24+
25+
toString(): string {
26+
return `${stringifyPath(this.path)} ${this.messageAtPath()}`
27+
}
2228
}

src/errorReporting/UnknownPropertyErrorItem.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Type from '../types/Type'
22
import RuntimeTypeErrorItem from './RuntimeTypeErrorItem'
3-
import { IdentifierPath, stringifyPath } from '../Validation'
3+
import { IdentifierPath } from '../Validation'
44
import { keyToString } from './keyToString'
55

66
export default class UnknownPropertyErrorItem extends RuntimeTypeErrorItem {
@@ -17,9 +17,7 @@ export default class UnknownPropertyErrorItem extends RuntimeTypeErrorItem {
1717
this.key = key
1818
}
1919

20-
toString(): string {
21-
return `${stringifyPath(this.path)} has unknown property: ${keyToString(
22-
this.key
23-
)}`
20+
messageAtPath(): string {
21+
return `has unknown property: ${keyToString(this.key)}`
2422
}
2523
}

src/errorReporting/ViolatedConstraintErrorItem.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Type from '../types/Type'
2-
import { IdentifierPath, stringifyPath } from '../Validation'
2+
import { IdentifierPath } from '../Validation'
33
import RuntimeTypeErrorItem from './RuntimeTypeErrorItem'
44

55
export default class ViolatedConstraintErrorItem extends RuntimeTypeErrorItem {
@@ -16,7 +16,7 @@ export default class ViolatedConstraintErrorItem extends RuntimeTypeErrorItem {
1616
this.constraintErrorMessage = constraintErrorMessage
1717
}
1818

19-
toString(): string {
20-
return `${stringifyPath(this.path)} ${this.constraintErrorMessage}`
19+
messageAtPath(): string {
20+
return this.constraintErrorMessage
2121
}
2222
}

0 commit comments

Comments
 (0)