From 1e290293c21a2650b2a809f7b771ae8e64f0fb72 Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Sun, 26 Mar 2023 17:31:18 -0700 Subject: [PATCH] fix: don't highlight code examples in returned error messages --- .../__snapshots__/index.test.js.snap | 44 +++++++++---------- .../__tests__/__snapshots__/enum.test.js.snap | 36 +++++++-------- .../__snapshots__/required.test.js.snap | 8 ++-- src/validation-errors/base.js | 14 +++++- 4 files changed, 57 insertions(+), 45 deletions(-) diff --git a/src/__tests__/__snapshots__/index.test.js.snap b/src/__tests__/__snapshots__/index.test.js.snap index cb94e829..806be8ee 100644 --- a/src/__tests__/__snapshots__/index.test.js.snap +++ b/src/__tests__/__snapshots__/index.test.js.snap @@ -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`] = ` @@ -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 | }" `; diff --git a/src/validation-errors/__tests__/__snapshots__/enum.test.js.snap b/src/validation-errors/__tests__/__snapshots__/enum.test.js.snap index f4bf5031..a1eab309 100644 --- a/src/validation-errors/__tests__/__snapshots__/enum.test.js.snap +++ b/src/validation-errors/__tests__/__snapshots__/enum.test.js.snap @@ -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?", ] `; @@ -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?", ] `; @@ -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", ] `; @@ -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 | }", ] `; @@ -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 | }", ] `; @@ -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 | }", ] `; diff --git a/src/validation-errors/__tests__/__snapshots__/required.test.js.snap b/src/validation-errors/__tests__/__snapshots__/required.test.js.snap index a958afae..7d692651 100644 --- a/src/validation-errors/__tests__/__snapshots__/required.test.js.snap +++ b/src/validation-errors/__tests__/__snapshots__/required.test.js.snap @@ -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 | }", ] `; diff --git a/src/validation-errors/base.js b/src/validation-errors/base.js index 75acbd34..22c9b377 100644 --- a/src/validation-errors/base.js +++ b/src/validation-errors/base.js @@ -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, }); }