Skip to content

Commit

Permalink
fix: don't highlight code examples in returned error messages (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion authored Mar 27, 2023
1 parent 438851c commit 989af8f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 45 deletions.
44 changes: 22 additions & 22 deletions src/__tests__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@ exports[`Main Babel export should function as normal 1`] = `
"ENUM must be equal to one of the allowed values
(paragraph, codeBlock, blockquote)
[0m[31m[1m>[22m[39m[90m 1 |[39m {[32m"type"[39m[33m:[39m[32m"doc"[39m[33m,[39m[32m"version"[39m[33m:[39m[35m1[39m[33m,[39m[32m"content"[39m[33m:[39m[{[32m"type"[39m[33m:[39m[32m"paragarph"[39m}]}[0m
[0m [90m |[39m [31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m [31m[1m👈🏽 Did you mean [95mparagraph[31m here?[22m[39m[0m"
> 1 | {"type":"doc","version":1,"content":[{"type":"paragarph"}]}
| ^^^^^^^^^^^ 👈🏽 Did you mean [95mparagraph[39m here?"
`;

exports[`Main complex schema examples should output an error on an invalid OpenAPI 3.1 definition 1`] = `
"UNEVALUATED PROPERTY must NOT have unevaluated properties
[0m [90m 15 |[39m [32m"summary"[39m[33m:[39m [32m"Example operation"[39m[33m,[39m[0m
[0m [90m 16 |[39m [32m"description"[39m[33m:[39m [32m"This operation has \`tags\` typod."[39m[33m,[39m[0m
[0m[31m[1m>[22m[39m[90m 17 |[39m [32m"tagss"[39m[33m:[39m [[0m
[0m [90m |[39m [31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m [31m[1m😲 [95mtagss[31m is not expected to be here![22m[39m[0m
[0m [90m 18 |[39m [32m"Tag Name"[39m[0m
[0m [90m 19 |[39m ][33m,[39m[0m
[0m [90m 20 |[39m [32m"responses"[39m[33m:[39m {[0m"
15 | "summary": "Example operation",
16 | "description": "This operation has \`tags\` typod.",
> 17 | "tagss": [
| ^^^^^^^ 😲 [95mtagss[39m is not expected to be here!
18 | "Tag Name"
19 | ],
20 | "responses": {"
`;
exports[`Main should output error with codeframe 1`] = `
"ENUM must be equal to one of the allowed values
(paragraph, codeBlock, blockquote)
[0m [90m 2 |[39m [32m"type"[39m[33m:[39m [32m"doc"[39m[33m,[39m[0m
[0m [90m 3 |[39m [32m"version"[39m[33m:[39m [35m1[39m[33m,[39m[0m
[0m[31m[1m>[22m[39m[90m 4 |[39m [32m"content"[39m[33m:[39m [{ [32m"type"[39m[33m:[39m [32m"paragarph"[39m }][0m
[0m [90m |[39m [31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m [31m[1m👈🏽 Did you mean [95mparagraph[31m here?[22m[39m[0m
[0m [90m 5 |[39m }[0m
[0m [90m 6 |[39m[0m"
2 | "type": "doc",
3 | "version": 1,
> 4 | "content": [{ "type": "paragarph" }]
| ^^^^^^^^^^^ 👈🏽 Did you mean [95mparagraph[39m here?
5 | }
6 |"
`;
exports[`Main should output error with reconstructed codeframe [without colors] 1`] = `
Expand All @@ -49,11 +49,11 @@ exports[`Main should output error with reconstructed codeframe 1`] = `
"ENUM must be equal to one of the allowed values
(paragraph, codeBlock, blockquote)
[0m [90m 4 |[39m [32m"content"[39m[33m:[39m [[0m
[0m [90m 5 |[39m {[0m
[0m[31m[1m>[22m[39m[90m 6 |[39m [32m"type"[39m[33m:[39m [32m"paragarph"[39m[0m
[0m [90m |[39m [31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m [31m[1m👈🏽 Did you mean [95mparagraph[31m here?[22m[39m[0m
[0m [90m 7 |[39m }[0m
[0m [90m 8 |[39m ][0m
[0m [90m 9 |[39m }[0m"
4 | "content": [
5 | {
> 6 | "type": "paragarph"
| ^^^^^^^^^^^ 👈🏽 Did you mean [95mparagraph[39m here?
7 | }
8 | ]
9 | }"
`;
36 changes: 18 additions & 18 deletions src/validation-errors/__tests__/__snapshots__/enum.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ exports[`Enum when value is a primitive prints correctly for empty value 1`] = `
"ENUM should be equal to one of the allowed values",
"(foo, bar)
",
"[0m[31m[1m>[22m[39m[90m 1 |[39m [32m"baz"[39m[0m
[0m [90m |[39m [31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m [31m[1m👈🏽 Did you mean [95mbar[31m here?[22m[39m[0m",
"> 1 | "baz"
| ^^^^^ 👈🏽 Did you mean [95mbar[39m here?",
]
`;

Expand All @@ -35,8 +35,8 @@ exports[`Enum when value is a primitive prints correctly for enum prop 1`] = `
"ENUM should be equal to one of the allowed values",
"(foo, bar)
",
"[0m[31m[1m>[22m[39m[90m 1 |[39m [32m"baz"[39m[0m
[0m [90m |[39m [31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m [31m[1m👈🏽 Did you mean [95mbar[31m here?[22m[39m[0m",
"> 1 | "baz"
| ^^^^^ 👈🏽 Did you mean [95mbar[39m here?",
]
`;

Expand All @@ -55,8 +55,8 @@ exports[`Enum when value is a primitive prints correctly for no levenshtein matc
"ENUM should be equal to one of the allowed values",
"(one, two)
",
"[0m[31m[1m>[22m[39m[90m 1 |[39m [32m"baz"[39m[0m
[0m [90m |[39m [31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m [31m[1m👈🏽 Unexpected value, should be equal to one of the allowed values[22m[39m[0m",
"> 1 | "baz"
| ^^^^^ 👈🏽 Unexpected value, should be equal to one of the allowed values",
]
`;

Expand All @@ -77,10 +77,10 @@ exports[`Enum when value is an object prints correctly for empty value 1`] = `
"ENUM should be equal to one of the allowed values",
"(foo, bar)
",
"[0m [90m 1 |[39m {[0m
[0m[31m[1m>[22m[39m[90m 2 |[39m [32m"id"[39m[33m:[39m [32m"baz"[39m[0m
[0m [90m |[39m [31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m [31m[1m👈🏽 Did you mean [95mbar[31m here?[22m[39m[0m
[0m [90m 3 |[39m }[0m",
" 1 | {
> 2 | "id": "baz"
| ^^^^^ 👈🏽 Did you mean [95mbar[39m here?
3 | }",
]
`;
Expand All @@ -101,10 +101,10 @@ exports[`Enum when value is an object prints correctly for enum prop 1`] = `
"ENUM should be equal to one of the allowed values",
"(foo, bar)
",
"[0m [90m 1 |[39m {[0m
[0m[31m[1m>[22m[39m[90m 2 |[39m [32m"id"[39m[33m:[39m [32m"baz"[39m[0m
[0m [90m |[39m [31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m [31m[1m👈🏽 Did you mean [95mbar[31m here?[22m[39m[0m
[0m [90m 3 |[39m }[0m",
" 1 | {
> 2 | "id": "baz"
| ^^^^^ 👈🏽 Did you mean [95mbar[39m here?
3 | }",
]
`;
Expand All @@ -125,9 +125,9 @@ exports[`Enum when value is an object prints correctly for no levenshtein match
"ENUM should be equal to one of the allowed values",
"(one, two)
",
"[0m [90m 1 |[39m {[0m
[0m[31m[1m>[22m[39m[90m 2 |[39m [32m"id"[39m[33m:[39m [32m"baz"[39m[0m
[0m [90m |[39m [31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m[31m[1m^[22m[39m [31m[1m👈🏽 Unexpected value, should be equal to one of the allowed values[22m[39m[0m
[0m [90m 3 |[39m }[0m",
" 1 | {
> 2 | "id": "baz"
| ^^^^^ 👈🏽 Unexpected value, should be equal to one of the allowed values
3 | }",
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ exports[`Required prints correctly for missing required prop 1`] = `
[
"REQUIRED should have required property 'id'
",
"[0m [90m 1 |[39m {[0m
[0m[31m[1m>[22m[39m[90m 2 |[39m [32m"nested"[39m[33m:[39m {}[0m
[0m [90m |[39m [31m[1m^[22m[39m [31m[1m☹️ [95mid[31m is missing here![22m[39m[0m
[0m [90m 3 |[39m }[0m",
" 1 | {
> 2 | "nested": {}
| ^ ☹️ [95mid[39m is missing here!
3 | }",
]
`;
Expand Down
14 changes: 13 additions & 1 deletion src/validation-errors/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,19 @@ export default class BaseValidationError {

getCodeFrame(message, dataPath = this.instancePath) {
return codeFrameColumns(this.jsonRaw, this.getLocation(dataPath), {
highlightCode: this.colorize,
/**
* `@babel/highlight`, by way of `@babel/code-frame`, highlights out entire block of raw JSON
* instead of just our `location` block -- so if you have a block of raw JSON that's upwards
* of 2mb+ and have a lot of errors to generate code frames for then we're re-highlighting
* the same huge chunk of code over and over and over and over again, all just so
* `@babel/code-frame` will eventually extract a small <10 line chunk out of it to return to
* us.
*
* Disabling `highlightCode` here will only disable highlighting the code we're showing users;
* if `options.colorize` is supplied to this library then the error message we're adding will
* still be highlighted.
*/
highlightCode: false,
message,
});
}
Expand Down

0 comments on commit 989af8f

Please sign in to comment.