better-ajv-errors
better-ajv-errors copied to clipboard
Feature: Option to include data path in CLI output
Data paths are great when working with big, repeating or complex responses, but at the moment these paths are not included in the CLI output. It would also be very useful if you don't have the indent
option set.
I'd like to have an option to enable data path output in the CLI mode. It might look something like.
const output = betterAjvErrors(schema, response.body, ajv.errors, {
indent: 2,
dataPath: true,
});
console.log(output);
Error: FORMAT should match format "date-time"
5 | "foo": "624050509",
6 | "bar": "04",
> 7 | "baz": "not-a-date-time",
| ^^^^^^^^^^^^^^^^^ 👈🏽 format should match format "date-time"
8 | "lorum": "@",
9 | "ipsum": "some string",
10 | "razz": 100797.51,
@ /a/long/path/9/with/2/big/21/arrays/baz
And sure, the example above is a really extreme example, and if your API looks like this you have bigger problems. But these things exist in the wild, so I'd like to at least be able to test them with some ease. This feature would contribute to this.
I'm willing to implement this myself if someone can tell me where to start :)
Hi @Lakitna, do you still want to work on this? And, how can I help you? :)
@torifat If you can point me to where I should probably start I can probably figure things out from there.
You can refactor getCodeFrame
to use this.options.dataPath
and print the path with the code frame.
https://github.com/atlassian/better-ajv-errors/blob/126ee2e45ee829e69e3b2f3f6d56eb49d5c42135/src/validation-errors/base.js#L34
Just a heads up, I'm converting the whole project to TypeScript: https://github.com/torifat/better-ajv-errors/tree/typescript. So, you welcome to make this change in either place.
I looked at this today and got it working (see screenshot below). But right now without the option since there does not seem to be an existing way to pass options to that part of the code. Should I create a way to pass the option or just make this the default behaviour without a way to disable it? I prefer to make this default as it is useful information.
Once we've made a decision I'll implement it and work on the tests.
I was planning to make it a default behavior for v1 :)
Some tests are failing and it seems to be unrelated to my changes. Can you make sense of errors like the one below? These tests already failed before I changed anything. It's probably an OS thing. I'm running Windows 10 and these tests don't fail on CI.
Probably be a good idea to fix this if I can.
Edit: Traced it back to json-to-ast
, probably not fixable.
If doesn't fail in CI then feel free to create the PR. I will follow up on the json-to-ast
thing. Also, I was planning to replace it anyways. See #51
In that case, everything is done but the documentation.
I've tried to update the screenshots, but that does not seem to work on Windows. Can you take care of that for me?
Can you take care of that for me?
Sure, I can do that :)
Just a heads up that this is ready for merge after the screenshot update as far as I'm concerned.