postmortem 0.1.1

A validation library that accumulates all errors for comprehensive feedback
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! Schema definitions for validation.
//!
//! This module provides schema types for validating data structures.
//! Each schema type (string, number, object, etc.) validates values and accumulates
//! all validation errors rather than short-circuiting on the first failure.
//!
//! # Example
//!
//! ```rust
//! use postmortem::{Schema, JsonPath};
//! use serde_json::json;
//!
//! let schema = Schema::string().min_len(1).max_len(100);
//!
//! let result = schema.validate(&json!("hello"), &JsonPath::root());
//! assert!(result.is_success());
//! ```

mod array;
mod combinators;
mod numeric;
mod object;
mod ref_schema;
mod string;
mod traits;

pub use array::ArraySchema;
pub use combinators::CombinatorSchema;
pub use numeric::IntegerSchema;
pub use object::ObjectSchema;
pub use ref_schema::RefSchema;
pub use string::StringSchema;
pub use traits::{SchemaLike, ValueValidator};

/// Entry point for creating validation schemas.
///
/// `Schema` provides factory methods for creating different schema types.
/// Each schema type validates specific value types and supports various
/// constraints through a builder pattern.
///
/// # Example
///
/// ```rust
/// use postmortem::Schema;
///
/// // Create a string schema with length constraints
/// let string_schema = Schema::string()
///     .min_len(1)
///     .max_len(100);
///
/// // Create a string schema with pattern validation
/// let email_schema = Schema::string()
///     .pattern(r"@")
///     .unwrap()
///     .error("must contain @");
/// ```
pub struct Schema;

impl Schema {
    /// Creates a new string schema.
    ///
    /// The returned schema validates that values are strings. Use builder
    /// methods to add constraints like minimum/maximum length or patterns.
    ///
    /// # Example
    ///
    /// ```rust
    /// use postmortem::{Schema, JsonPath};
    /// use serde_json::json;
    ///
    /// let schema = Schema::string().min_len(5);
    ///
    /// let result = schema.validate(&json!("hello"), &JsonPath::root());
    /// assert!(result.is_success());
    ///
    /// let result = schema.validate(&json!("hi"), &JsonPath::root());
    /// assert!(result.is_failure());
    /// ```
    pub fn string() -> StringSchema {
        StringSchema::new()
    }

    /// Creates a new integer schema.
    ///
    /// The returned schema validates that values are integers (not floats).
    /// Use builder methods to add constraints like minimum/maximum value,
    /// range, or sign requirements.
    ///
    /// # Example
    ///
    /// ```rust
    /// use postmortem::{Schema, JsonPath};
    /// use serde_json::json;
    ///
    /// let schema = Schema::integer().min(0).max(100);
    ///
    /// let result = schema.validate(&json!(50), &JsonPath::root());
    /// assert!(result.is_success());
    ///
    /// let result = schema.validate(&json!(-5), &JsonPath::root());
    /// assert!(result.is_failure());
    ///
    /// // Float values are rejected
    /// let result = schema.validate(&json!(1.5), &JsonPath::root());
    /// assert!(result.is_failure());
    /// ```
    pub fn integer() -> IntegerSchema {
        IntegerSchema::new()
    }

    /// Creates a new object schema.
    ///
    /// The returned schema validates that values are JSON objects. Use builder
    /// methods to define required fields, optional fields, default values, and
    /// control handling of additional properties.
    ///
    /// # Example
    ///
    /// ```rust
    /// use postmortem::{Schema, JsonPath};
    /// use serde_json::json;
    ///
    /// let schema = Schema::object()
    ///     .field("name", Schema::string().min_len(1))
    ///     .field("age", Schema::integer().positive())
    ///     .optional("email", Schema::string())
    ///     .default("role", Schema::string(), json!("user"))
    ///     .additional_properties(false);
    ///
    /// let result = schema.validate(&json!({
    ///     "name": "Alice",
    ///     "age": 30
    /// }), &JsonPath::root());
    /// assert!(result.is_success());
    ///
    /// // Missing required field produces error
    /// let result = schema.validate(&json!({"name": "Bob"}), &JsonPath::root());
    /// assert!(result.is_failure());
    /// ```
    pub fn object() -> ObjectSchema {
        ObjectSchema::new()
    }

    /// Creates a new array schema with the given item schema.
    ///
    /// The returned schema validates that values are arrays and that each item
    /// passes validation against the provided item schema. Use builder methods
    /// to add constraints like minimum/maximum length or uniqueness.
    ///
    /// # Example
    ///
    /// ```rust
    /// use postmortem::{Schema, JsonPath};
    /// use serde_json::json;
    ///
    /// // Array of positive integers
    /// let schema = Schema::array(Schema::integer().positive())
    ///     .min_len(1)
    ///     .max_len(10);
    ///
    /// let result = schema.validate(&json!([1, 2, 3]), &JsonPath::root());
    /// assert!(result.is_success());
    ///
    /// // Empty array fails min_len constraint
    /// let result = schema.validate(&json!([]), &JsonPath::root());
    /// assert!(result.is_failure());
    ///
    /// // Non-positive integer fails item validation
    /// let result = schema.validate(&json!([1, -2, 3]), &JsonPath::root());
    /// assert!(result.is_failure());
    /// ```
    pub fn array<S: SchemaLike>(item_schema: S) -> ArraySchema<S> {
        ArraySchema::new(item_schema)
    }

    /// Creates a one-of combinator schema.
    ///
    /// Exactly one of the provided schemas must match. This is ideal for
    /// discriminated unions where a value must be one of several distinct types.
    ///
    /// # Example
    ///
    /// ```rust
    /// use postmortem::{Schema, ValueValidator, SchemaLike, JsonPath};
    /// use serde_json::json;
    ///
    /// // Shape can be either a circle or rectangle
    /// let shape = Schema::one_of(vec![
    ///     Box::new(Schema::object()
    ///         .field("type", Schema::string())
    ///         .field("radius", Schema::integer().positive())) as Box<dyn ValueValidator>,
    ///     Box::new(Schema::object()
    ///         .field("type", Schema::string())
    ///         .field("width", Schema::integer().positive())
    ///         .field("height", Schema::integer().positive())) as Box<dyn ValueValidator>,
    /// ]);
    ///
    /// let result = shape.validate(&json!({
    ///     "type": "circle",
    ///     "radius": 5
    /// }), &JsonPath::root());
    /// assert!(result.is_success());
    /// ```
    pub fn one_of<I>(schemas: I) -> CombinatorSchema
    where
        I: IntoIterator<Item = Box<dyn ValueValidator>>,
    {
        use crate::schema::combinators::ValidatorFn;
        use std::sync::Arc;
        let validators: Vec<Arc<dyn ValueValidator>> = schemas
            .into_iter()
            .map(|schema| Arc::from(schema) as Arc<dyn ValueValidator>)
            .collect();
        let validator_fns: Vec<ValidatorFn> = validators
            .iter()
            .map(|validator| {
                let v = Arc::clone(validator);
                Arc::new(
                    move |value: &serde_json::Value, path: &crate::path::JsonPath| {
                        v.validate_value(value, path)
                    },
                ) as ValidatorFn
            })
            .collect();
        CombinatorSchema::OneOf {
            schemas: validator_fns,
            validators,
        }
    }

    /// Creates an any-of combinator schema.
    ///
    /// At least one of the provided schemas must match. This is more permissive
    /// than `one_of` and allows multiple matches. Validation short-circuits on
    /// the first match.
    ///
    /// # Example
    ///
    /// ```rust
    /// use postmortem::{Schema, ValueValidator, SchemaLike, JsonPath};
    /// use serde_json::json;
    ///
    /// // ID can be either a string or positive integer
    /// let id = Schema::any_of(vec![
    ///     Box::new(Schema::string().min_len(1)) as Box<dyn ValueValidator>,
    ///     Box::new(Schema::integer().positive()) as Box<dyn ValueValidator>,
    /// ]);
    ///
    /// let result = id.validate(&json!("abc-123"), &JsonPath::root());
    /// assert!(result.is_success());
    ///
    /// let result = id.validate(&json!(42), &JsonPath::root());
    /// assert!(result.is_success());
    /// ```
    pub fn any_of<I>(schemas: I) -> CombinatorSchema
    where
        I: IntoIterator<Item = Box<dyn ValueValidator>>,
    {
        use crate::schema::combinators::ValidatorFn;
        use std::sync::Arc;
        let validators: Vec<Arc<dyn ValueValidator>> = schemas
            .into_iter()
            .map(|schema| Arc::from(schema) as Arc<dyn ValueValidator>)
            .collect();
        let validator_fns: Vec<ValidatorFn> = validators
            .iter()
            .map(|validator| {
                let v = Arc::clone(validator);
                Arc::new(
                    move |value: &serde_json::Value, path: &crate::path::JsonPath| {
                        v.validate_value(value, path)
                    },
                ) as ValidatorFn
            })
            .collect();
        CombinatorSchema::AnyOf {
            schemas: validator_fns,
            validators,
        }
    }

    /// Creates an all-of combinator schema.
    ///
    /// All of the provided schemas must match. This is useful for schema
    /// composition and intersection, where a value must satisfy multiple
    /// independent constraints.
    ///
    /// # Example
    ///
    /// ```rust
    /// use postmortem::{Schema, ValueValidator, SchemaLike, JsonPath};
    /// use serde_json::json;
    ///
    /// // Entity must have both a name and a timestamp
    /// let named = Schema::object()
    ///     .field("name", Schema::string().min_len(1));
    ///
    /// let timestamped = Schema::object()
    ///     .field("created_at", Schema::string());
    ///
    /// let entity = Schema::all_of(vec![
    ///     Box::new(named) as Box<dyn ValueValidator>,
    ///     Box::new(timestamped) as Box<dyn ValueValidator>,
    /// ]);
    ///
    /// let result = entity.validate(&json!({
    ///     "name": "Alice",
    ///     "created_at": "2025-01-01"
    /// }), &JsonPath::root());
    /// assert!(result.is_success());
    /// ```
    pub fn all_of<I>(schemas: I) -> CombinatorSchema
    where
        I: IntoIterator<Item = Box<dyn ValueValidator>>,
    {
        use crate::schema::combinators::ValidatorFn;
        use std::sync::Arc;
        let validators: Vec<Arc<dyn ValueValidator>> = schemas
            .into_iter()
            .map(|schema| Arc::from(schema) as Arc<dyn ValueValidator>)
            .collect();
        let validator_fns: Vec<ValidatorFn> = validators
            .iter()
            .map(|validator| {
                let v = Arc::clone(validator);
                Arc::new(
                    move |value: &serde_json::Value, path: &crate::path::JsonPath| {
                        v.validate_value(value, path)
                    },
                ) as ValidatorFn
            })
            .collect();
        CombinatorSchema::AllOf {
            schemas: validator_fns,
            validators,
        }
    }

    /// Creates an optional combinator schema.
    ///
    /// The value can be null. Non-null values are validated against the inner schema.
    ///
    /// # Example
    ///
    /// ```rust
    /// use postmortem::{Schema, ValueValidator, SchemaLike, JsonPath};
    /// use serde_json::json;
    ///
    /// let optional_string = Schema::optional(
    ///     Box::new(Schema::string().min_len(1)) as Box<dyn ValueValidator>
    /// );
    ///
    /// // Null is valid
    /// let result = optional_string.validate(&json!(null), &JsonPath::root());
    /// assert!(result.is_success());
    ///
    /// // Non-null values are validated
    /// let result = optional_string.validate(&json!("hello"), &JsonPath::root());
    /// assert!(result.is_success());
    ///
    /// let result = optional_string.validate(&json!(""), &JsonPath::root());
    /// assert!(result.is_failure());
    /// ```
    pub fn optional(inner: Box<dyn ValueValidator>) -> CombinatorSchema {
        use crate::schema::combinators::ValidatorFn;
        use std::sync::Arc;
        let validator = Arc::from(inner) as Arc<dyn ValueValidator>;
        let validator_fn: ValidatorFn = {
            let v = Arc::clone(&validator);
            Arc::new(
                move |value: &serde_json::Value, path: &crate::path::JsonPath| {
                    v.validate_value(value, path)
                },
            )
        };
        CombinatorSchema::Optional {
            inner: validator_fn,
            validator,
        }
    }

    /// Creates a reference to a named schema.
    ///
    /// Schema references enable reuse and recursive structures. The referenced
    /// schema must be registered in a `SchemaRegistry` before validation.
    ///
    /// References can only be validated through a registry. Attempting to validate
    /// without a registry produces an error with code `missing_registry`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use postmortem::{Schema, SchemaRegistry};
    /// use serde_json::json;
    ///
    /// let registry = SchemaRegistry::new();
    ///
    /// // Register base schema
    /// registry.register("UserId", Schema::integer().positive()).unwrap();
    ///
    /// // Use reference in another schema
    /// registry.register("User", Schema::object()
    ///     .field("id", Schema::ref_("UserId"))
    ///     .field("name", Schema::string())
    /// ).unwrap();
    ///
    /// let result = registry.validate("User", &json!({
    ///     "id": 42,
    ///     "name": "Alice"
    /// })).unwrap();
    ///
    /// assert!(result.is_success());
    /// ```
    pub fn ref_(name: impl Into<String>) -> RefSchema {
        RefSchema::new(name)
    }
}