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/spicy-kings-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'better-ajv-errors': major
---

Remove Chalk from the project, swapping to styleText from node.
3 changes: 1 addition & 2 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"

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.

This needs to be a major bump.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Will do.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

What else, if anything, should be added to help the pipeline test suite to understand the shift to node 20.12.0? There's several automated comments around styleText not existing, and I imagine it's flagging the changes as unmergable.

},
"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
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.

2 changes: 1 addition & 1 deletion renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"rangeStrategy": "replace",
"packageRules": [
{
"matchPackagePatterns": ["chalk", "leven"],
"matchPackagePatterns": ["leven"],
"enabled": false
}
]
Expand Down
19 changes: 16 additions & 3 deletions src/validation-errors/additional-prop.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import chalk from 'chalk';
import { styleText } from 'node:util';

import BaseValidationError from './base';

export default class AdditionalPropValidationError extends BaseValidationError {
Expand All @@ -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}`
)
);
Expand Down
13 changes: 8 additions & 5 deletions src/validation-errors/default.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
28 changes: 15 additions & 13 deletions src/validation-errors/enum.js
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -10,6 +10,9 @@
params: { allowedValues },
} = this.options;
const bestMatch = this.findBestMatch();
const codeFrameMessage = bestMatch !== null
? `👈🏽 Did you mean ${styleText('magentaBright', bestMatch)} here?`

Check failure on line 14 in src/validation-errors/enum.js

View workflow job for this annotation

GitHub Actions / Test (18.x)

src/validation-errors/__tests__/enum.js > Enum > when enum 'does not include' null > when value is a primitive > prints correctly for enum prop

TypeError: (0 , styleText) is not a function ❯ EnumValidationError.print src/validation-errors/enum.js:14:37 ❯ src/validation-errors/__tests__/enum.js:99:18

Check failure on line 14 in src/validation-errors/enum.js

View workflow job for this annotation

GitHub Actions / Test (18.x)

src/validation-errors/__tests__/enum.js > Enum > when enum 'includes' null > when value is an object > prints correctly for empty value

TypeError: (0 , styleText) is not a function ❯ EnumValidationError.print src/validation-errors/enum.js:14:37 ❯ src/validation-errors/__tests__/enum.js:68:18

Check failure on line 14 in src/validation-errors/enum.js

View workflow job for this annotation

GitHub Actions / Test (18.x)

src/validation-errors/__tests__/enum.js > Enum > when enum 'includes' null > when value is an object > prints correctly for enum prop

TypeError: (0 , styleText) is not a function ❯ EnumValidationError.print src/validation-errors/enum.js:14:37 ❯ src/validation-errors/__tests__/enum.js:33:18

Check failure on line 14 in src/validation-errors/enum.js

View workflow job for this annotation

GitHub Actions / Test (18.x)

src/validation-errors/__tests__/enum.js > Enum > when enum 'does not include' null > when value is an object > prints correctly for empty value

TypeError: (0 , styleText) is not a function ❯ EnumValidationError.print src/validation-errors/enum.js:14:37 ❯ src/validation-errors/__tests__/enum.js:68:18

Check failure on line 14 in src/validation-errors/enum.js

View workflow job for this annotation

GitHub Actions / Test (18.x)

src/validation-errors/__tests__/enum.js > Enum > when enum 'does not include' null > when value is an object > prints correctly for enum prop

TypeError: (0 , styleText) is not a function ❯ EnumValidationError.print src/validation-errors/enum.js:14:37 ❯ src/validation-errors/__tests__/enum.js:33:18

Check failure on line 14 in src/validation-errors/enum.js

View workflow job for this annotation

GitHub Actions / Test (18.x)

src/__tests__/index.js > Main > should output error with codeframe

TypeError: (0 , styleText) is not a function ❯ EnumValidationError.print src/validation-errors/enum.js:14:37 ❯ customErrorToText src/index.js:10:44 ❯ __vite_ssr_export_default__ src/index.js:20:25 ❯ src/__tests__/index.js:28:32

Check failure on line 14 in src/validation-errors/enum.js

View workflow job for this annotation

GitHub Actions / Test (18.x)

src/__tests__/index.js > Main > should output error with reconstructed codeframe

TypeError: (0 , styleText) is not a function ❯ EnumValidationError.print src/validation-errors/enum.js:14:37 ❯ customErrorToText src/index.js:10:44 ❯ __vite_ssr_export_default__ src/index.js:20:25 ❯ src/__tests__/index.js:14:32
: '👈🏽 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;
Expand All @@ -18,24 +21,23 @@
firstValue || '',
);

const output = [
chalk`{red {bold ENUM} ${message}}`,
chalk`{red (${allowedValuesMessage})}\n`,
];
const boldEnumLabel = styleText('bold', 'ENUM');

Check failure on line 24 in src/validation-errors/enum.js

View workflow job for this annotation

GitHub Actions / Test (18.x)

src/validation-errors/__tests__/enum.js > Enum > when enum 'does not include' null > when value is a primitive > prints correctly for no levenshtein match

TypeError: (0 , styleText) is not a function ❯ EnumValidationError.print src/validation-errors/enum.js:24:34 ❯ src/validation-errors/__tests__/enum.js:116:18

Check failure on line 24 in src/validation-errors/enum.js

View workflow job for this annotation

GitHub Actions / Test (18.x)

src/validation-errors/__tests__/enum.js > Enum > when enum 'includes' null > when value is an object > prints correctly for no levenshtein match

TypeError: (0 , styleText) is not a function ❯ EnumValidationError.print src/validation-errors/enum.js:24:34 ❯ src/validation-errors/__tests__/enum.js:51:18

Check failure on line 24 in src/validation-errors/enum.js

View workflow job for this annotation

GitHub Actions / Test (18.x)

src/validation-errors/__tests__/enum.js > Enum > when enum 'does not include' null > when value is an object > prints correctly for no levenshtein match

TypeError: (0 , styleText) is not a function ❯ EnumValidationError.print src/validation-errors/enum.js:24:34 ❯ src/validation-errors/__tests__/enum.js:51:18

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(),
Expand Down
20 changes: 13 additions & 7 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,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() {
Expand Down
Loading