Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't highlight code examples in returned error messages #166

Merged
merged 1 commit into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

> 1 | {"type":"doc","version":1,"content":[{"type":"paragarph"}]}
  | ^^^^^^^^^^^ 👈🏽 Did you mean paragraph here?"
> 1 | {"type":"doc","version":1,"content":[{"type":"paragarph"}]}
| ^^^^^^^^^^^ 👈🏽 Did you mean paragraph 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

  15 | "summary": "Example operation",
  16 | "description": "This operation has \`tags\` typod.",
> 17 | "tagss": [
  | ^^^^^^^ 😲 tagss is not expected to be here!
  18 | "Tag Name"
  19 | ],
  20 | "responses": {"
15 | "summary": "Example operation",
16 | "description": "This operation has \`tags\` typod.",
> 17 | "tagss": [
| ^^^^^^^ 😲 tagss 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)

  2 | "type": "doc",
  3 | "version": 1,
> 4 | "content": [{ "type": "paragarph" }]
  | ^^^^^^^^^^^ 👈🏽 Did you mean paragraph here?
  5 | }
  6 |"
2 | "type": "doc",
3 | "version": 1,
> 4 | "content": [{ "type": "paragarph" }]
| ^^^^^^^^^^^ 👈🏽 Did you mean paragraph 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)

  4 | "content": [
  5 | {
> 6 | "type": "paragarph"
  | ^^^^^^^^^^^ 👈🏽 Did you mean paragraph here?
  7 | }
  8 | ]
  9 | }"
4 | "content": [
5 | {
> 6 | "type": "paragarph"
| ^^^^^^^^^^^ 👈🏽 Did you mean paragraph 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)
",
"> 1 | "baz"
  | ^^^^^ 👈🏽 Did you mean bar here?",
"> 1 | "baz"
| ^^^^^ 👈🏽 Did you mean bar 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)
",
"> 1 | "baz"
  | ^^^^^ 👈🏽 Did you mean bar here?",
"> 1 | "baz"
| ^^^^^ 👈🏽 Did you mean bar 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)
",
"> 1 | "baz"
  | ^^^^^ 👈🏽 Unexpected value, should be equal to one of the allowed values",
"> 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)
",
"  1 | {
> 2 | "id": "baz"
  | ^^^^^ 👈🏽 Did you mean bar here?
  3 | }",
" 1 | {
> 2 | "id": "baz"
| ^^^^^ 👈🏽 Did you mean bar 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)
",
"  1 | {
> 2 | "id": "baz"
  | ^^^^^ 👈🏽 Did you mean bar here?
  3 | }",
" 1 | {
> 2 | "id": "baz"
| ^^^^^ 👈🏽 Did you mean bar 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)
",
"  1 | {
> 2 | "id": "baz"
  | ^^^^^ 👈🏽 Unexpected value, should be equal to one of the allowed values
  3 | }",
" 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'
",
"  1 | {
> 2 | "nested": {}
  | ^ ☹️ id is missing here!
  3 | }",
" 1 | {
> 2 | "nested": {}
| ^ ☹️ id 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