Skip to content

Commit c7ad5d2

Browse files
committed
refactor: validate to adapt to jsonschema 0.36 API
per https://github.com/Stranger6667/jsonschema/blob/master/MIGRATION.md
1 parent f45693d commit c7ad5d2

File tree

1 file changed

+34
-35
lines changed

1 file changed

+34
-35
lines changed

src/cmd/validate.rs

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ use indicatif::HumanCount;
277277
use indicatif::{ProgressBar, ProgressDrawTarget};
278278
use jsonschema::{
279279
Keyword, PatternOptions, ValidationError, Validator,
280-
output::BasicOutput,
281280
paths::{LazyLocation, Location},
282281
};
283282
use log::debug;
@@ -1381,29 +1380,32 @@ Try running `qsv validate schema {}` to check the JSON Schema file."#, json_sche
13811380
};
13821381

13831382
// validate JSON instance against JSON Schema
1384-
match schema_compiled.apply(&json_instance).basic() {
1385-
BasicOutput::Valid(_) => None,
1386-
BasicOutput::Invalid(errors) => {
1387-
// Only convert to string when we have validation errors
1388-
// safety: see safety comment above
1389-
let row_number_string = unsafe {
1390-
simdutf8::basic::from_utf8(&record[header_len]).unwrap_unchecked()
1391-
};
1383+
let evaluation = schema_compiled.evaluate(&json_instance);
1384+
if evaluation.flag().valid {
1385+
None
1386+
} else {
1387+
// Only convert to string when we have validation errors
1388+
// safety: see safety comment above
1389+
let row_number_string = unsafe {
1390+
simdutf8::basic::from_utf8(&record[header_len]).unwrap_unchecked()
1391+
};
13921392

1393-
// Preallocate the vector with the known size
1394-
let mut error_messages = Vec::with_capacity(errors.len());
1395-
1396-
// there can be multiple validation errors for a single record,
1397-
// squash multiple errors into one long String with linebreaks
1398-
for e in errors {
1399-
error_messages.push(format!(
1400-
"{row_number_string}\t{field}\t{error}",
1401-
field = e.instance_location().as_str().trim_start_matches('/'),
1402-
error = e.error_description()
1403-
));
1404-
}
1405-
Some(error_messages.join("\n"))
1406-
},
1393+
// Collect errors into a vector
1394+
let errors: Vec<_> = evaluation.iter_errors().collect();
1395+
1396+
// Preallocate the vector with the known size
1397+
let mut error_messages = Vec::with_capacity(errors.len());
1398+
1399+
// there can be multiple validation errors for a single record,
1400+
// squash multiple errors into one long String with linebreaks
1401+
for e in errors {
1402+
error_messages.push(format!(
1403+
"{row_number_string}\t{field}\t{error}",
1404+
field = e.instance_location.as_str().trim_start_matches('/'),
1405+
error = e.error
1406+
));
1407+
}
1408+
Some(error_messages.join("\n"))
14071409
}
14081410
})
14091411
.collect_into_vec(&mut batch_validation_results);
@@ -2082,19 +2084,16 @@ fn validate_json_instance(
20822084
instance: &Value,
20832085
schema_compiled: &Validator,
20842086
) -> Option<Vec<(String, String)>> {
2085-
match schema_compiled.apply(instance).basic() {
2086-
BasicOutput::Valid(_) => None,
2087-
BasicOutput::Invalid(errors) => Some(
2088-
errors
2089-
.iter()
2090-
.map(|e| {
2091-
(
2092-
e.instance_location().to_string(),
2093-
e.error_description().to_string(),
2094-
)
2095-
})
2087+
let evaluation = schema_compiled.evaluate(instance);
2088+
if evaluation.flag().valid {
2089+
None
2090+
} else {
2091+
Some(
2092+
evaluation
2093+
.iter_errors()
2094+
.map(|e| (e.instance_location.to_string(), e.error.to_string()))
20962095
.collect(),
2097-
),
2096+
)
20982097
}
20992098
}
21002099

0 commit comments

Comments
 (0)