From 616438213de44e4377a98ea81a676a7900bd4ae9 Mon Sep 17 00:00:00 2001 From: Joel Natividad <1980690+jqnatividad@users.noreply.github.com> Date: Sun, 30 Jun 2024 09:59:46 -0400 Subject: [PATCH] `validate`: validating against a JSONschema requires headers --- src/cmd/validate.rs | 11 ++++++++++- tests/test_validate.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/cmd/validate.rs b/src/cmd/validate.rs index a4bc5a66c..1240b8053 100644 --- a/src/cmd/validate.rs +++ b/src/cmd/validate.rs @@ -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 The field delimiter for reading CSV data. Must be a single character. [default: ,] -p, --progressbar Show progress bars. Not valid for stdin. @@ -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)); diff --git a/tests/test_validate.rs b/tests/test_validate.rs index afcf31020..a5b07ada3 100644 --- a/tests/test_validate.rs +++ b/tests/test_validate.rs @@ -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);