diff --git a/.changeset/replace-chalk-with-styletext.md b/.changeset/replace-chalk-with-styletext.md new file mode 100644 index 00000000..36f16df1 --- /dev/null +++ b/.changeset/replace-chalk-with-styletext.md @@ -0,0 +1,5 @@ +--- +"better-ajv-errors": major +--- + +Replace `chalk` with Node.js built-in `util.styleText`. This change requires Node.js >= 20.12.0. diff --git a/package.json b/package.json index 9a2d1b63..f5aa8738 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" }, @@ -88,4 +87,4 @@ "peerDependencies": { "ajv": "4.11.8 - 8" } -} +} \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c56bf991..3f42a826 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/src/json/__tests__/__snapshots__/index.js.snap b/src/json/__tests__/__snapshots__/index.js.snap index 02993e21..6acdabd2 100644 --- a/src/json/__tests__/__snapshots__/index.js.snap +++ b/src/json/__tests__/__snapshots__/index.js.snap @@ -6,12 +6,12 @@ exports[`JSON > can work on JSON with Array 1`] = ` "end": { "column": 19, "line": 6, - "offset": 60, + "offset": 65, }, "start": { "column": 14, "line": 6, - "offset": 55, + "offset": 60, }, }, "type": "String", @@ -25,12 +25,12 @@ exports[`JSON > can work on JSON with Array 2`] = ` "end": { "column": 12, "line": 6, - "offset": 53, + "offset": 58, }, "start": { "column": 7, "line": 6, - "offset": 48, + "offset": 53, }, }, "type": "String", @@ -46,12 +46,12 @@ exports[`JSON > can work on JSON with a key named meta 1`] = ` "end": { "column": 19, "line": 4, - "offset": 48, + "offset": 51, }, "start": { "column": 15, "line": 4, - "offset": 44, + "offset": 47, }, }, "type": "Boolean", @@ -65,12 +65,12 @@ exports[`JSON > can work on JSON with a key named meta 2`] = ` "end": { "column": 13, "line": 4, - "offset": 42, + "offset": 45, }, "start": { "column": 5, "line": 4, - "offset": 34, + "offset": 37, }, }, "type": "String", @@ -84,12 +84,12 @@ exports[`JSON > can work on JSON with a key named value 1`] = ` "end": { "column": 14, "line": 3, - "offset": 31, + "offset": 33, }, "start": { "column": 12, "line": 3, - "offset": 29, + "offset": 31, }, }, "type": "Number", @@ -103,12 +103,12 @@ exports[`JSON > can work on JSON with a key named value 2`] = ` "end": { "column": 10, "line": 3, - "offset": 27, + "offset": 29, }, "start": { "column": 3, "line": 3, - "offset": 20, + "offset": 22, }, }, "type": "String", @@ -122,12 +122,12 @@ exports[`JSON > can work on simple JSON 1`] = ` "end": { "column": 15, "line": 2, - "offset": 16, + "offset": 17, }, "start": { "column": 10, "line": 2, - "offset": 11, + "offset": 12, }, }, "type": "String", @@ -141,12 +141,12 @@ exports[`JSON > can work on simple JSON 2`] = ` "end": { "column": 8, "line": 2, - "offset": 9, + "offset": 10, }, "start": { "column": 3, "line": 2, - "offset": 4, + "offset": 5, }, }, "type": "String", @@ -160,12 +160,12 @@ exports[`JSON > can work with unescaped JSON pointers with ~0 1`] = ` "end": { "column": 17, "line": 7, - "offset": 93, + "offset": 99, }, "start": { "column": 16, "line": 7, - "offset": 92, + "offset": 98, }, }, "type": "Number", @@ -179,12 +179,12 @@ exports[`JSON > can work with unescaped JSON pointers with ~1 1`] = ` "end": { "column": 17, "line": 4, - "offset": 49, + "offset": 52, }, "start": { "column": 16, "line": 4, - "offset": 48, + "offset": 51, }, }, "type": "Number", diff --git a/src/validation-errors/additional-prop.js b/src/validation-errors/additional-prop.js index c680f8a5..21e76a1f 100644 --- a/src/validation-errors/additional-prop.js +++ b/src/validation-errors/additional-prop.js @@ -1,4 +1,4 @@ -import chalk from 'chalk'; +import { styleText } from 'node:util'; import BaseValidationError from './base'; export default class AdditionalPropValidationError extends BaseValidationError { @@ -9,11 +9,13 @@ export default class AdditionalPropValidationError extends BaseValidationError { print() { const { message, params } = this.options; - const output = [chalk`{red {bold ADDTIONAL PROPERTY} ${message}}\n`]; + const output = [ + styleText('red', styleText('bold', 'ADDTIONAL PROPERTY') + ' ' + message) + '\n', + ]; return output.concat( this.getCodeFrame( - chalk`😲 {magentaBright ${params.additionalProperty}} is not expected to be here!`, + '😲 ' + styleText('magentaBright', params.additionalProperty) + ' is not expected to be here!', `${this.instancePath}/${params.additionalProperty}` ) ); @@ -24,9 +26,8 @@ export default class AdditionalPropValidationError extends BaseValidationError { return { ...this.getLocation(`${this.instancePath}/${params.additionalProperty}`), - error: `${this.getDecoratedPath()} Property ${ - params.additionalProperty - } is not expected to be here`, + error: `${this.getDecoratedPath()} Property ${params.additionalProperty + } is not expected to be here`, path: this.instancePath, }; } diff --git a/src/validation-errors/default.js b/src/validation-errors/default.js index a00df894..8339bd26 100644 --- a/src/validation-errors/default.js +++ b/src/validation-errors/default.js @@ -1,13 +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 output = [ + styleText('red', styleText('bold', keyword.toUpperCase()) + ' ' + message) + '\n', + ]; return output.concat( - this.getCodeFrame(chalk`👈đŸŊ {magentaBright ${keyword}} ${message}`) + this.getCodeFrame( + '👈đŸŊ ' + styleText('magentaBright', keyword) + ' ' + message + ) ); } diff --git a/src/validation-errors/enum.js b/src/validation-errors/enum.js index 22d8d192..5e0f1e89 100644 --- a/src/validation-errors/enum.js +++ b/src/validation-errors/enum.js @@ -1,4 +1,4 @@ -import chalk from 'chalk'; +import { styleText } from 'node:util'; import leven from 'leven'; import pointer from 'jsonpointer'; import BaseValidationError from './base'; @@ -12,15 +12,15 @@ export default class EnumValidationError extends BaseValidationError { const bestMatch = this.findBestMatch(); const output = [ - chalk`{red {bold ENUM} ${message}}`, - chalk`{red (${allowedValues.join(', ')})}\n`, + styleText('red', styleText('bold', 'ENUM') + ' ' + message), + styleText('red', '(' + allowedValues.join(', ') + ')') + '\n', ]; 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` + ? '👈đŸŊ Did you mean ' + styleText('magentaBright', bestMatch) + ' here?' + : '👈đŸŊ Unexpected value, should be equal to one of the allowed values' ) ); } diff --git a/src/validation-errors/required.js b/src/validation-errors/required.js index 9a438fc0..eb6fb5e6 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,11 +9,13 @@ export default class RequiredValidationError extends BaseValidationError { print() { const { message, params } = this.options; - const output = [chalk`{red {bold REQUIRED} ${message}}\n`]; + const output = [ + styleText('red', styleText('bold', 'REQUIRED') + ' ' + message) + '\n', + ]; return output.concat( this.getCodeFrame( - chalk`â˜šī¸ {magentaBright ${params.missingProperty}} is missing here!` + 'â˜šī¸ ' + styleText('magentaBright', params.missingProperty) + ' is missing here!' ) ); }