1use crate::schema::Schema;
2use serde_json::Value;
3use zod_rs_util::{ValidateResult, ValidationError, ValidationType};
4
5#[derive(Debug, Clone)]
6pub struct NullSchema;
7
8impl NullSchema {
9 pub fn new() -> Self {
10 Self
11 }
12}
13
14impl Default for NullSchema {
15 fn default() -> Self {
16 Self::new()
17 }
18}
19
20impl Schema<()> for NullSchema {
21 fn validate(&self, value: &Value) -> ValidateResult<()> {
22 if value.is_null() {
23 Ok(())
24 } else {
25 Err(ValidationError::invalid_type(
26 ValidationType::Null,
27 ValidationType::from(value),
28 )
29 .into())
30 }
31 }
32}
33
34pub fn null() -> NullSchema {
35 NullSchema::new()
36}
37
38#[cfg(test)]
39mod tests {
40 use super::*;
41 use serde_json::json;
42
43 #[test]
44 fn test_null_validation() {
45 let schema = null();
46
47 assert!(schema.validate(&json!(null)).is_ok());
48 assert!(schema.validate(&json!(true)).is_err());
49 assert!(schema.validate(&json!(false)).is_err());
50 assert!(schema.validate(&json!("null")).is_err());
51 assert!(schema.validate(&json!(0)).is_err());
52 }
53
54 #[test]
55 fn test_null_returns_unit() {
56 let schema = null();
57 let result = schema.validate(&json!(null));
58 assert!(result.is_ok());
59 assert_eq!(result.unwrap(), ());
60 }
61
62 #[test]
63 fn test_rejects_empty_string() {
64 let schema = null();
65 assert!(schema.validate(&json!("")).is_err());
66 }
67
68 #[test]
69 fn test_rejects_array() {
70 let schema = null();
71 assert!(schema.validate(&json!([])).is_err());
72 }
73
74 #[test]
75 fn test_rejects_object() {
76 let schema = null();
77 assert!(schema.validate(&json!({})).is_err());
78 }
79}