backbone-core 3.0.1

Backbone Framework Core - Foundation for generic CRUD system
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
//! Generic entity validation — composable field rules with typed errors.
//!
//! Generated services get a type alias:
//!
//! ```rust,ignore
//! // Generated:
//! pub type StoredFileValidator = EntityValidator<StoredFile>;
//! ```
//!
//! Custom decorators then add field rules without touching generated code:
//!
//! ```rust,ignore
//! // Custom:
//! impl StoredFileValidator {
//!     pub fn with_business_rules() -> Self {
//!         EntityValidator::new()
//!             .rule(RequiredString::new("name", |e: &StoredFile| &e.name))
//!             .rule(MaxLength::new("name", |e: &StoredFile| &e.name, 255))
//!             .rule(NonNegative::new("size_bytes", |e: &StoredFile| e.size_bytes))
//!     }
//! }
//! ```

use std::marker::PhantomData;
use std::sync::Arc;

// ─── Validation error ────────────────────────────────────────────────────────

/// A single field-level validation failure.
#[derive(Debug, Clone, PartialEq)]
pub struct ValidationError {
    /// The field name (dot-separated for nested: `"address.street"`).
    pub field: String,
    /// Human-readable message.
    pub message: String,
    /// Optional machine-readable code for API clients.
    pub code: Option<String>,
}

impl ValidationError {
    pub fn new(field: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            field: field.into(),
            message: message.into(),
            code: None,
        }
    }

    pub fn with_code(mut self, code: impl Into<String>) -> Self {
        self.code = Some(code.into());
        self
    }
}

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

/// All validation errors collected from a single `validate()` call.
#[derive(Debug, Clone, Default)]
pub struct ValidationErrors(Vec<ValidationError>);

impl ValidationErrors {
    pub fn new() -> Self {
        Self(Vec::new())
    }

    pub fn push(&mut self, error: ValidationError) {
        self.0.push(error);
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub fn errors(&self) -> &[ValidationError] {
        &self.0
    }

    pub fn into_errors(self) -> Vec<ValidationError> {
        self.0
    }

    /// Returns `Ok(())` if no errors, `Err(self)` otherwise.
    pub fn into_result(self) -> Result<(), ValidationErrors> {
        if self.is_empty() {
            Ok(())
        } else {
            Err(self)
        }
    }
}

impl std::fmt::Display for ValidationErrors {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for error in &self.0 {
            writeln!(f, "{error}")?;
        }
        Ok(())
    }
}

// ─── Field rule trait ────────────────────────────────────────────────────────

/// A single validation rule applied to an entity.
///
/// Implement this to create custom rules beyond the built-ins.
pub trait FieldRule<E>: Send + Sync {
    fn validate(&self, entity: &E) -> Vec<ValidationError>;
}

// ─── EntityValidator ─────────────────────────────────────────────────────────

/// Composable validator for any entity type `E`.
///
/// Rules are evaluated in registration order and all failures are collected
/// before returning — no fail-fast behaviour.
pub struct EntityValidator<E> {
    rules: Vec<Arc<dyn FieldRule<E>>>,
    _phantom: PhantomData<E>,
}

impl<E: Send + Sync + 'static> EntityValidator<E> {
    pub fn new() -> Self {
        Self {
            rules: Vec::new(),
            _phantom: PhantomData,
        }
    }

    /// Add a rule to this validator (builder pattern).
    pub fn rule(mut self, rule: impl FieldRule<E> + 'static) -> Self {
        self.rules.push(Arc::new(rule));
        self
    }

    /// Run all rules and collect errors.
    pub fn validate(&self, entity: &E) -> ValidationErrors {
        let mut errors = ValidationErrors::new();
        for rule in &self.rules {
            for error in rule.validate(entity) {
                errors.push(error);
            }
        }
        errors
    }

    /// Convenience — returns `Ok(())` or `Err(ValidationErrors)`.
    pub fn validate_result(&self, entity: &E) -> Result<(), ValidationErrors> {
        self.validate(entity).into_result()
    }
}

impl<E: Send + Sync + 'static> Default for EntityValidator<E> {
    fn default() -> Self {
        Self::new()
    }
}

// ─── Built-in rules ──────────────────────────────────────────────────────────

/// Fails if a string field is empty or whitespace-only.
pub struct RequiredString<E, F> {
    field_name: &'static str,
    accessor: F,
    _phantom: PhantomData<E>,
}

impl<E, F: Fn(&E) -> &str + Send + Sync> RequiredString<E, F> {
    pub fn new(field_name: &'static str, accessor: F) -> Self {
        Self {
            field_name,
            accessor,
            _phantom: PhantomData,
        }
    }
}

impl<E: Send + Sync, F: Fn(&E) -> &str + Send + Sync> FieldRule<E> for RequiredString<E, F> {
    fn validate(&self, entity: &E) -> Vec<ValidationError> {
        let value = (self.accessor)(entity);
        if value.trim().is_empty() {
            vec![ValidationError::new(
                self.field_name,
                format!("{} is required", self.field_name),
            )
            .with_code("required")]
        } else {
            vec![]
        }
    }
}

/// Fails if a string field exceeds `max_len` Unicode scalar values.
pub struct MaxLength<E, F> {
    field_name: &'static str,
    accessor: F,
    max_len: usize,
    _phantom: PhantomData<E>,
}

impl<E, F: Fn(&E) -> &str + Send + Sync> MaxLength<E, F> {
    pub fn new(field_name: &'static str, accessor: F, max_len: usize) -> Self {
        Self {
            field_name,
            accessor,
            max_len,
            _phantom: PhantomData,
        }
    }
}

impl<E: Send + Sync, F: Fn(&E) -> &str + Send + Sync> FieldRule<E> for MaxLength<E, F> {
    fn validate(&self, entity: &E) -> Vec<ValidationError> {
        let value = (self.accessor)(entity);
        if value.chars().count() > self.max_len {
            vec![ValidationError::new(
                self.field_name,
                format!(
                    "{} must be at most {} characters",
                    self.field_name, self.max_len
                ),
            )
            .with_code("max_length")]
        } else {
            vec![]
        }
    }
}

/// Fails if a numeric field is negative.
pub struct NonNegative<E, F> {
    field_name: &'static str,
    accessor: F,
    _phantom: PhantomData<E>,
}

impl<E, F: Fn(&E) -> i64 + Send + Sync> NonNegative<E, F> {
    pub fn new(field_name: &'static str, accessor: F) -> Self {
        Self {
            field_name,
            accessor,
            _phantom: PhantomData,
        }
    }
}

impl<E: Send + Sync, F: Fn(&E) -> i64 + Send + Sync> FieldRule<E> for NonNegative<E, F> {
    fn validate(&self, entity: &E) -> Vec<ValidationError> {
        let value = (self.accessor)(entity);
        if value < 0 {
            vec![ValidationError::new(
                self.field_name,
                format!("{} must be 0 or greater", self.field_name),
            )
            .with_code("non_negative")]
        } else {
            vec![]
        }
    }
}

/// Fails if an `Option<String>` field is `Some("")` or `Some("   ")`.
pub struct OptionalNotBlank<E, F> {
    field_name: &'static str,
    accessor: F,
    _phantom: PhantomData<E>,
}

impl<E, F: Fn(&E) -> Option<&str> + Send + Sync> OptionalNotBlank<E, F> {
    pub fn new(field_name: &'static str, accessor: F) -> Self {
        Self {
            field_name,
            accessor,
            _phantom: PhantomData,
        }
    }
}

impl<E: Send + Sync, F: Fn(&E) -> Option<&str> + Send + Sync> FieldRule<E>
    for OptionalNotBlank<E, F>
{
    fn validate(&self, entity: &E) -> Vec<ValidationError> {
        if let Some(value) = (self.accessor)(entity) {
            if value.trim().is_empty() {
                return vec![ValidationError::new(
                    self.field_name,
                    format!("{} must not be blank when provided", self.field_name),
                )
                .with_code("not_blank")];
            }
        }
        vec![]
    }
}

/// Fails if a UUID string field is empty or not a valid v4 UUID.
///
/// Checks that the field is a non-empty string that can be parsed as a UUID.
pub struct RequiredUuid<E, F> {
    field_name: &'static str,
    accessor: F,
    _phantom: PhantomData<E>,
}

impl<E, F: Fn(&E) -> &str + Send + Sync> RequiredUuid<E, F> {
    pub fn new(field_name: &'static str, accessor: F) -> Self {
        Self {
            field_name,
            accessor,
            _phantom: PhantomData,
        }
    }
}

impl<E: Send + Sync, F: Fn(&E) -> &str + Send + Sync> FieldRule<E> for RequiredUuid<E, F> {
    fn validate(&self, entity: &E) -> Vec<ValidationError> {
        let value = (self.accessor)(entity);
        if value.trim().is_empty() {
            return vec![ValidationError::new(
                self.field_name,
                format!("{} is required", self.field_name),
            )
            .with_code("required")];
        }
        if uuid::Uuid::parse_str(value).is_err() {
            return vec![ValidationError::new(
                self.field_name,
                format!("{} must be a valid UUID", self.field_name),
            )
            .with_code("invalid_uuid")];
        }
        vec![]
    }
}

/// Fails if a string field doesn't match the given regex pattern.
pub struct Regex<E, F> {
    field_name: &'static str,
    accessor: F,
    pattern: &'static str,
    message: &'static str,
    _phantom: PhantomData<E>,
}

impl<E, F: Fn(&E) -> &str + Send + Sync> Regex<E, F> {
    pub fn new(
        field_name: &'static str,
        accessor: F,
        pattern: &'static str,
        message: &'static str,
    ) -> Self {
        Self {
            field_name,
            accessor,
            pattern,
            message,
            _phantom: PhantomData,
        }
    }
}

impl<E: Send + Sync, F: Fn(&E) -> &str + Send + Sync> FieldRule<E> for Regex<E, F> {
    fn validate(&self, entity: &E) -> Vec<ValidationError> {
        let value = (self.accessor)(entity);
        match regex::Regex::new(self.pattern) {
            Ok(re) if re.is_match(value) => vec![],
            Ok(_) => vec![ValidationError::new(self.field_name, self.message).with_code("pattern")],
            Err(_) => vec![ValidationError::new(
                self.field_name,
                format!("invalid regex pattern: {}", self.pattern),
            )
            .with_code("invalid_pattern")],
        }
    }
}

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

    #[derive(Debug)]
    struct User {
        name: String,
        age: i64,
        bio: Option<String>,
    }

    #[test]
    fn required_string_rejects_blank() {
        let rule = RequiredString::new("name", |u: &User| u.name.as_str());
        let user = User {
            name: "  ".into(),
            age: 30,
            bio: None,
        };
        let errors = rule.validate(&user);
        assert_eq!(errors.len(), 1);
        assert_eq!(errors[0].code.as_deref(), Some("required"));
    }

    #[test]
    fn max_length_allows_exact_length() {
        let rule = MaxLength::new("name", |u: &User| u.name.as_str(), 3);
        let user = User {
            name: "abc".into(),
            age: 30,
            bio: None,
        };
        assert!(rule.validate(&user).is_empty());
    }

    #[test]
    fn max_length_rejects_over_limit() {
        let rule = MaxLength::new("name", |u: &User| u.name.as_str(), 3);
        let user = User {
            name: "abcd".into(),
            age: 30,
            bio: None,
        };
        assert!(!rule.validate(&user).is_empty());
    }

    #[test]
    fn regex_rule_validates_pattern() {
        let rule = Regex::new("phone", |u: &User| u.name.as_str(), r"^\+\d{7,15}$", "must be E.164");

        let valid = User { name: "+628123456789".into(), age: 0, bio: None };
        assert!(rule.validate(&valid).is_empty(), "valid E.164 should pass");

        let invalid = User { name: "no-plus".into(), age: 0, bio: None };
        let errors = rule.validate(&invalid);
        assert_eq!(errors.len(), 1);
        assert_eq!(errors[0].code.as_deref(), Some("pattern"));
    }

    #[test]
    fn entity_validator_collects_all_errors() {
        let validator = EntityValidator::new()
            .rule(RequiredString::new("name", |u: &User| u.name.as_str()))
            .rule(NonNegative::new("age", |u: &User| u.age));

        let user = User {
            name: "".into(),
            age: -1,
            bio: None,
        };

        let errors = validator.validate(&user);
        assert_eq!(errors.errors().len(), 2);
    }
}