Skip to main content

Keyword

Trait Keyword 

Source
pub trait Keyword<'i, F: Json = SerdeJson>: Send + Sync {
    // Required methods
    fn validate(&self, instance: F::Node<'i>) -> Result<(), ValidationError<'i>>;
    fn is_valid(&self, instance: F::Node<'i>) -> bool;

    // Provided method
    fn iter_errors(
        &self,
        instance: F::Node<'i>,
    ) -> Box<dyn Iterator<Item = ValidationError<'i>> + 'i> { ... }
}
Expand description

Trait for implementing custom keyword validators.

Custom keywords extend JSON Schema validation with domain-specific rules. F selects the instance representation the keyword operates on; the default is serde_json. The 'i lifetime is the validated instance’s; implementations are generic over it (impl<'i> Keyword<'i> for ...).

§Example

use jsonschema::{Keyword, ValidationError};
use serde_json::Value;

struct EvenNumberValidator;

impl<'i> Keyword<'i> for EvenNumberValidator {
    fn validate(&self, instance: &'i Value) -> Result<(), ValidationError<'i>> {
        if self.is_valid(instance) {
            Ok(())
        } else {
            Err(ValidationError::custom("number must be even"))
        }
    }

    fn is_valid(&self, instance: &'i Value) -> bool {
        instance.as_u64().is_none_or(|number| number % 2 == 0)
    }
}

A keyword written against the Node accessors works for every representation:

use jsonschema::{
    json::{Json, JsonNumber, Node},
    Keyword, ValidationError,
};

struct EvenNumberValidator;

impl<'i, F: Json> Keyword<'i, F> for EvenNumberValidator {
    fn validate(&self, instance: F::Node<'i>) -> Result<(), ValidationError<'i>> {
        if Keyword::<F>::is_valid(self, instance) {
            Ok(())
        } else {
            Err(ValidationError::custom("number must be even"))
        }
    }

    fn is_valid(&self, instance: F::Node<'i>) -> bool {
        instance
            .as_number()
            .and_then(|number| number.as_u64())
            .is_none_or(|number| number % 2 == 0)
    }
}

Required Methods§

Source

fn validate(&self, instance: F::Node<'i>) -> Result<(), ValidationError<'i>>

Validate an instance against this custom keyword.

Use ValidationError::custom for error messages. Path information (instance_path and schema_path) is filled in automatically.

§Errors

Returns a ValidationError if the instance is invalid.

Source

fn is_valid(&self, instance: F::Node<'i>) -> bool

Check validity without collecting error details.

Validator::is_valid calls this instead of validate, so the two must agree on every instance. A keyword that carries state across calls must update it in both.

Provided Methods§

Source

fn iter_errors( &self, instance: F::Node<'i>, ) -> Box<dyn Iterator<Item = ValidationError<'i>> + 'i>

Validate an instance, yielding every error at once.

Override this to report multiple problems from a single keyword. The default yields at most one error, from validate.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§