Open phochste opened 8 years ago
This will probably be implemented as a fix condition
dev has a new Fix::Condition::validate
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.
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
E.g. something like
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.