pub struct Evaluation { /* private fields */ }Expand description
Result of evaluating a JSON instance against a schema.
This type provides access to structured output formats as defined in the JSON Schema specification.
§Output Formats
The evaluation result can be accessed in three standard formats:
- Flag: Simple boolean validity indicator via
flag() - List: Flat list of all evaluation units via
list() - Hierarchical: Nested tree structure via
hierarchical()
All formats are serializable to JSON using serde_json.
§Examples
use serde_json::json;
let schema = json!({"type": "string", "minLength": 3});
let validator = jsonschema::validator_for(&schema)?;
// Evaluate an instance
let instance = json!("ab");
let evaluation = validator.evaluate(&instance);
// Check validity with flag format
let flag = evaluation.flag();
assert!(!flag.valid);
// Get structured output as JSON
let list_output = serde_json::to_value(evaluation.list())?;
println!("{}", serde_json::to_string_pretty(&list_output)?);
// Iterate over errors
for error in evaluation.iter_errors() {
println!("Error at {}: {}", error.instance_location, error.error);
}Implementations§
Source§impl Evaluation
impl Evaluation
Sourcepub fn flag(&self) -> FlagOutput
pub fn flag(&self) -> FlagOutput
Returns the flag output format.
This is the simplest output format, containing only a boolean indicating whether the instance is valid according to the schema.
§Examples
use serde_json::json;
let schema = json!({"type": "number"});
let validator = jsonschema::validator_for(&schema)?;
let evaluation = validator.evaluate(&json!(42));
let flag = evaluation.flag();
assert!(flag.valid);
let evaluation = validator.evaluate(&json!("not a number"));
let flag = evaluation.flag();
assert!(!flag.valid);Sourcepub fn is_valid(&self) -> bool
pub fn is_valid(&self) -> bool
Whether the instance is valid against the schema.
§Examples
use serde_json::json;
let validator = jsonschema::validator_for(&json!({"type": "number"}))?;
assert!(validator.evaluate(&json!(42)).is_valid());
assert!(!validator.evaluate(&json!("oops")).is_valid());Sourcepub fn list(&self) -> ListOutput<'_>
pub fn list(&self) -> ListOutput<'_>
Returns the list output format.
This format provides a flat list of all evaluation units, where each unit contains information about a specific validation step including its location, validity, annotations, and errors.
§Examples
use serde_json::json;
let schema = json!({
"type": "array",
"prefixItems": [{"type": "string"}],
"items": {"type": "integer"}
});
let validator = jsonschema::validator_for(&schema)?;
let evaluation = validator.evaluate(&json!(["hello", "oops"]));
assert_eq!(
serde_json::to_value(evaluation.list())?,
json!({
"valid": false,
"details": [
{"evaluationPath": "", "instanceLocation": "", "schemaLocation": "", "valid": false},
{
"valid": true,
"evaluationPath": "/type",
"instanceLocation": "",
"schemaLocation": "/type"
},
{
"valid": false,
"evaluationPath": "/items",
"instanceLocation": "",
"schemaLocation": "/items",
"droppedAnnotations": true
},
{
"valid": false,
"evaluationPath": "/items",
"instanceLocation": "/1",
"schemaLocation": "/items"
},
{
"valid": false,
"evaluationPath": "/items/type",
"instanceLocation": "/1",
"schemaLocation": "/items/type",
"errors": {"type": "\"oops\" is not of type \"integer\""}
},
{
"valid": true,
"evaluationPath": "/prefixItems",
"instanceLocation": "",
"schemaLocation": "/prefixItems",
"annotations": 0
},
{
"valid": true,
"evaluationPath": "/prefixItems/0",
"instanceLocation": "/0",
"schemaLocation": "/prefixItems/0"
},
{
"valid": true,
"evaluationPath": "/prefixItems/0/type",
"instanceLocation": "/0",
"schemaLocation": "/prefixItems/0/type"
}
]
})
);Sourcepub fn hierarchical(&self) -> HierarchicalOutput<'_>
pub fn hierarchical(&self) -> HierarchicalOutput<'_>
Returns the hierarchical output format.
This format represents the evaluation as a tree structure that mirrors the schema’s logical structure. Each node contains its validation result along with nested child nodes representing sub-schema evaluations.
§Examples
use serde_json::json;
let schema = json!({
"type": "array",
"prefixItems": [{"type": "string"}],
"items": {"type": "integer"}
});
let validator = jsonschema::validator_for(&schema)?;
let evaluation = validator.evaluate(&json!(["hello", "oops"]));
assert_eq!(
serde_json::to_value(evaluation.hierarchical())?,
json!({
"valid": false,
"evaluationPath": "",
"schemaLocation": "",
"instanceLocation": "",
"details": [
{
"valid": true,
"evaluationPath": "/type",
"instanceLocation": "",
"schemaLocation": "/type"
},
{
"valid": false,
"evaluationPath": "/items",
"instanceLocation": "",
"schemaLocation": "/items",
"droppedAnnotations": true,
"details": [
{
"valid": false,
"evaluationPath": "/items",
"instanceLocation": "/1",
"schemaLocation": "/items",
"details": [
{
"valid": false,
"evaluationPath": "/items/type",
"instanceLocation": "/1",
"schemaLocation": "/items/type",
"errors": {"type": "\"oops\" is not of type \"integer\""}
}
]
}
]
},
{
"valid": true,
"evaluationPath": "/prefixItems",
"instanceLocation": "",
"schemaLocation": "/prefixItems",
"annotations": 0,
"details": [
{
"valid": true,
"evaluationPath": "/prefixItems/0",
"instanceLocation": "/0",
"schemaLocation": "/prefixItems/0",
"details": [
{
"valid": true,
"evaluationPath": "/prefixItems/0/type",
"instanceLocation": "/0",
"schemaLocation": "/prefixItems/0/type"
}
]
}
]
}
]
})
);Sourcepub fn iter_annotations(&self) -> AnnotationIter<'_>
pub fn iter_annotations(&self) -> AnnotationIter<'_>
Returns an iterator over all annotations produced during evaluation.
Annotations are metadata emitted by keywords during successful validation. They can be used to collect information about which parts of a schema matched the instance.
§Examples
use serde_json::json;
let schema = json!({
"type": "object",
"properties": {"name": {"type": "string"}, "age": {"type": "number", "minimum": 0}},
"required": ["name"]
});
let validator = jsonschema::validator_for(&schema)?;
let evaluation = validator.evaluate(&json!({"name": "Alice", "age": 30}));
let entries: Vec<_> = evaluation.iter_annotations().collect();
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].schema_location, "/properties");
assert_eq!(entries[0].instance_location.as_str(), "");
let mut annotation_names: Vec<_> = entries[0]
.annotations
.value()
.as_array()
.expect("annotation should be an array")
.iter()
.map(|value| value.as_str().expect("annotation items should be strings"))
.collect();
annotation_names.sort_unstable();
assert_eq!(annotation_names, vec!["age", "name"]);Sourcepub fn iter_errors(&self) -> ErrorIter<'_>
pub fn iter_errors(&self) -> ErrorIter<'_>
Returns an iterator over all errors produced during evaluation.
Each error entry contains information about a validation failure, including its location in both the schema and instance.
§Examples
use serde_json::json;
let schema = json!({
"type": "object",
"required": ["name"],
"properties": {
"age": {"type": "number"}
}
});
let validator = jsonschema::validator_for(&schema)?;
let evaluation = validator.evaluate(&json!({"name": "Bob", "age": "oops"}));
let errors: Vec<_> = evaluation.iter_errors().collect();
assert_eq!(errors.len(), 1);
assert_eq!(errors[0].schema_location, "/properties/age/type");
assert_eq!(errors[0].instance_location.as_str(), "/age");
assert_eq!(errors[0].error.to_string(), "\"oops\" is not of type \"number\"");