1mod array;
2mod boolean;
3mod literal;
4mod null;
5mod number;
6mod object;
7mod optional;
8mod string;
9mod tuple;
10mod union;
11
12pub use array::*;
13pub use boolean::*;
14pub use literal::*;
15pub use null::*;
16pub use number::*;
17pub use object::*;
18pub use optional::*;
19pub use string::*;
20pub use tuple::*;
21pub use union::*;
22
23use serde_json::Value;
24use std::fmt::Debug;
25use zod_rs_util::ValidateResult;
26
27pub trait Schema<T>: Debug
28where
29 T: std::fmt::Debug,
30{
31 fn validate(&self, value: &Value) -> ValidateResult<T>;
33
34 fn parse(&self, value: &Value) -> T {
48 match self.validate(value) {
49 Ok(result) => result,
50 Err(errors) => panic!("Validation failed: {errors}"),
51 }
52 }
53
54 fn safe_parse(&self, value: &Value) -> ValidateResult<T> {
57 self.validate(value)
58 }
59
60 fn optional(self) -> OptionalSchema<Self, T>
61 where
62 Self: Sized,
63 {
64 OptionalSchema::new(self)
65 }
66
67 fn array(self) -> ArraySchema<Self, T>
68 where
69 Self: Sized,
70 {
71 ArraySchema::new(self)
72 }
73}