use crate::{
paths::{LazyLocation, Location},
validator::Validate,
ValidationError,
};
use serde_json::{Map, Value};
pub(crate) struct CustomKeyword {
inner: Box<dyn Keyword>,
}
impl CustomKeyword {
pub(crate) fn new(inner: Box<dyn Keyword>) -> Self {
Self { inner }
}
}
impl Validate for CustomKeyword {
fn validate<'i>(
&self,
instance: &'i Value,
location: &LazyLocation,
) -> Result<(), ValidationError<'i>> {
self.inner.validate(instance, location)
}
fn is_valid(&self, instance: &Value) -> bool {
self.inner.is_valid(instance)
}
}
pub trait Keyword: Send + Sync {
fn validate<'i>(
&self,
instance: &'i Value,
location: &LazyLocation,
) -> Result<(), ValidationError<'i>>;
fn is_valid(&self, instance: &Value) -> bool;
}
pub(crate) trait KeywordFactory: Send + Sync {
fn init<'a>(
&self,
parent: &'a Map<String, Value>,
schema: &'a Value,
path: Location,
) -> Result<Box<dyn Keyword>, ValidationError<'a>>;
}
impl<F> KeywordFactory for F
where
F: for<'a> Fn(
&'a Map<String, Value>,
&'a Value,
Location,
) -> Result<Box<dyn Keyword>, ValidationError<'a>>
+ Send
+ Sync,
{
fn init<'a>(
&self,
parent: &'a Map<String, Value>,
schema: &'a Value,
path: Location,
) -> Result<Box<dyn Keyword>, ValidationError<'a>> {
self(parent, schema, path)
}
}