diff --git a/.changeset/spicy-kings-obey.md b/.changeset/spicy-kings-obey.md new file mode 100644 index 00000000..6d16e0ed --- /dev/null +++ b/.changeset/spicy-kings-obey.md @@ -0,0 +1,5 @@ +--- +'better-ajv-errors': major +--- + +Remove Chalk from the project, swapping to styleText from node. diff --git a/package.json b/package.json index 46e839ea..a2590f10 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ }, "module": "./lib/esm/index.mjs", "engines": { - "node": ">= 18.20.6" + "node": ">= 20.12.0" }, "keywords": [ "json-schema", @@ -58,7 +58,6 @@ "dependencies": { "@babel/code-frame": "^7.27.1", "@humanwhocodes/momoa": "^2.0.4", - "chalk": "^4.1.2", "jsonpointer": "^5.0.1", "leven": "^3.1.0 < 4" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 01175138..97db7cf4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -26,9 +26,6 @@ importers: '@humanwhocodes/momoa': specifier: ^2.0.4 version: 2.0.4 - chalk: - specifier: ^4.1.2 - version: 4.1.2 jsonpointer: specifier: ^5.0.1 version: 5.0.1 diff --git a/renovate.json b/renovate.json index f02d06f2..dca675ff 100644 --- a/renovate.json +++ b/renovate.json @@ -5,7 +5,7 @@ "rangeStrategy": "replace", "packageRules": [ { - "matchPackagePatterns": ["chalk", "leven"], + "matchPackagePatterns": ["leven"], "enabled": false } ] diff --git a/src/validation-errors/additional-prop.js b/src/validation-errors/additional-prop.js index ba590720..f3a7d54d 100644 --- a/src/validation-errors/additional-prop.js +++ b/src/validation-errors/additional-prop.js @@ -1,4 +1,5 @@ -import chalk from 'chalk'; +import { styleText } from 'node:util'; + import BaseValidationError from './base'; export default class AdditionalPropValidationError extends BaseValidationError { @@ -9,11 +10,23 @@ export default class AdditionalPropValidationError extends BaseValidationError { print() { const { message, params } = this.options; - const output = [chalk`{red {bold ADDITIONAL PROPERTY} ${message}}\n`]; + + const boldAdditionalPropertiesLabel = styleText( + 'bold', + 'ADDTIONAL PROPERTY', + ); + const magentaAdditionalProperty = styleText( + ['magentaBright'], + params.additionalProperty, + ); + + const output = [ + styleText('red', `${boldAdditionalPropertiesLabel} ${message}\n`), + ]; return output.concat( this.getCodeFrame( - chalk`😲 {magentaBright ${params.additionalProperty}} is not expected to be here!`, + `😲 ${magentaAdditionalProperty} is not expected to be here!`, `${this.instancePath}/${params.additionalProperty}` ) ); diff --git a/src/validation-errors/default.js b/src/validation-errors/default.js index a00df894..15c3a7a2 100644 --- a/src/validation-errors/default.js +++ b/src/validation-errors/default.js @@ -1,14 +1,17 @@ -import chalk from 'chalk'; +import { styleText } from 'node:util'; + import BaseValidationError from './base'; export default class DefaultValidationError extends BaseValidationError { print() { const { keyword, message } = this.options; - const output = [chalk`{red {bold ${keyword.toUpperCase()}} ${message}}\n`]; + const boldUpperKeyword = styleText('bold', keyword.toUpperCase()); + const magentaBrightKeyword = styleText('magentaBright', keyword); - return output.concat( - this.getCodeFrame(chalk`👈đŸŊ {magentaBright ${keyword}} ${message}`) - ); + return [ + styleText('red', `${boldUpperKeyword} ${message}\n`), + this.getCodeFrame(`👈đŸŊ ${magentaBrightKeyword} ${message}`) + ]; } getError() { diff --git a/src/validation-errors/enum.js b/src/validation-errors/enum.js index 8e6a33a5..f5dfc866 100644 --- a/src/validation-errors/enum.js +++ b/src/validation-errors/enum.js @@ -1,5 +1,5 @@ -import chalk from 'chalk'; import leven from 'leven'; +import { styleText } from 'node:util'; import pointer from 'jsonpointer'; import BaseValidationError from './base'; @@ -10,6 +10,9 @@ export default class EnumValidationError extends BaseValidationError { params: { allowedValues }, } = this.options; const bestMatch = this.findBestMatch(); + const codeFrameMessage = bestMatch !== null + ? `👈đŸŊ Did you mean ${styleText('magentaBright', bestMatch)} here?` + : '👈đŸŊ Unexpected value, should be equal to one of the allowed values'; // Needed to handle nullable enums, as joining on null just prints ", " const [firstValue, ...rest] = allowedValues; @@ -18,24 +21,23 @@ export default class EnumValidationError extends BaseValidationError { firstValue || '', ); - const output = [ - chalk`{red {bold ENUM} ${message}}`, - chalk`{red (${allowedValuesMessage})}\n`, - ]; + const boldEnumLabel = styleText('bold', 'ENUM'); - return output.concat( - this.getCodeFrame( - bestMatch !== null - ? chalk`👈đŸŊ Did you mean {magentaBright ${bestMatch}} here?` - : chalk`👈đŸŊ Unexpected value, should be equal to one of the allowed values` - ) - ); + return [ + styleText('red', `${boldEnumLabel} ${message}`), + styleText('red', `(${allowedValuesMessage})\n`), + this.getCodeFrame(codeFrameMessage), + ] } getError() { const { message, params } = this.options; const bestMatch = this.findBestMatch(); - const allowedValues = params.allowedValues.join(', '); + const [firstValue, ...rest] = params.allowedValues; + const allowedValues = rest.reduce( + (acc, value) => `${acc}, ${value}`, + firstValue || '', + ); const output = { ...this.getLocation(), diff --git a/src/validation-errors/required.js b/src/validation-errors/required.js index 9a438fc0..c055e78f 100644 --- a/src/validation-errors/required.js +++ b/src/validation-errors/required.js @@ -1,4 +1,4 @@ -import chalk from 'chalk'; +import { styleText } from 'node:util'; import BaseValidationError from './base'; export default class RequiredValidationError extends BaseValidationError { @@ -9,13 +9,19 @@ export default class RequiredValidationError extends BaseValidationError { print() { const { message, params } = this.options; - const output = [chalk`{red {bold REQUIRED} ${message}}\n`]; - return output.concat( - this.getCodeFrame( - chalk`â˜šī¸ {magentaBright ${params.missingProperty}} is missing here!` - ) - ); + const boldRequiredLabel = styleText('bold', 'REQUIRED'); + const magentaMissingProperty = styleText( + 'magentaBright', + params.missingProperty, + ); + + return [ + styleText('red', `${boldRequiredLabel} ${message}\n`), + this.getCodeFrame( + `â˜šī¸ ${magentaMissingProperty} is missing here!`, + ), + ]; } getError() {