Catmandu icon indicating copy to clipboard operation
Catmandu copied to clipboard

Make the Catmandu::Validator part of the command line tools

Open phochste opened this issue 8 years ago • 4 comments

E.g. something like

$ catmandu import JSON to MongoDB with JSONSchema --schema data/myschema.json < data.json

This will send all items, after optional fixes, before being stored in MongoDB, through a JSONSchema validator and reject any records that are not valid.

phochste avatar Oct 31 '16 05:10 phochste

This will probably be implemented as a fix condition

nics avatar Mar 20 '17 13:03 nics

dev has a new Fix::Condition::validate

nics avatar Mar 21 '17 09:03 nics

Validation is still complicated to set up. Workflows based on validation results can be created with fixes but the basic use case "are these records valid or not (and if not why)?" is still difficult to set up. The best I could find is:

catmandu convert JSON to JSON --fix "validate('', JSONSchema, schema: schema.json); retain(errors)" < data.json

But the exit code is not set on validation error. A dedicated validate command should be useful for instance like this:

catmandu validate JSON with JSONSchema --schema schema.json < data.json

~~This requires a way to throw custom errors from a fix as implemented in https://github.com/LibreCat/Catmandu/pull/334. With this fix,~~ the validate command could be a shortcut for such fix:

 validate('author', JSONSchema, schema: 'my/schema.json')
 if exists(errors) join(errors,"\n"); error($.errors) end

If no errors are found, I expect no output (Null exporter), otherwise error exit code.

nichtich avatar Feb 22 '18 13:02 nichtich

Validating on the command line is also not feasible because the error fix will quit immediately. Validating a single record works this way:

catmandu convert Null to JSON -fix 'validate(.,Mock,reject:1);if exists(errors) join(errors,"\n");error($.errors) end' || echo ERROR

Same with validate command could be:

catmandu validate Null with Mock --reject 1 || echo ERROR

nichtich avatar Sep 22 '21 07:09 nichtich