pub fn validate<'a>(
cfg: &'a Config<'a>,
instance: &'a Value,
) -> Result<(), ErrorIterator<'a>>
Expand description
Validates a given JSON instance against a given JSON schema, returning the errors, if any. draft may provide the schema draft to use. If not provided, it will be determined automatically from the schema.
§Arguments
cfg
: The configuration object to useinstance
: The JSON document to validate
§Returns
errors
: AResult
indicating whether there were any validation errors. IfOk(())
, theinstance
is valid againstschema
. IfErr(e)
,e
is an iterator over all of the validation errors.
§Example:
The following example validates some JSON data against a draft 6 JSON schema.
let schema: Value = serde_json::from_str(schema_json)?;
let data: Value = serde_json::from_str(your_json_data)?;
let cfg = jsonschema_valid::Config::from_schema(&schema, Some(schemas::Draft::Draft6))?;
let mut validation = jsonschema_valid::validate(&cfg, &data);
if let Err(errors) = validation {
for error in errors {
println!("Error: {}", error);
}
}