jsonschema-rs/jsonschema/src/keywords/max_length.rs

58 lines
1.5 KiB
Rust

use crate::{
compilation::{context::CompilationContext, JSONSchema},
error::{error, no_error, CompilationError, ErrorIterator, ValidationError},
keywords::CompilationResult,
validator::Validate,
};
use serde_json::{Map, Value};
pub(crate) struct MaxLengthValidator {
limit: u64,
}
impl MaxLengthValidator {
#[inline]
pub(crate) fn compile(schema: &Value) -> CompilationResult {
if let Some(limit) = schema.as_u64() {
Ok(Box::new(MaxLengthValidator { limit }))
} else {
Err(CompilationError::SchemaError)
}
}
}
impl Validate for MaxLengthValidator {
fn is_valid(&self, _: &JSONSchema, instance: &Value) -> bool {
if let Value::String(item) = instance {
if (item.chars().count() as u64) > self.limit {
return false;
}
}
true
}
fn validate<'a>(&self, _schema: &'a JSONSchema, instance: &'a Value) -> ErrorIterator<'a> {
if let Value::String(item) = instance {
if (item.chars().count() as u64) > self.limit {
return error(ValidationError::max_length(instance, self.limit));
}
}
no_error()
}
}
impl ToString for MaxLengthValidator {
fn to_string(&self) -> String {
format!("maxLength: {}", self.limit)
}
}
#[inline]
pub(crate) fn compile(
_: &Map<String, Value>,
schema: &Value,
_: &CompilationContext,
) -> Option<CompilationResult> {
Some(MaxLengthValidator::compile(schema))
}