70 lines
1.8 KiB
Rust
70 lines
1.8 KiB
Rust
use crate::{
|
|
compilation::{context::CompilationContext, JSONSchema},
|
|
error::{no_error, CompilationError, ErrorIterator, ValidationError},
|
|
keywords::CompilationResult,
|
|
validator::Validate,
|
|
};
|
|
use serde_json::{Map, Value};
|
|
|
|
pub(crate) struct MaxPropertiesValidator {
|
|
limit: u64,
|
|
}
|
|
|
|
impl MaxPropertiesValidator {
|
|
#[inline]
|
|
pub(crate) fn compile(schema: &Value) -> CompilationResult {
|
|
if let Some(limit) = schema.as_u64() {
|
|
return Ok(Box::new(MaxPropertiesValidator { limit }));
|
|
}
|
|
Err(CompilationError::SchemaError)
|
|
}
|
|
}
|
|
|
|
impl Validate for MaxPropertiesValidator {
|
|
#[inline]
|
|
fn build_validation_error<'a>(&self, instance: &'a Value) -> ValidationError<'a> {
|
|
ValidationError::max_properties(instance, self.limit)
|
|
}
|
|
|
|
#[inline]
|
|
fn is_valid_object(
|
|
&self,
|
|
_: &JSONSchema,
|
|
_: &Value,
|
|
instance_value: &Map<String, Value>,
|
|
) -> bool {
|
|
(instance_value.len() as u64) <= self.limit
|
|
}
|
|
#[inline]
|
|
fn is_valid(&self, _: &JSONSchema, instance: &Value) -> bool {
|
|
if let Value::Object(instance_value) = instance {
|
|
instance_value.len() as u64 <= self.limit
|
|
} else {
|
|
true
|
|
}
|
|
}
|
|
|
|
#[inline]
|
|
fn validate<'a>(&self, schema: &'a JSONSchema, instance: &'a Value) -> ErrorIterator<'a> {
|
|
if let Value::Object(instance_value) = instance {
|
|
self.validate_object(schema, instance, instance_value)
|
|
} else {
|
|
no_error()
|
|
}
|
|
}
|
|
}
|
|
impl ToString for MaxPropertiesValidator {
|
|
fn to_string(&self) -> String {
|
|
format!("maxProperties: {}", self.limit)
|
|
}
|
|
}
|
|
|
|
#[inline]
|
|
pub(crate) fn compile(
|
|
_: &Map<String, Value>,
|
|
schema: &Value,
|
|
_: &CompilationContext,
|
|
) -> Option<CompilationResult> {
|
|
Some(MaxPropertiesValidator::compile(schema))
|
|
}
|