#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(missing_docs)]
pub mod collections;
pub mod combinators;
pub mod error;
mod macros;
pub mod modifiers;
pub mod object;
#[cfg(feature = "openapi")]
#[cfg_attr(docsrs, doc(cfg(feature = "openapi")))]
pub mod openapi;
pub mod prelude;
pub mod primitives;
pub mod schema;
#[doc(hidden)]
#[path = "private.rs"]
pub mod __private;
pub use error::{format, i18n};
#[cfg(feature = "openapi")]
#[cfg_attr(docsrs, doc(cfg(feature = "openapi")))]
pub use openapi as json_schema;
#[cfg(feature = "diff")]
#[cfg_attr(docsrs, doc(cfg(feature = "diff")))]
pub use openapi::diff;
pub use schema::input;
#[cfg(feature = "chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))]
pub use primitives::date::chrono;
#[cfg(feature = "jiff")]
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
pub use primitives::date::jiff;
#[cfg(feature = "time")]
#[cfg_attr(docsrs, doc(cfg(feature = "time")))]
pub use primitives::date::time;
#[doc(hidden)]
pub use serde_json;
#[doc(hidden)]
pub use serde;
#[cfg(feature = "regex")]
#[cfg_attr(docsrs, doc(cfg(feature = "regex")))]
pub use regex_lite;
#[cfg(feature = "derive")]
#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
pub use vld_derive::Validate;
pub fn string() -> primitives::ZString {
primitives::ZString::new()
}
pub fn number() -> primitives::ZNumber {
primitives::ZNumber::new()
}
macro_rules! int_constructors {
($($name:ident: $t:ty),+ $(,)?) => {$(
#[doc = concat!("Create a `", stringify!($t), "` integer schema (exact, range-checked; see [`primitives::ZInteger`]).")]
pub fn $name() -> primitives::ZInteger<$t> {
primitives::ZInteger::new()
}
)+};
}
int_constructors! {
i8: i8, i16: i16, i32: i32, i64: i64, isize: isize,
u8: u8, u16: u16, u32: u32, u64: u64, usize: usize,
}
pub fn f32() -> primitives::ZF32 {
primitives::ZF32::new()
}
pub fn boolean() -> primitives::ZBoolean {
primitives::ZBoolean::new()
}
pub fn bytes() -> primitives::ZBytes {
primitives::ZBytes::new()
}
#[cfg(feature = "decimal")]
#[cfg_attr(docsrs, doc(cfg(feature = "decimal")))]
pub fn decimal() -> primitives::ZDecimal {
primitives::ZDecimal::new()
}
#[cfg(feature = "net")]
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]
pub fn ip_network() -> primitives::ZIpNetwork {
primitives::ZIpNetwork::new()
}
pub fn socket_addr() -> primitives::ZSocketAddr {
primitives::ZSocketAddr::new()
}
pub fn json_value() -> primitives::ZJsonValue {
primitives::ZJsonValue::new()
}
#[cfg(feature = "file")]
#[cfg_attr(docsrs, doc(cfg(feature = "file")))]
pub fn file() -> primitives::ZFile {
primitives::ZFile::new()
}
pub fn duration() -> primitives::ZDuration {
primitives::ZDuration::new()
}
pub fn path() -> primitives::ZPath {
primitives::ZPath::new()
}
pub fn array<T: schema::VldSchema>(element: T) -> collections::ZArray<T> {
collections::ZArray::new(element)
}
pub fn object() -> object::ZObject {
object::ZObject::new()
}
pub fn nested<T, F>(f: F) -> schema::NestedSchema<T, F>
where
F: Fn(&serde_json::Value) -> Result<T, error::VldError>,
{
schema::NestedSchema::new(f)
}
pub fn nested_named<T, F>(
name: &'static str,
f: F,
json_schema_fn: Option<fn() -> serde_json::Value>,
collect_nested_fn: Option<schema::NestedSchemaCollectFn>,
) -> schema::NestedSchema<T, F>
where
F: Fn(&serde_json::Value) -> Result<T, error::VldError>,
{
schema::NestedSchema::new_named(f, name, json_schema_fn, collect_nested_fn)
}
pub fn literal<T: primitives::IntoLiteral>(val: T) -> primitives::ZLiteral<T> {
primitives::ZLiteral::new(val)
}
pub fn enumeration(variants: &[&str]) -> primitives::ZEnum {
primitives::ZEnum::new(variants)
}
pub fn from_serde<T: serde::de::DeserializeOwned>() -> primitives::ZFromSerde<T> {
primitives::ZFromSerde::new()
}
pub fn from_str<T>() -> primitives::ZFromStr<T>
where
T: std::str::FromStr,
T::Err: std::fmt::Display,
{
primitives::ZFromStr::new()
}
pub fn enum_of<T: Clone>(variants: &[(&str, T)]) -> primitives::ZEnumOf<T> {
primitives::ZEnumOf::new(variants)
}
pub fn any() -> primitives::ZAny {
primitives::ZAny::new()
}
pub fn null() -> primitives::ZNull {
primitives::ZNull::new()
}
pub fn never() -> primitives::ZNever {
primitives::ZNever::new()
}
pub fn char() -> primitives::ZChar {
primitives::ZChar::new()
}
#[cfg(any(
all(feature = "chrono", not(feature = "jiff"), not(feature = "time")),
all(feature = "jiff", not(feature = "chrono"), not(feature = "time")),
all(feature = "time", not(feature = "chrono"), not(feature = "jiff"))
))]
#[cfg_attr(
docsrs,
doc(cfg(any(
all(feature = "chrono", not(feature = "jiff"), not(feature = "time")),
all(feature = "jiff", not(feature = "chrono"), not(feature = "time")),
all(feature = "time", not(feature = "chrono"), not(feature = "jiff"))
)))
)]
pub fn date() -> primitives::ZDate {
primitives::ZDate::new()
}
#[cfg(any(
all(feature = "chrono", not(feature = "jiff"), not(feature = "time")),
all(feature = "jiff", not(feature = "chrono"), not(feature = "time")),
all(feature = "time", not(feature = "chrono"), not(feature = "jiff"))
))]
#[cfg_attr(
docsrs,
doc(cfg(any(
all(feature = "chrono", not(feature = "jiff"), not(feature = "time")),
all(feature = "jiff", not(feature = "chrono"), not(feature = "time")),
all(feature = "time", not(feature = "chrono"), not(feature = "jiff"))
)))
)]
pub fn datetime() -> primitives::ZDateTime {
primitives::ZDateTime::new()
}
#[cfg(any(
all(feature = "chrono", not(feature = "jiff"), not(feature = "time")),
all(feature = "jiff", not(feature = "chrono"), not(feature = "time")),
all(feature = "time", not(feature = "chrono"), not(feature = "jiff"))
))]
#[cfg_attr(
docsrs,
doc(cfg(any(
all(feature = "chrono", not(feature = "jiff"), not(feature = "time")),
all(feature = "jiff", not(feature = "chrono"), not(feature = "time")),
all(feature = "time", not(feature = "chrono"), not(feature = "jiff"))
)))
)]
pub fn time_of_day() -> primitives::ZTime {
primitives::ZTime::new()
}
pub fn record<V: schema::VldSchema>(value_schema: V) -> collections::ZRecord<V> {
collections::ZRecord::new(value_schema)
}
pub fn record_with_keys<K, V>(
key_schema: K,
value_schema: V,
) -> collections::ZRecord<
V,
collections::KeySchema<K>,
std::collections::HashMap<K::Output, V::Output>,
>
where
K: schema::VldSchema,
K::Output: Eq + std::hash::Hash,
V: schema::VldSchema,
{
collections::ZRecord::with_keys(collections::KeySchema(key_schema), value_schema)
}
pub fn map<K: schema::VldSchema, V: schema::VldSchema>(
key_schema: K,
value_schema: V,
) -> collections::ZMap<K, V> {
collections::ZMap::new(key_schema, value_schema)
}
pub fn set<T: schema::VldSchema>(element: T) -> collections::ZSet<T> {
collections::ZSet::new(element)
}
pub fn union<A: schema::VldSchema, B: schema::VldSchema>(a: A, b: B) -> combinators::ZUnion2<A, B> {
combinators::ZUnion2::new(a, b)
}
pub fn union3<A: schema::VldSchema, B: schema::VldSchema, C: schema::VldSchema>(
a: A,
b: B,
c: C,
) -> combinators::ZUnion3<A, B, C> {
combinators::ZUnion3::new(a, b, c)
}
pub fn intersection<A: schema::VldSchema, B: schema::VldSchema>(
a: A,
b: B,
) -> combinators::ZIntersection<A, B> {
combinators::ZIntersection::new(a, b)
}
pub fn discriminated_union(discriminator: impl Into<String>) -> combinators::ZDiscriminatedUnion {
combinators::ZDiscriminatedUnion::new(discriminator)
}
pub fn lazy<T: schema::VldSchema, F: Fn() -> T>(factory: F) -> combinators::ZLazy<T, F> {
combinators::ZLazy::new(factory)
}
pub fn custom<F, T>(check: F) -> combinators::ZCustom<F, T>
where
F: Fn(&serde_json::Value) -> Result<T, String>,
{
combinators::ZCustom::new(check)
}
pub fn preprocess<F, S>(preprocessor: F, schema: S) -> combinators::ZPreprocess<F, S>
where
F: Fn(&serde_json::Value) -> serde_json::Value,
S: schema::VldSchema,
{
combinators::ZPreprocess::new(preprocessor, schema)
}