Skip to content

Commit 43836df

Browse files
authored
feat: creation of a new cli-array format for hybrid output (#243)
* chore(deps-dev): bumping out of date deps * feat: creation of a new `cli-array` format for cli/array hybrids * fix: prettier problem
1 parent ff153fd commit 43836df

File tree

7 files changed

+851
-1640
lines changed

7 files changed

+851
-1640
lines changed

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
2+
"editor.defaultFormatter": "esbenp.prettier-vscode",
23
"editor.codeActionsOnSave": {
34
"source.fixAll": "explicit"
4-
}
5+
},
6+
"editor.formatOnSave": true
57
}

package-lock.json

Lines changed: 782 additions & 1608 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@
5151
"@babel/core": "^7.26.7",
5252
"@babel/plugin-transform-runtime": "^7.25.9",
5353
"@babel/preset-env": "^7.26.7",
54-
"@readme/eslint-config": "^14.1.2",
54+
"@readme/eslint-config": "^14.4.2",
5555
"@readme/openapi-schemas": "^3.1.0",
56-
"@vitest/coverage-v8": "^3.0.4",
56+
"@vitest/coverage-v8": "^3.0.7",
5757
"ajv": "^8.17.1",
5858
"babel-plugin-add-module-exports": "^1.0.4",
5959
"eslint": "^8.44.0",
6060
"prettier": "^3.4.2",
61-
"vitest": "^3.0.4"
61+
"vitest": "^3.0.7"
6262
},
6363
"peerDependencies": {
6464
"ajv": "4.11.8 - 8"

src/__tests__/__snapshots__/index.test.js.snap

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,35 @@ exports[`Main > complex schema examples > should output an error on an unrecogni
4141
20 | "responses": {"
4242
`;
4343
44-
exports[`Main > should output error with codeframe 1`] = `
44+
exports[`Main > given a format of \`cli\` > should output error with reconstructed codeframe [without colors] 1`] = `
45+
"ENUM must be equal to one of the allowed values
46+
(paragraph, codeBlock, blockquote)
47+
48+
4 | "content": [
49+
5 | {
50+
> 6 | "type": "paragarph"
51+
| ^^^^^^^^^^^ Did you mean paragraph here?
52+
7 | }
53+
8 | ]
54+
9 | }"
55+
`;
56+
57+
exports[`Main > given a format of \`cli\` > should output error with reconstructed codeframe 1`] = `
4558
"ENUM must be equal to one of the allowed values
4659
(paragraph, codeBlock, blockquote)
4760
48-
2 | "type": "doc",
49-
3 | "version": 1,
50-
> 4 | "content": [{ "type": "paragarph" }]
51-
| ^^^^^^^^^^^ Did you mean paragraph here?
52-
5 | }
53-
6 |"
61+
4 | "content": [
62+
5 | {
63+
> 6 | "type": "paragarph"
64+
| ^^^^^^^^^^^ Did you mean paragraph here?
65+
7 | }
66+
8 | ]
67+
9 | }"
5468
`;
5569
56-
exports[`Main > should output error with reconstructed codeframe [without colors] 1`] = `
57-
"ENUM must be equal to one of the allowed values
70+
exports[`Main > given a format of \`cli-array\` > should output error with reconstructed codeframe [without colors] 1`] = `
71+
[
72+
"ENUM must be equal to one of the allowed values
5873
(paragraph, codeBlock, blockquote)
5974
6075
4 | "content": [
@@ -63,11 +78,13 @@ exports[`Main > should output error with reconstructed codeframe [without colors
6378
| ^^^^^^^^^^^ Did you mean paragraph here?
6479
7 | }
6580
8 | ]
66-
9 | }"
81+
9 | }",
82+
]
6783
`;
6884
69-
exports[`Main > should output error with reconstructed codeframe 1`] = `
70-
"ENUM must be equal to one of the allowed values
85+
exports[`Main > given a format of \`cli-array\` > should output error with reconstructed codeframe 1`] = `
86+
[
87+
"ENUM must be equal to one of the allowed values
7188
(paragraph, codeBlock, blockquote)
7289
7390
4 | "content": [
@@ -76,5 +93,18 @@ exports[`Main > should output error with reconstructed codeframe 1`] = `
7693
| ^^^^^^^^^^^ Did you mean paragraph here?
7794
7 | }
7895
8 | ]
79-
9 | }"
96+
9 | }",
97+
]
98+
`;
99+
100+
exports[`Main > should output error with codeframe 1`] = `
101+
"ENUM must be equal to one of the allowed values
102+
(paragraph, codeBlock, blockquote)
103+
104+
2 | "type": "doc",
105+
3 | "version": 1,
106+
> 4 | "content": [{ "type": "paragarph" }]
107+
| ^^^^^^^^^^^ Did you mean paragraph here?
108+
5 | }
109+
6 |"
80110
`;

src/__tests__/index.test.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,25 @@ import betterAjvErrorsBabelExport from '../../lib';
88
import { getSchemaAndData } from '../test-helpers';
99

1010
describe('Main', () => {
11-
it.each([
12-
['should output error with reconstructed codeframe', true],
13-
['should output error with reconstructed codeframe [without colors]', false],
14-
])('%s', async (_, colorize) => {
15-
const [schema, data] = await getSchemaAndData('default', __dirname);
16-
const ajv = new Ajv();
17-
const validate = ajv.compile(schema);
18-
const valid = validate(data);
19-
expect(valid).toBe(false);
11+
describe.each(['cli', 'cli-array'])('given a format of `%s`', format => {
12+
it.each([
13+
['should output error with reconstructed codeframe', true],
14+
['should output error with reconstructed codeframe [without colors]', false],
15+
])('%s', async (_, colorize) => {
16+
const [schema, data] = await getSchemaAndData('default', __dirname);
17+
const ajv = new Ajv();
18+
const validate = ajv.compile(schema);
19+
const valid = validate(data);
20+
expect(valid).toBe(false);
2021

21-
const res = betterAjvErrors(schema, data, validate.errors, {
22-
colorize,
23-
format: 'cli',
24-
indent: 2,
22+
const res = betterAjvErrors(schema, data, validate.errors, {
23+
colorize,
24+
format,
25+
indent: 2,
26+
});
27+
28+
expect(res).toMatchSnapshot();
2529
});
26-
expect(res).toMatchSnapshot();
2730
});
2831

2932
it('should output error with codeframe', async () => {

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export default function betterAjvErrors(schema, data, errors, options = {}) {
2020

2121
if (format === 'cli') {
2222
return customErrors.map(customErrorToText).join('\n\n');
23+
} else if (format === 'cli-array') {
24+
return customErrors.map(customErrorToText);
2325
}
2426

2527
return customErrors.map(customErrorToStructure);

typings.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface IOutputError {
99
}
1010

1111
export interface IInputOptions {
12-
format?: 'cli' | 'js';
12+
format?: 'cli-array' | 'cli' | 'js';
1313
indent?: number | null;
1414

1515
/** Raw JSON used when highlighting error location */

0 commit comments

Comments
 (0)