fraiseql-db 2.2.0

Database abstraction layer for FraiseQL v2
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
//! Default validation rules for extended operators.
//!
//! This module provides sensible default validation rules for all 44+ rich type operators.
//! Rules can be overridden via fraiseql.toml configuration at compile time.
//!
//! Each rule is defined as a TOML-compatible structure that gets embedded in the
//! compiled schema.

use std::collections::HashMap;

use serde_json::{Value, json};

/// Get all default validation rules.
///
/// These rules are embedded in the compiled schema and applied at runtime
/// before SQL generation. They can be overridden per-application in fraiseql.toml.
pub fn get_default_rules() -> HashMap<String, Value> {
    let mut rules = HashMap::new();

    rules.extend(add_contact_rules());
    rules.extend(add_location_rules());
    rules.extend(add_financial_rules());
    rules.extend(add_identifier_rules());
    rules.extend(add_transportation_rules());
    rules.extend(add_network_rules());
    rules.extend(add_content_rules());
    rules.extend(add_measurement_rules());

    rules
}

fn add_contact_rules() -> HashMap<String, Value> {
    let mut rules = HashMap::new();

    // Email operators
    rules.insert(
        "email_domain_eq".to_string(),
        json!({
            "pattern": "^[a-z0-9]([a-z0-9-]*\\.)*[a-z0-9]([a-z0-9-]*[a-z0-9])?$",
            "description": "Valid domain name (RFC 1035)"
        }),
    );

    rules.insert(
        "email_domain_in".to_string(),
        json!({
            "pattern": "^[a-z0-9]([a-z0-9-]*\\.)*[a-z0-9]([a-z0-9-]*[a-z0-9])?$",
            "description": "Valid domain name in list"
        }),
    );

    rules.insert(
        "email_domain_endswith".to_string(),
        json!({
            "pattern": "^\\.[a-z0-9]([a-z0-9-]*\\.)*[a-z0-9]([a-z0-9-]*[a-z0-9])?$",
            "description": "Domain suffix (starts with dot)"
        }),
    );

    rules.insert(
        "email_local_part_startswith".to_string(),
        json!({
            "min_length": 1,
            "max_length": 64,
            "description": "Local part prefix (before @)"
        }),
    );

    // Phone operators
    rules.insert(
        "phone_country_code_eq".to_string(),
        json!({
            "pattern": "^[A-Z]{2}$",
            "description": "ISO 3166-1 alpha-2 country code"
        }),
    );

    rules.insert(
        "phone_country_code_in".to_string(),
        json!({
            "pattern": "^[A-Z]{2}$",
            "description": "ISO 3166-1 alpha-2 country code"
        }),
    );

    rules.insert(
        "phone_type_eq".to_string(),
        json!({
            "enum": ["mobile", "fixed", "tollfree", "premium", "shared", "voip"],
            "description": "Phone number type"
        }),
    );

    // URL operators
    rules.insert(
        "url_protocol_eq".to_string(),
        json!({
            "enum": ["http", "https", "ftp", "ftps", "ws", "wss"],
            "description": "Protocol scheme"
        }),
    );

    // Domain operators
    rules.insert(
        "domain_name_tld_eq".to_string(),
        json!({
            "pattern": "^[a-z]{2,}$",
            "description": "Top-level domain (lowercase)"
        }),
    );

    rules.insert(
        "domain_name_tld_in".to_string(),
        json!({
            "pattern": "^[a-z]{2,}$",
            "description": "Top-level domain"
        }),
    );

    rules.insert(
        "hostname_depth_eq".to_string(),
        json!({
            "min": 1.0,
            "max": 127.0,
            "description": "Hostname depth (number of labels)"
        }),
    );

    rules
}

fn add_location_rules() -> HashMap<String, Value> {
    let mut rules = HashMap::new();

    // PostalCode operators
    rules.insert(
        "postal_code_country_eq".to_string(),
        json!({
            "pattern": "^[A-Z]{2}$",
            "description": "ISO 3166-1 alpha-2 country code"
        }),
    );

    // Latitude/Longitude operators
    rules.insert(
        "latitude_within_range".to_string(),
        json!({
            "min": -90.0,
            "max": 90.0,
            "description": "Latitude in degrees"
        }),
    );

    rules.insert(
        "latitude_hemisphere_eq".to_string(),
        json!({
            "enum": ["North", "South"],
            "description": "Latitude hemisphere"
        }),
    );

    rules.insert(
        "longitude_within_range".to_string(),
        json!({
            "min": -180.0,
            "max": 180.0,
            "description": "Longitude in degrees"
        }),
    );

    rules.insert(
        "longitude_hemisphere_eq".to_string(),
        json!({
            "enum": ["East", "West"],
            "description": "Longitude hemisphere"
        }),
    );

    // Timezone operators
    rules.insert(
        "timezone_eq".to_string(),
        json!({
            "pattern": "^[A-Za-z_/]+$",
            "description": "IANA timezone identifier"
        }),
    );

    // LocaleCode operators
    rules.insert(
        "locale_code_eq".to_string(),
        json!({
            "pattern": "^[a-z]{2}(?:-[A-Z]{2})?$",
            "description": "BCP 47 locale code"
        }),
    );

    // LanguageCode operators
    rules.insert(
        "language_code_eq".to_string(),
        json!({
            "pattern": "^[a-z]{2}$",
            "description": "ISO 639-1 language code"
        }),
    );

    // CountryCode operators
    rules.insert(
        "country_code_eq".to_string(),
        json!({
            "pattern": "^[A-Z]{2}$",
            "description": "ISO 3166-1 alpha-2 country code"
        }),
    );

    rules
}

fn add_financial_rules() -> HashMap<String, Value> {
    let mut rules = HashMap::new();

    // IBAN operators
    rules.insert(
        "iban_country_eq".to_string(),
        json!({
            "pattern": "^[A-Z]{2}$",
            "description": "ISO 3166-1 country code"
        }),
    );

    rules.insert(
        "iban_check_digit_eq".to_string(),
        json!({
            "pattern": "^[0-9]{2}$",
            "length": 2,
            "description": "IBAN check digits"
        }),
    );

    // CUSIP operators
    rules.insert(
        "cusip_issuer_eq".to_string(),
        json!({
            "length": 6,
            "pattern": "^[A-Z0-9]{6}$",
            "description": "CUSIP issuer code"
        }),
    );

    // ISIN operators
    rules.insert(
        "isin_country_eq".to_string(),
        json!({
            "pattern": "^[A-Z]{2}$",
            "description": "ISO 3166-1 country code"
        }),
    );

    // SEDOL operators (UK securities)
    rules.insert(
        "sedol_check_digit_eq".to_string(),
        json!({
            "pattern": "^[0-9]$",
            "description": "SEDOL check digit"
        }),
    );

    // LEI operators
    rules.insert(
        "lei_country_eq".to_string(),
        json!({
            "pattern": "^[A-Z]{2}$",
            "description": "ISO 3166-1 country code"
        }),
    );

    // MIC operators (Market Identifier Code)
    rules.insert(
        "mic_country_eq".to_string(),
        json!({
            "pattern": "^[A-Z]{2}$",
            "description": "ISO 3166-1 country code"
        }),
    );

    // CurrencyCode operators
    rules.insert(
        "currency_code_eq".to_string(),
        json!({
            "pattern": "^[A-Z]{3}$",
            "description": "ISO 4217 currency code"
        }),
    );

    // Money operators
    rules.insert(
        "money_currency_eq".to_string(),
        json!({
            "pattern": "^[A-Z]{3}$",
            "description": "ISO 4217 currency code"
        }),
    );

    rules.insert(
        "money_amount_within_range".to_string(),
        json!({
            "description": "Numeric money amount range"
        }),
    );

    // ExchangeCode operators
    rules.insert(
        "exchange_code_eq".to_string(),
        json!({
            "pattern": "^[A-Z]{1,4}$",
            "description": "ISO 10383 exchange code"
        }),
    );

    // ExchangeRate operators
    rules.insert(
        "exchange_rate_within_range".to_string(),
        json!({
            "min": 0.0,
            "description": "Exchange rate range"
        }),
    );

    // StockSymbol operators
    rules.insert(
        "stock_symbol_eq".to_string(),
        json!({
            "pattern": "^[A-Z0-9]{1,5}$",
            "description": "Stock ticker symbol"
        }),
    );

    rules
}

fn add_identifier_rules() -> HashMap<String, Value> {
    let mut rules = HashMap::new();

    // Slug operators
    rules.insert(
        "slug_eq".to_string(),
        json!({
            "pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$",
            "description": "URL-safe slug"
        }),
    );

    // SemanticVersion operators
    rules.insert(
        "semantic_version_eq".to_string(),
        json!({
            "pattern": "^[0-9]+(\\.[0-9]+){0,2}(?:-[a-zA-Z0-9]+)?$",
            "description": "Semantic versioning (X.Y.Z)"
        }),
    );

    // HashSHA256 operators
    rules.insert(
        "hash_sha256_eq".to_string(),
        json!({
            "pattern": "^[a-f0-9]{64}$",
            "length": 64,
            "description": "SHA-256 hash (hexadecimal)"
        }),
    );

    // APIKey operators
    rules.insert(
        "api_key_eq".to_string(),
        json!({
            "min_length": 16,
            "max_length": 256,
            "description": "API key (alphanumeric, dashes, underscores)"
        }),
    );

    rules
}

fn add_transportation_rules() -> HashMap<String, Value> {
    let mut rules = HashMap::new();

    // LicensePlate operators
    rules.insert(
        "license_plate_country_eq".to_string(),
        json!({
            "pattern": "^[A-Z]{2}$",
            "description": "ISO 3166-1 country code"
        }),
    );

    // VIN operators
    rules.insert(
        "vin_wmi_eq".to_string(),
        json!({
            "length": 3,
            "pattern": "^[A-Z0-9]{3}$",
            "description": "VIN World Manufacturer Identifier"
        }),
    );

    rules.insert(
        "vin_manufacturer_eq".to_string(),
        json!({
            "length": 3,
            "pattern": "^[A-Z0-9]{3}$",
            "description": "VIN manufacturer code"
        }),
    );

    // TrackingNumber operators
    rules.insert(
        "tracking_number_carrier_eq".to_string(),
        json!({
            "enum": ["UPS", "FedEx", "DHL", "USPS", "Other"],
            "description": "Shipping carrier"
        }),
    );

    // ContainerNumber operators
    rules.insert(
        "container_number_owner_code_eq".to_string(),
        json!({
            "length": 3,
            "pattern": "^[A-Z]{3}$",
            "description": "ISO 6346 owner code"
        }),
    );

    rules
}

fn add_network_rules() -> HashMap<String, Value> {
    let mut rules = HashMap::new();

    // Port operators
    rules.insert(
        "port_eq".to_string(),
        json!({
            "min": 1.0,
            "max": 65535.0,
            "description": "Network port number"
        }),
    );

    // AirportCode operators
    rules.insert(
        "airport_code_eq".to_string(),
        json!({
            "pattern": "^[A-Z]{3}$",
            "description": "IATA airport code"
        }),
    );

    // PortCode operators
    rules.insert(
        "port_code_eq".to_string(),
        json!({
            "pattern": "^[A-Z]{3}$",
            "description": "UN/LOCODE port code"
        }),
    );

    // FlightNumber operators
    rules.insert(
        "flight_number_airline_code_eq".to_string(),
        json!({
            "pattern": "^[A-Z]{2}$",
            "description": "IATA airline code"
        }),
    );

    rules
}

fn add_content_rules() -> HashMap<String, Value> {
    let mut rules = HashMap::new();

    // MimeType operators
    rules.insert(
        "mime_type_eq".to_string(),
        json!({
            "pattern": "^[a-z]+/[a-z0-9.+\\-]+$",
            "description": "MIME type (e.g., image/png)"
        }),
    );

    // Color operators
    rules.insert(
        "color_format_eq".to_string(),
        json!({
            "enum": ["hex", "rgb", "hsl", "named"],
            "description": "Color format"
        }),
    );

    rules
}

fn add_measurement_rules() -> HashMap<String, Value> {
    let mut rules = HashMap::new();

    // Duration operators
    rules.insert(
        "duration_within_range".to_string(),
        json!({
            "min": 0.0,
            "description": "Duration in seconds"
        }),
    );

    // Percentage operators
    rules.insert(
        "percentage_within_range".to_string(),
        json!({
            "min": 0.0,
            "max": 100.0,
            "description": "Percentage value (0-100)"
        }),
    );

    rules
}

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

    #[test]
    fn test_default_rules_exist() {
        let rules = get_default_rules();
        assert!(!rules.is_empty());
        println!("Total default rules: {}", rules.len());
    }

    #[test]
    fn test_email_domain_rule() {
        let rules = get_default_rules();
        assert!(rules.contains_key("email_domain_eq"));
        assert!(rules.contains_key("email_domain_in"));
    }

    #[test]
    fn test_country_code_rule() {
        let rules = get_default_rules();
        assert!(rules.contains_key("country_code_eq"));
    }
}