Skip to content

Commit

Permalink
validate: validating against a JSONschema requires headers
Browse files Browse the repository at this point in the history
  • Loading branch information
jqnatividad committed Jun 30, 2024
1 parent 06180a6 commit 6164382
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/cmd/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,12 @@ Validate options:
Common options:
-h, --help Display this message
-n, --no-headers When set, the first row will not be interpreted
as headers. Namely, it will be sorted with the rest
as headers. It will be validated with the rest
of the rows. Otherwise, the first row will always
appear as the header row in the output.
Note that this option is only valid when running
in RFC 4180 validation mode as JSON Schema validation
requires headers.
-d, --delimiter <arg> The field delimiter for reading CSV data.
Must be a single character. [default: ,]
-p, --progressbar Show progress bars. Not valid for stdin.
Expand Down Expand Up @@ -463,6 +466,12 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
return Ok(());
}

// if we're here, we're validating with a JSON Schema
// JSONSchema validation requires headers
if args.flag_no_headers {
return fail_clierror!("Cannot validate CSV without headers against a JSON Schema.");
}

// prep progress bar
#[cfg(any(feature = "feature_capable", feature = "lite"))]
let progress = ProgressBar::with_draw_target(None, ProgressDrawTarget::stderr_with_hz(5));
Expand Down
26 changes: 26 additions & 0 deletions tests/test_validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,32 @@ fn validate_adur_public_toilets_dataset_with_json_schema_valid_output() {
wrk.assert_err(&mut cmd);
}

#[test]
fn validate_with_schema_noheader() {
let wrk = Workdir::new("validate_with_schema_noheader").flexible(true);

// copy schema file to workdir
let schema: String = wrk.load_test_resource("public-toilets-schema.json");
wrk.create_from_string("schema.json", &schema);

// copy csv file to workdir
let csv: String = wrk.load_test_resource("adur-public-toilets-valid.csv");
wrk.create_from_string("data.csv", &csv);

// run validate command
let mut cmd = wrk.command("validate");
cmd.arg("data.csv")
.arg("schema.json")
.arg("--no-headers")
.args(["--valid-output", "-"]);

let got = wrk.output_stderr(&mut cmd);
let expected = "Cannot validate CSV without headers against a JSON Schema.\n".to_string();
assert_eq!(got, expected);

wrk.assert_err(&mut cmd);
}

#[test]
fn validate_adur_public_toilets_dataset_with_json_schema_url() {
let wrk = Workdir::new("validate").flexible(true);
Expand Down

0 comments on commit 6164382

Please sign in to comment.