Skip to content

Commit c0c7604

Browse files
committed
feat(RuntimeTypeError.formatMessage): add includeActualValues: false option
1 parent 8bb93bb commit c0c7604

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

src/errorReporting/RuntimeTypeError.js.flow

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import type RuntimeTypeErrorItem from './RuntimeTypeErrorItem'
55
declare class RuntimeTypeError extends TypeError {
66
+errors: RuntimeTypeErrorItem[];
77
constructor(errors: RuntimeTypeErrorItem[]): void;
8-
formatMessage(options?: { limit?: number, ... }): string;
8+
formatMessage(options?: {
9+
limit?: number,
10+
includeActualValues?: boolean,
11+
...
12+
}): string;
913
}
1014

1115
export default RuntimeTypeError

src/errorReporting/RuntimeTypeError.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ export default class RuntimeTypeError extends TypeError {
1616
return this.formatMessage()
1717
}
1818

19-
formatMessage({ limit = 10000 }: { limit?: number } = {}): string {
19+
formatMessage({
20+
limit = 10000,
21+
includeActualValues = true,
22+
}: { limit?: number; includeActualValues?: boolean } = {}): string {
2023
const result = []
2124
let remaining =
2225
limit - delimiter.length + `... ${this.errors.length} more errors`.length
@@ -25,16 +28,23 @@ export default class RuntimeTypeError extends TypeError {
2528
if (result.length) remaining -= delimiter.length
2629
const stringified = error.toString()
2730
remaining -= stringified.length
28-
const actualValuePart = `\n\nActual Value: ${stringifyValue(
29-
error.valueAtPath,
30-
{ limit: remaining - `\n\nActual Value: `.length }
31-
)}`
32-
remaining -= actualValuePart.length
31+
let actualValuePart
32+
if (includeActualValues) {
33+
actualValuePart = `\n\nActual Value: ${stringifyValue(
34+
error.valueAtPath,
35+
{ limit: remaining - `\n\nActual Value: `.length }
36+
)}`
37+
remaining -= actualValuePart.length
38+
}
3339
if (remaining < 0 && result.length) {
3440
result.push(`... ${this.errors.length - i} more errors`)
3541
break
3642
}
37-
result.push(remaining < 0 ? stringified : stringified + actualValuePart)
43+
result.push(
44+
remaining < 0 || !actualValuePart
45+
? stringified
46+
: stringified + actualValuePart
47+
)
3848
}
3949
return result.join(delimiter)
4050
}

test/RuntimeTypeError.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,29 @@ describe(`RuntimeTypeError`, function() {
6161
`
6262
)
6363
})
64+
it(`includeActualValues: false`, function() {
65+
const TestType = t.array(t.object({ foo: t.number() }))
66+
const testValue = []
67+
for (let i = 0; i < 2; i++) {
68+
testValue.push({
69+
foo: 'this is a long long long long long property name',
70+
})
71+
}
72+
73+
expect(
74+
expectError(TestType, testValue).formatMessage({
75+
includeActualValues: false,
76+
})
77+
).to.equal(
78+
dedent`
79+
input[0].foo must be a number
80+
81+
-------------------------------------------------
82+
83+
input[1].foo must be a number
84+
`
85+
)
86+
})
6487
it(`limit works on number of errors`, function() {
6588
const TestType = t.array(t.object({ foo: t.number() }))
6689
const testValue = []

0 commit comments

Comments
 (0)