use serde_json::Value;
use crate::error::{IssueCode, VldError};
use crate::schema::VldSchema;
#[derive(Clone, Copy)]
pub struct ZAny;
impl ZAny {
pub fn new() -> Self {
Self
}
}
impl Default for ZAny {
fn default() -> Self {
Self::new()
}
}
impl VldSchema for ZAny {
type Output = Value;
fn parse_value(&self, value: &Value) -> Result<Value, VldError> {
Ok(value.clone())
}
}
#[cfg(feature = "openapi")]
impl crate::openapi::JsonSchema for ZAny {
fn json_schema(&self) -> serde_json::Value {
serde_json::json!({})
}
fn is_required(&self) -> bool {
false
}
}
#[cfg(feature = "openapi")]
impl crate::openapi::CollectNestedSchemas for ZAny {}
#[derive(Clone, Default)]
pub struct ZNull {
custom_type_error: Option<String>,
}
impl ZNull {
pub fn new() -> Self {
Self::default()
}
pub fn type_error(mut self, msg: impl Into<String>) -> Self {
self.custom_type_error = Some(msg.into());
self
}
}
impl VldSchema for ZNull {
type Output = ();
fn parse_value(&self, value: &Value) -> Result<(), VldError> {
if value.is_null() {
Ok(())
} else {
Err(crate::primitives::type_error(
&self.custom_type_error,
"null",
value,
))
}
}
}
#[cfg(feature = "openapi")]
impl crate::openapi::JsonSchema for ZNull {
fn json_schema(&self) -> serde_json::Value {
serde_json::json!({"type": "null"})
}
fn is_required(&self) -> bool {
false
}
}
#[cfg(feature = "openapi")]
impl crate::openapi::CollectNestedSchemas for ZNull {}
#[derive(Clone, Copy, Default)]
pub struct ZNever;
impl ZNever {
pub fn new() -> Self {
Self
}
}
impl VldSchema for ZNever {
type Output = std::convert::Infallible;
fn parse_value(&self, value: &Value) -> Result<std::convert::Infallible, VldError> {
Err(VldError::from_code_with_value(
IssueCode::InvalidType {
expected: "never".to_string(),
received: crate::error::value_type_name(value),
},
value,
))
}
}
#[cfg(feature = "openapi")]
impl crate::openapi::JsonSchema for ZNever {
fn json_schema(&self) -> serde_json::Value {
serde_json::json!({"not": {}})
}
}
#[cfg(feature = "openapi")]
impl crate::openapi::CollectNestedSchemas for ZNever {}