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
4 changes: 4 additions & 0 deletions .changeset/warm-donkeys-applaud/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"releases": [{ "name": "better-ajv-errors", "type": "minor" }],
"dependents": []
}
1 change: 1 addition & 0 deletions .changeset/warm-donkeys-applaud/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Include data path in CLI output
2 changes: 1 addition & 1 deletion media/screenshot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 9 additions & 3 deletions src/validation-errors/__tests__/__snapshots__/enum.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ Array [
"  1 | {
> 2 |  \\"id\\": \\"baz\\"
  |  ^^^^^ 👈🏽 Did you mean bar here?
  3 | }",
  3 | }

@ /id",
]
`;

Expand All @@ -50,7 +52,9 @@ Array [
"  1 | {
> 2 |  \\"id\\": \\"baz\\"
  |  ^^^^^ 👈🏽 Did you mean bar here?
  3 | }",
  3 | }

@ /id",
]
`;

Expand All @@ -62,6 +66,8 @@ Array [
"  1 | {
> 2 |  \\"id\\": \\"baz\\"
  |  ^^^^^ 👈🏽 Unexpected value, should be equal to one of the allowed values
  3 | }",
  3 | }

@ /id",
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Array [
"  1 | {
> 2 |  \\"nested\\": {}
  |  ^ ☹️ id is missing here!
  3 | }",
  3 | }

@ /nested",
]
`;
18 changes: 14 additions & 4 deletions src/validation-errors/base.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { codeFrameColumns } from '@babel/code-frame';
import { getMetaFromPath, getDecoratedDataPath } from '../json';
import chalk from 'chalk';

export default class BaseValidationError {
constructor(
Expand Down Expand Up @@ -32,10 +33,19 @@ export default class BaseValidationError {
}

getCodeFrame(message, dataPath = this.options.dataPath) {
return codeFrameColumns(this.jsonRaw, this.getLocation(dataPath), {
highlightCode: true,
message,
});
const formattedDataPath = dataPath
? chalk`\n\n {yellow @} {grey ${dataPath}}`
: '';
const codeFrame = codeFrameColumns(
this.jsonRaw,
this.getLocation(dataPath),
{
highlightCode: true,
message,
}
);

return codeFrame + formattedDataPath;
}

print() {
Expand Down