pjson-rs-domain 0.5.2

Pure domain logic for PJS - WASM-compatible core
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//! Schema value object for JSON validation
//!
//! Represents a JSON schema definition following a subset of JSON Schema specification.
//! This is a domain value object with no identity, defined purely by its attributes.

use serde::{Deserialize, Serialize};
use smallvec::SmallVec;
use std::collections::HashMap;

use crate::DomainError;

/// Schema identifier for tracking and referencing schemas
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SchemaId(String);

impl SchemaId {
    /// Create a new schema identifier
    ///
    /// # Arguments
    /// * `id` - Unique schema identifier string
    ///
    /// # Returns
    /// New schema ID instance
    ///
    /// # Examples
    /// ```
    /// # use pjson_rs_domain::value_objects::SchemaId;
    /// let schema_id = SchemaId::new("user-profile-v1");
    /// ```
    pub fn new(id: impl Into<String>) -> Self {
        Self(id.into())
    }

    /// Get schema ID as string slice
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl std::fmt::Display for SchemaId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// JSON Schema representation for validation
///
/// Supports a practical subset of JSON Schema Draft 2020-12 focused on
/// validation needs for streaming JSON data.
///
/// # Design Philosophy
/// - Focused on validation, not full JSON Schema specification
/// - Performance-oriented with pre-compiled validation rules
/// - Zero-copy where possible using Arc for shared data
/// - Type-safe with strongly-typed enum variants
///
/// # Examples
/// ```
/// # use pjson_rs_domain::value_objects::{Schema, SchemaType};
/// let schema = Schema::Object {
///     properties: vec![
///         ("id".to_string(), Schema::Integer { minimum: Some(1), maximum: None }),
///         ("name".to_string(), Schema::String {
///             min_length: Some(1),
///             max_length: Some(100),
///             pattern: None,
///             allowed_values: None,
///         }),
///     ].into_iter().collect(),
///     required: vec!["id".to_string(), "name".to_string()],
///     additional_properties: false,
/// };
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Schema {
    /// String type with optional constraints
    String {
        /// Minimum string length (inclusive)
        min_length: Option<usize>,
        /// Maximum string length (inclusive)
        max_length: Option<usize>,
        /// Pattern to match (regex)
        pattern: Option<String>,
        /// Enumeration of allowed values
        allowed_values: Option<SmallVec<[String; 8]>>,
    },

    /// Integer type with optional range constraints
    Integer {
        /// Minimum value (inclusive)
        minimum: Option<i64>,
        /// Maximum value (inclusive)
        maximum: Option<i64>,
    },

    /// Number type (float/double) with optional range constraints
    Number {
        /// Minimum value (inclusive)
        minimum: Option<f64>,
        /// Maximum value (inclusive)
        maximum: Option<f64>,
    },

    /// Boolean type (no constraints)
    Boolean,

    /// Null type (no constraints)
    Null,

    /// Array type with element schema and size constraints
    Array {
        /// Schema for array elements (None = any type)
        items: Option<Box<Schema>>,
        /// Minimum array length (inclusive)
        min_items: Option<usize>,
        /// Maximum array length (inclusive)
        max_items: Option<usize>,
        /// Whether all items must be unique
        unique_items: bool,
    },

    /// Object type with property schemas
    Object {
        /// Property name to schema mapping
        properties: HashMap<String, Schema>,
        /// List of required property names
        required: Vec<String>,
        /// Whether additional properties are allowed
        additional_properties: bool,
    },

    /// Union type (one of multiple schemas)
    OneOf {
        /// List of possible schemas
        schemas: SmallVec<[Box<Schema>; 4]>,
    },

    /// Intersection type (all of multiple schemas)
    AllOf {
        /// List of schemas that must all match
        schemas: SmallVec<[Box<Schema>; 4]>,
    },

    /// Any type (no validation)
    Any,
}

/// Schema validation result
pub type SchemaValidationResult<T> = Result<T, SchemaValidationError>;

/// Schema validation error with detailed context
///
/// Provides rich error information including the JSON path where validation failed,
/// expected vs actual values, and human-readable error messages.
///
/// # Design
/// - Includes full path context for nested validation failures
/// - Provides actionable error messages for debugging
/// - Zero-allocation for common error cases using `String`
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, thiserror::Error)]
pub enum SchemaValidationError {
    /// Type mismatch error
    #[error("Type mismatch at '{path}': expected {expected}, got {actual}")]
    TypeMismatch {
        /// JSON path where error occurred
        path: String,
        /// Expected type
        expected: String,
        /// Actual type
        actual: String,
    },

    /// Missing required field
    #[error("Missing required field at '{path}': {field}")]
    MissingRequired {
        /// JSON path to parent object
        path: String,
        /// Name of missing field
        field: String,
    },

    /// Value out of range
    #[error("Value out of range at '{path}': {value} not in [{min}, {max}]")]
    OutOfRange {
        /// JSON path where error occurred
        path: String,
        /// Actual value
        value: String,
        /// Minimum allowed value
        min: String,
        /// Maximum allowed value
        max: String,
    },

    /// String length constraint violation
    #[error("String length constraint at '{path}': length {actual} not in [{min}, {max}]")]
    StringLengthConstraint {
        /// JSON path where error occurred
        path: String,
        /// Actual string length
        actual: usize,
        /// Minimum allowed length
        min: usize,
        /// Maximum allowed length
        max: usize,
    },

    /// Pattern mismatch
    #[error("Pattern mismatch at '{path}': value '{value}' does not match pattern '{pattern}'")]
    PatternMismatch {
        /// JSON path where error occurred
        path: String,
        /// Actual value
        value: String,
        /// Expected pattern
        pattern: String,
    },

    /// Invalid regex pattern in schema
    #[error("Invalid pattern at '{path}': pattern '{pattern}' is not valid regex: {reason}")]
    InvalidPattern {
        /// JSON path where error occurred
        path: String,
        /// The invalid pattern string
        pattern: String,
        /// Regex compilation error message
        reason: String,
    },

    /// Array size constraint violation
    #[error("Array size constraint at '{path}': size {actual} not in [{min}, {max}]")]
    ArraySizeConstraint {
        /// JSON path where error occurred
        path: String,
        /// Actual array size
        actual: usize,
        /// Minimum allowed size
        min: usize,
        /// Maximum allowed size
        max: usize,
    },

    /// Unique items constraint violation
    #[error("Unique items constraint at '{path}': duplicate items found")]
    DuplicateItems {
        /// JSON path where error occurred
        path: String,
    },

    /// Invalid enum value
    #[error("Invalid enum value at '{path}': '{value}' not in allowed values")]
    InvalidEnumValue {
        /// JSON path where error occurred
        path: String,
        /// Actual value
        value: String,
    },

    /// Additional properties not allowed
    #[error("Additional property not allowed at '{path}': '{property}'")]
    AdditionalPropertyNotAllowed {
        /// JSON path where error occurred
        path: String,
        /// Property name
        property: String,
    },

    /// No matching schema in OneOf
    #[error("No matching schema in OneOf at '{path}'")]
    NoMatchingOneOf {
        /// JSON path where error occurred
        path: String,
    },

    /// Not all schemas match in AllOf
    #[error("Not all schemas match in AllOf at '{path}': {failures}")]
    AllOfFailure {
        /// JSON path where error occurred
        path: String,
        /// List of failing schema indices
        failures: String,
    },
}

impl Schema {
    /// Check if schema allows a specific type
    ///
    /// Used for quick type compatibility checks before full validation.
    ///
    /// # Arguments
    /// * `schema_type` - The type to check compatibility for
    ///
    /// # Returns
    /// `true` if the schema allows the type, `false` otherwise
    pub fn allows_type(&self, schema_type: SchemaType) -> bool {
        match (self, schema_type) {
            (Self::String { .. }, SchemaType::String) => true,
            (Self::Integer { .. }, SchemaType::Integer) => true,
            (Self::Number { .. }, SchemaType::Number) => true,
            (Self::Boolean, SchemaType::Boolean) => true,
            (Self::Null, SchemaType::Null) => true,
            (Self::Array { .. }, SchemaType::Array) => true,
            (Self::Object { .. }, SchemaType::Object) => true,
            (Self::Any, _) => true,
            (Self::OneOf { schemas }, schema_type) => {
                schemas.iter().any(|s| s.allows_type(schema_type))
            }
            (Self::AllOf { schemas }, schema_type) => {
                schemas.iter().all(|s| s.allows_type(schema_type))
            }
            _ => false,
        }
    }

    /// Get estimated validation cost for performance optimization
    ///
    /// Higher cost indicates more expensive validation operations.
    /// Used by validation scheduler to optimize validation order.
    ///
    /// # Returns
    /// Validation cost estimate (0-1000 range)
    pub fn validation_cost(&self) -> usize {
        match self {
            Self::Null | Self::Boolean | Self::Any => 1,
            Self::Integer { .. } | Self::Number { .. } => 5,
            Self::String {
                pattern: Some(_), ..
            } => 50, // Regex is expensive
            Self::String { .. } => 10,
            Self::Array { items, .. } => {
                let item_cost = items.as_ref().map_or(1, |s| s.validation_cost());
                10 + item_cost
            }
            Self::Object { properties, .. } => {
                let prop_cost: usize = properties.values().map(|s| s.validation_cost()).sum();
                20 + prop_cost
            }
            Self::OneOf { schemas } => {
                let max_cost = schemas
                    .iter()
                    .map(|s| s.validation_cost())
                    .max()
                    .unwrap_or(0);
                30 + max_cost * schemas.len()
            }
            Self::AllOf { schemas } => {
                let total_cost: usize = schemas.iter().map(|s| s.validation_cost()).sum();
                20 + total_cost
            }
        }
    }

    /// Create a simple string schema with length constraints
    pub fn string(min_length: Option<usize>, max_length: Option<usize>) -> Self {
        Self::String {
            min_length,
            max_length,
            pattern: None,
            allowed_values: None,
        }
    }

    /// Create a simple integer schema with range constraints
    pub fn integer(minimum: Option<i64>, maximum: Option<i64>) -> Self {
        Self::Integer { minimum, maximum }
    }

    /// Create a simple number schema with range constraints
    pub fn number(minimum: Option<f64>, maximum: Option<f64>) -> Self {
        Self::Number { minimum, maximum }
    }

    /// Create an array schema with item type
    pub fn array(items: Option<Schema>) -> Self {
        Self::Array {
            items: items.map(Box::new),
            min_items: None,
            max_items: None,
            unique_items: false,
        }
    }

    /// Create an object schema with properties
    pub fn object(properties: HashMap<String, Schema>, required: Vec<String>) -> Self {
        Self::Object {
            properties,
            required,
            additional_properties: true,
        }
    }
}

/// Simplified schema type for quick type checking
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SchemaType {
    /// String type
    String,
    /// Integer type
    Integer,
    /// Floating-point number type
    Number,
    /// Boolean type
    Boolean,
    /// Null type
    Null,
    /// Array type
    Array,
    /// Object type
    Object,
}

impl From<&Schema> for SchemaType {
    fn from(schema: &Schema) -> Self {
        match schema {
            Schema::String { .. } => Self::String,
            Schema::Integer { .. } => Self::Integer,
            Schema::Number { .. } => Self::Number,
            Schema::Boolean => Self::Boolean,
            Schema::Null => Self::Null,
            Schema::Array { .. } => Self::Array,
            Schema::Object { .. } => Self::Object,
            Schema::Any => Self::Object, // Default to most flexible
            Schema::OneOf { .. } | Schema::AllOf { .. } => Self::Object,
        }
    }
}

impl From<DomainError> for SchemaValidationError {
    fn from(error: DomainError) -> Self {
        match error {
            DomainError::ValidationError(msg) => Self::TypeMismatch {
                path: "/".to_string(),
                expected: "valid".to_string(),
                actual: msg,
            },
            _ => Self::TypeMismatch {
                path: "/".to_string(),
                expected: "valid".to_string(),
                actual: error.to_string(),
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_schema_id_creation() {
        let id = SchemaId::new("test-schema-v1");
        assert_eq!(id.as_str(), "test-schema-v1");
        assert_eq!(id.to_string(), "test-schema-v1");
    }

    #[test]
    fn test_schema_allows_type() {
        let string_schema = Schema::string(Some(1), Some(100));
        assert!(string_schema.allows_type(SchemaType::String));
        assert!(!string_schema.allows_type(SchemaType::Integer));

        let any_schema = Schema::Any;
        assert!(any_schema.allows_type(SchemaType::String));
        assert!(any_schema.allows_type(SchemaType::Integer));
    }

    #[test]
    fn test_validation_cost() {
        let simple = Schema::Boolean;
        assert_eq!(simple.validation_cost(), 1);

        let complex = Schema::Object {
            properties: [
                ("id".to_string(), Schema::integer(None, None)),
                ("name".to_string(), Schema::string(Some(1), Some(100))),
            ]
            .into_iter()
            .collect(),
            required: vec!["id".to_string()],
            additional_properties: false,
        };
        assert!(complex.validation_cost() > 20);
    }

    #[test]
    fn test_schema_builders() {
        let str_schema = Schema::string(Some(1), Some(100));
        assert!(matches!(str_schema, Schema::String { .. }));

        let int_schema = Schema::integer(Some(0), Some(100));
        assert!(matches!(int_schema, Schema::Integer { .. }));

        let arr_schema = Schema::array(Some(Schema::integer(None, None)));
        assert!(matches!(arr_schema, Schema::Array { .. }));
    }
}