ajv-cli icon indicating copy to clipboard operation
ajv-cli copied to clipboard

Merging Defaults with CLI

Open mvastola opened this issue 4 years ago • 2 comments

What version of Ajv you are you using? [email protected]

What problem do you want to solve? I'm actually not sure if I'm missing a setting or just getting confused by the docs, but I was hoping to use the cli and the --use-defaults option to produce the merged JSON output, as is done in scripting.

What do you think is the correct solution to problem? Based on the documentation indicating this function modified data, I expected the cli to either edit the data file in place, or else output the combined JSON (either would be fine).

The closest thing currently possible appears to be returning a JSON patch which would yield the data complete with defaults, but a) I can't seem to find any CLI tools that apply JSON patches and b) It seems like it would be a bit hacky in any case as the result of the validation is mixed into stdout with the diff.

As for implementing this, the paths of least resistance would seem to be supporting the -o argument in validations and/or supporting a merged value for the --changes argument. I would also suggest moving status messages about validation to standard error.

Will you be able to implement it? Probably not due to time constraints and my lack of familiarity with the code, but I suspect this would be fairly trivial as this is already possible via the api.

mvastola avatar Sep 24 '21 05:09 mvastola

Correct - currently use-defaults option only takes defaults into account when validating, but the modified object is not used.

-o option to print resulting object is ok as the idea. I don’t think I follow the idea about “merged” for changes.

Re moving errors to stderr is a good suggestion, but it’s a breaking change. Also it’s not necessary for this change - there would be either errors or output (and there will be a different exit code) - as with compile, when the proposed -o option is used there should be no other output.

PR is welcome.

epoberezkin avatar Sep 24 '21 07:09 epoberezkin

-o option to print resulting object is ok as the idea. I don’t think I follow the idea about “merged” for changes.

I just meant that instead of printing the patch it could print the data with the patch applied. That might have fit in better with existing functionality.

Re moving errors to stderr is a good suggestion, but it’s a breaking change. Also it’s not necessary for this change - there would be either errors or output (and there will be a different exit code)

I agree it's not necessary if -o is added (at least for my purposes) but currently a correct validation with the changes outputted prints both the patch and x.json is valid to stdout. So if it passes (per the exit code) and you want to do something with the patch, you have to extract it from the status info.

as with compile, when the proposed -o option is used there should be no other output.

Up to you. Normally I'd expect the status to still be printed out (albeit to stderr) unless there was a quiet/-q flag available and set. Not too big of a deal as long as the status isn't written to the file though, as if you're scripting the status message doesn't matter.

PR is welcome.

I'll see if I have a chance. It doesn't seem like too big a change. Someone with more familiarity would be able to do it pretty quickly though, should some such person happen upon this and have a couple of spare minutes.

mvastola avatar Sep 24 '21 18:09 mvastola