rustrails-record 0.1.2

ORM layer (ActiveRecord equivalent)
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
use rustrails_support::{database, runtime};
use sea_orm::{
    ColumnTrait, DatabaseConnection, EntityTrait, ExprTrait, FromQueryResult, Iterable,
    PaginatorTrait, QueryFilter,
    sea_query::{Expr, Func},
};
use serde_json::Value;

use crate::{
    Record,
    relation::{json_to_sea_value, resolve_column},
};

/// Record-level uniqueness validation metadata and database lookup behavior.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct UniquenessValidator {
    /// Additional attributes intended to narrow the uniqueness scope.
    ///
    /// Scope metadata is retained even though the current generic record API cannot yet project
    /// sibling attribute values for query construction.
    pub scope: Vec<String>,
    /// Whether string comparisons preserve case.
    pub case_sensitive: bool,
    /// Optional custom validation message for higher layers.
    pub message: Option<String>,
}

impl UniquenessValidator {
    /// Creates a new uniqueness validator.
    #[must_use]
    pub fn new() -> Self {
        Self {
            scope: Vec::new(),
            case_sensitive: true,
            message: None,
        }
    }

    /// Sets additional scope fields for the uniqueness rule.
    #[must_use]
    pub fn scope(mut self, fields: Vec<String>) -> Self {
        self.scope = fields;
        self
    }

    /// Marks the validator as case-insensitive for string comparisons.
    #[must_use]
    pub fn case_insensitive(mut self) -> Self {
        self.case_sensitive = false;
        self
    }

    /// Overrides the default error message recorded by higher layers.
    #[must_use]
    pub fn message(mut self, message: impl Into<String>) -> Self {
        self.message = Some(message.into());
        self
    }

    /// Checks whether the candidate value is unique for the target attribute.
    ///
    /// The current generic implementation validates the requested attribute and excludes the
    /// current record by primary key when present. `scope` metadata is stored for future use once
    /// generic sibling-attribute access is available across all record types.
    pub async fn validate_unique<R: Record>(
        &self,
        attribute: &str,
        value: &Value,
        record: &R,
        db: &DatabaseConnection,
    ) -> bool
    where
        <R::Entity as EntityTrait>::Column: ColumnTrait + Iterable,
        <R::Entity as EntityTrait>::Model: FromQueryResult + Send + Sync,
    {
        let _scope = &self.scope;

        let attribute_column = match resolve_column::<R>(attribute) {
            Ok(column) => column,
            Err(_) => return false,
        };

        let mut query = R::Entity::find();
        query = if !self.case_sensitive {
            match value {
                Value::String(text) => query
                    .filter(Func::lower(Expr::col(attribute_column)).eq(text.to_ascii_lowercase())),
                _ => match json_to_sea_value(value) {
                    Ok(candidate) => query.filter(Expr::col(attribute_column).eq(candidate)),
                    Err(_) => return false,
                },
            }
        } else {
            match json_to_sea_value(value) {
                Ok(candidate) => query.filter(Expr::col(attribute_column).eq(candidate)),
                Err(_) => return false,
            }
        };

        if let Some(id) = record.id() {
            let primary_key_column = match resolve_column::<R>(R::primary_key_name()) {
                Ok(column) => column,
                Err(_) => return false,
            };
            query = query.filter(Expr::col(primary_key_column).ne(id));
        }

        match query.paginate(db, 1).num_items().await {
            Ok(count) => count == 0,
            Err(_) => false,
        }
    }

    /// Synchronous wrapper for [`Self::validate_unique`].
    pub fn validate_unique_sync<R: Record>(
        &self,
        attribute: &str,
        value: &Value,
        record: &R,
    ) -> bool
    where
        <R::Entity as EntityTrait>::Column: ColumnTrait + Iterable,
        <R::Entity as EntityTrait>::Model: FromQueryResult + Send + Sync,
    {
        database::with_db(|db| {
            runtime::block_on(self.validate_unique(attribute, value, record, db))
        })
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use rustrails_model::{
        errors::{ErrorType, Errors},
        validations::{
            CustomValidator, FormatValidator, LengthValidator, NumericalityValidator,
            PresenceValidator, ValidationSet,
        },
    };
    use sea_orm::{ConnectionTrait, Schema};
    use serde_json::{Value, json};

    use super::UniquenessValidator;
    use crate::{
        RecordState,
        base::test_support::{TestUser, seed_users, setup_db, test_user},
    };
    use rustrails_support::{database, runtime};

    fn run_sync_validation_test(seed: bool, test: impl FnOnce() + Send + 'static) {
        std::thread::spawn(move || {
            let _rt = runtime::init_runtime();
            database::establish("sqlite::memory:")
                .expect("sqlite in-memory connection should succeed");
            runtime::block_on(async {
                let db = database::db();
                let schema = Schema::new(db.get_database_backend());
                db.execute(&schema.create_table_from_entity(test_user::Entity))
                    .await
                    .expect("test_users table should be created");
                if seed {
                    seed_users(&db).await;
                }
            });
            test();
        })
        .join()
        .unwrap();
    }

    fn run_seeded_sync_validation_test(test: impl FnOnce() + Send + 'static) {
        run_sync_validation_test(true, test);
    }

    fn run_validations(
        set: &ValidationSet,
        attrs: impl IntoIterator<Item = (&'static str, Value)>,
    ) -> Errors {
        let attrs = attrs
            .into_iter()
            .map(|(name, value)| (name.to_owned(), value))
            .collect::<HashMap<_, _>>();
        let mut errors = Errors::new();
        let _ = set.validate(&|name| attrs.get(name).cloned(), &mut errors);
        errors
    }

    fn run_validations_without_attrs(set: &ValidationSet) -> Errors {
        let mut errors = Errors::new();
        let _ = set.validate(&|_| None, &mut errors);
        errors
    }

    #[test]
    fn builder_methods_update_validator_configuration() {
        let validator = UniquenessValidator::new()
            .scope(vec!["account_id".to_owned()])
            .case_insensitive()
            .message("already used");

        assert_eq!(validator.scope, vec!["account_id"]);
        assert!(!validator.case_sensitive);
        assert_eq!(validator.message.as_deref(), Some("already used"));
    }

    #[test]
    fn presence_validation_rejects_missing_values() {
        let mut set = ValidationSet::new();
        set.add("name", PresenceValidator::new());

        let errors = run_validations_without_attrs(&set);

        assert_eq!(errors.on("name")[0].error_type, ErrorType::Blank);
        assert_eq!(errors.messages_for("name"), vec!["can't be blank"]);
    }

    #[test]
    fn presence_validation_rejects_blank_strings() {
        let mut set = ValidationSet::new();
        set.add("name", PresenceValidator::new());

        let errors = run_validations(&set, [("name", json!("   "))]);

        assert_eq!(errors.on("name")[0].error_type, ErrorType::Blank);
    }

    #[test]
    fn presence_validation_accepts_present_strings() {
        let mut set = ValidationSet::new();
        set.add("name", PresenceValidator::new());

        let errors = run_validations(&set, [("name", json!("Alice"))]);

        assert!(errors.is_empty());
    }

    #[test]
    fn length_validation_rejects_values_shorter_than_minimum() {
        let mut set = ValidationSet::new();
        set.add("name", LengthValidator::new().minimum(3));

        let errors = run_validations(&set, [("name", json!("Al"))]);

        assert_eq!(errors.on("name")[0].error_type, ErrorType::TooShort);
        assert_eq!(errors.on("name")[0].details.get("count"), Some(&json!(3)));
    }

    #[test]
    fn length_validation_accepts_values_at_minimum_boundary() {
        let mut set = ValidationSet::new();
        set.add("name", LengthValidator::new().minimum(3));

        let errors = run_validations(&set, [("name", json!("Ada"))]);

        assert!(errors.is_empty());
    }

    #[test]
    fn length_validation_rejects_values_longer_than_maximum() {
        let mut set = ValidationSet::new();
        set.add("name", LengthValidator::new().maximum(5));

        let errors = run_validations(&set, [("name", json!("Roberto"))]);

        assert_eq!(errors.on("name")[0].error_type, ErrorType::TooLong);
        assert_eq!(errors.on("name")[0].details.get("count"), Some(&json!(5)));
    }

    #[test]
    fn length_validation_accepts_values_at_maximum_boundary() {
        let mut set = ValidationSet::new();
        set.add("name", LengthValidator::new().maximum(5));

        let errors = run_validations(&set, [("name", json!("Alice"))]);

        assert!(errors.is_empty());
    }

    #[test]
    fn length_validation_rejects_values_with_wrong_exact_length() {
        let mut set = ValidationSet::new();
        set.add("code", LengthValidator::new().is(4));

        let errors = run_validations(&set, [("code", json!("abc"))]);

        assert_eq!(errors.on("code")[0].error_type, ErrorType::WrongLength);
        assert_eq!(
            errors.messages_for("code"),
            vec!["is the wrong length (should be 4 characters)"]
        );
    }

    #[test]
    fn length_validation_accepts_values_with_exact_length() {
        let mut set = ValidationSet::new();
        set.add("code", LengthValidator::new().is(4));

        let errors = run_validations(&set, [("code", json!("ABCD"))]);

        assert!(errors.is_empty());
    }

    #[test]
    fn length_validation_counts_unicode_scalars() {
        let mut set = ValidationSet::new();
        set.add("nickname", LengthValidator::new().is(5));

        let errors = run_validations(&set, [("nickname", json!("あいうえお"))]);

        assert!(errors.is_empty());
    }

    #[test]
    fn format_validation_accepts_matching_patterns() {
        let mut set = ValidationSet::new();
        set.add(
            "email",
            FormatValidator::with_pattern(r"^[^@\s]+@[^@\s]+\.[^@\s]+$"),
        );

        let errors = run_validations(&set, [("email", json!("alice@example.com"))]);

        assert!(errors.is_empty());
    }

    #[test]
    fn format_validation_rejects_non_matching_patterns() {
        let mut set = ValidationSet::new();
        set.add(
            "email",
            FormatValidator::with_pattern(r"^[^@\s]+@[^@\s]+\.[^@\s]+$"),
        );

        let errors = run_validations(&set, [("email", json!("alice-at-example"))]);

        assert_eq!(errors.on("email")[0].error_type, ErrorType::Invalid);
    }

    #[test]
    fn format_validation_uses_custom_messages() {
        let mut set = ValidationSet::new();
        set.add(
            "pin",
            FormatValidator::with_pattern(r"^\d+$").message("digits only"),
        );

        let errors = run_validations(&set, [("pin", json!("12ab"))]);

        assert_eq!(errors.messages_for("pin"), vec!["digits only"]);
    }

    #[test]
    fn format_validation_can_reject_forbidden_matches() {
        let mut set = ValidationSet::new();
        set.add("body", FormatValidator::new().without("spam"));

        let errors = run_validations(&set, [("body", json!("contains spam"))]);

        assert_eq!(errors.on("body")[0].error_type, ErrorType::Invalid);
    }

    #[test]
    fn numericality_validation_rejects_non_numeric_strings() {
        let mut set = ValidationSet::new();
        set.add("age", NumericalityValidator::new());

        let errors = run_validations(&set, [("age", json!("old enough"))]);

        assert_eq!(errors.on("age")[0].error_type, ErrorType::NotANumber);
    }

    #[test]
    fn numericality_validation_accepts_numeric_strings() {
        let mut set = ValidationSet::new();
        set.add("age", NumericalityValidator::new());

        let errors = run_validations(&set, [("age", json!("42"))]);

        assert!(errors.is_empty());
    }

    #[test]
    fn numericality_validation_rejects_non_integer_values_when_integer_required() {
        let mut set = ValidationSet::new();
        set.add("age", NumericalityValidator::new().only_integer());

        let errors = run_validations(&set, [("age", json!("12.5"))]);

        assert_eq!(errors.on("age")[0].error_type, ErrorType::NotAnInteger);
    }

    #[test]
    fn numericality_validation_rejects_values_below_greater_than_bound() {
        let mut set = ValidationSet::new();
        set.add("score", NumericalityValidator::new().greater_than(10.0));

        let errors = run_validations(&set, [("score", json!(10))]);

        assert_eq!(errors.on("score")[0].error_type, ErrorType::GreaterThan);
    }

    #[test]
    fn numericality_validation_accepts_values_that_satisfy_multiple_constraints() {
        let mut set = ValidationSet::new();
        set.add(
            "score",
            NumericalityValidator::new()
                .greater_than(10.0)
                .less_than_or_equal_to(20.0)
                .even(),
        );

        let errors = run_validations(&set, [("score", json!(18))]);

        assert!(errors.is_empty());
    }

    #[test]
    fn numericality_validation_allow_nil_skips_missing_values() {
        let mut set = ValidationSet::new();
        set.add("score", NumericalityValidator::new().allow_nil());

        let errors = run_validations_without_attrs(&set);

        assert!(errors.is_empty());
    }

    #[test]
    fn custom_validation_can_add_errors() {
        let mut set = ValidationSet::new();
        set.add(
            "slug",
            CustomValidator::new(|attribute, value, errors| {
                if value.and_then(Value::as_str) == Some("reserved") {
                    errors.add(
                        attribute,
                        ErrorType::Custom("reserved".to_owned()),
                        "is reserved",
                    );
                }
            }),
        );

        let errors = run_validations(&set, [("slug", json!("reserved"))]);

        assert_eq!(
            errors.on("slug")[0].error_type,
            ErrorType::Custom("reserved".to_owned())
        );
        assert_eq!(errors.messages_for("slug"), vec!["is reserved"]);
    }

    #[test]
    fn custom_validation_receives_candidate_values() {
        let mut set = ValidationSet::new();
        set.add(
            "slug",
            CustomValidator::new(|attribute, value, errors| {
                if value.and_then(Value::as_str) != Some("rustrails") {
                    errors.add(
                        attribute,
                        ErrorType::Custom("slug".to_owned()),
                        "must equal rustrails",
                    );
                }
            }),
        );

        let errors = run_validations(&set, [("slug", json!("rustrails"))]);

        assert!(errors.is_empty());
    }

    #[test]
    fn error_collection_returns_messages_by_attribute() {
        let mut set = ValidationSet::new();
        set.add("name", PresenceValidator::new());
        set.add(
            "email",
            FormatValidator::with_pattern(r"^[^@\s]+@[^@\s]+\.[^@\s]+$"),
        );

        let errors = run_validations(
            &set,
            [("name", json!("")), ("email", json!("not-an-email"))],
        );

        assert_eq!(errors.attributes(), vec!["name", "email"]);
        assert_eq!(errors.messages_for("name"), vec!["can't be blank"]);
        assert_eq!(errors.messages_for("email"), vec!["is invalid"]);
    }

    #[test]
    fn error_collection_builds_full_messages_in_insertion_order() {
        let mut set = ValidationSet::new();
        set.add("name", PresenceValidator::new());
        set.add(
            "email",
            FormatValidator::with_pattern(r"^[^@\s]+@[^@\s]+\.[^@\s]+$"),
        );

        let errors = run_validations(
            &set,
            [("name", json!("")), ("email", json!("not-an-email"))],
        );

        assert_eq!(
            errors.full_messages(),
            vec!["Name can't be blank", "Email is invalid"]
        );
    }

    #[test]
    fn multiple_validations_on_same_field_collect_multiple_errors() {
        let mut set = ValidationSet::new();
        set.add("name", PresenceValidator::new());
        set.add("name", LengthValidator::new().minimum(3));

        let errors = run_validations(&set, [("name", json!(""))]);

        assert_eq!(errors.on("name").len(), 2);
        assert_eq!(
            errors.messages_for("name"),
            vec!["can't be blank", "is too short (minimum is 3 characters)",]
        );
    }

    #[test]
    fn multiple_validations_on_same_field_preserve_error_type_order() {
        let mut set = ValidationSet::new();
        set.add("name", PresenceValidator::new());
        set.add("name", LengthValidator::new().minimum(3));

        let errors = run_validations(&set, [("name", json!(""))]);

        let error_types = errors
            .on("name")
            .into_iter()
            .map(|error| error.error_type.clone())
            .collect::<Vec<_>>();

        assert_eq!(error_types, vec![ErrorType::Blank, ErrorType::TooShort]);
    }

    #[tokio::test]
    async fn validate_unique_returns_false_for_duplicate_values() {
        let db = setup_db().await;
        seed_users(&db).await;

        let candidate = TestUser {
            name: "Alice Clone".to_owned(),
            email: "alice@example.com".to_owned(),
            state: RecordState::New,
            ..Default::default()
        };

        let is_unique = UniquenessValidator::new()
            .validate_unique("email", &json!("alice@example.com"), &candidate, &db)
            .await;

        assert!(!is_unique);
    }

    #[tokio::test]
    async fn validate_unique_excludes_the_current_record() {
        let db = setup_db().await;
        let mut users = seed_users(&db).await;
        let alice = users.remove(0);

        let is_unique = UniquenessValidator::new()
            .validate_unique("email", &json!("alice@example.com"), &alice, &db)
            .await;

        assert!(is_unique);
    }

    #[tokio::test]
    async fn validate_unique_supports_case_insensitive_string_checks() {
        let db = setup_db().await;
        seed_users(&db).await;

        let candidate = TestUser {
            name: "Alice Clone".to_owned(),
            email: "ALICE@EXAMPLE.COM".to_owned(),
            state: RecordState::New,
            ..Default::default()
        };

        let is_unique = UniquenessValidator::new()
            .case_insensitive()
            .validate_unique("email", &json!("ALICE@EXAMPLE.COM"), &candidate, &db)
            .await;

        assert!(!is_unique);
    }

    #[test]
    fn validate_unique_sync_returns_false_for_duplicate_values() {
        run_seeded_sync_validation_test(|| {
            let candidate = TestUser {
                name: "Alice Clone".to_owned(),
                email: "alice@example.com".to_owned(),
                state: RecordState::New,
                ..Default::default()
            };

            let is_unique = UniquenessValidator::new().validate_unique_sync(
                "email",
                &json!("alice@example.com"),
                &candidate,
            );

            assert!(!is_unique);
        });
    }
}