Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/replace-chalk-with-styletext.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"module": "./lib/esm/index.mjs",
"engines": {
"node": ">= 18.20.6"
"node": ">= 20.12.0"
},
"keywords": [
"json-schema",
Expand Down Expand Up @@ -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"
},
Expand Down Expand Up @@ -88,4 +87,4 @@
"peerDependencies": {
"ajv": "4.11.8 - 8"
}
}
}
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 20 additions & 20 deletions src/json/__tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ exports[`JSON > can work on JSON with Array 1`] = `
"end": {
"column": 19,
"line": 6,
"offset": 60,
"offset": 65,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @kbrahmateja, sorry about the late review :(

I was wondering if we can avoid updating these offsets.

},
"start": {
"column": 14,
"line": 6,
"offset": 55,
"offset": 60,
},
},
"type": "String",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
13 changes: 7 additions & 6 deletions src/validation-errors/additional-prop.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import chalk from 'chalk';
import { styleText } from 'node:util';
import BaseValidationError from './base';

export default class AdditionalPropValidationError extends BaseValidationError {
Expand All @@ -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}`
)
);
Expand All @@ -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,
};
}
Expand Down
10 changes: 7 additions & 3 deletions src/validation-errors/default.js
Original file line number Diff line number Diff line change
@@ -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
)
);
}

Expand Down
10 changes: 5 additions & 5 deletions src/validation-errors/enum.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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'
)
);
}
Expand Down
8 changes: 5 additions & 3 deletions src/validation-errors/required.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import chalk from 'chalk';
import { styleText } from 'node:util';
import BaseValidationError from './base';

export default class RequiredValidationError extends BaseValidationError {
Expand All @@ -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!'
)
);
}
Expand Down