domainstack-derive 1.1.1

Derive macros for domainstack: #[derive(Validate, ToSchema, ToJsonSchema)] - validation + auto-generate OpenAPI/JSON Schema
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
use domainstack_derive::ToJsonSchema;
use domainstack_schema::{JsonSchemaBuilder, ToJsonSchema as ToJsonSchemaTrait};

// Test basic ToJsonSchema derivation
#[derive(ToJsonSchema)]
#[allow(dead_code)]
struct SimpleUser {
    #[validate(email)]
    #[validate(max_len = 255)]
    #[schema(description = "User's email address")]
    email: String,

    #[validate(range(min = 18, max = 120))]
    #[schema(description = "User's age")]
    age: u8,
}

#[test]
fn test_simple_json_schema_derivation() {
    let schema = SimpleUser::json_schema();

    // Schema should be an object
    let json = serde_json::to_value(&schema).unwrap();
    eprintln!(
        "SimpleUser JSON schema: {}",
        serde_json::to_string_pretty(&json).unwrap()
    );
    assert_eq!(json["type"], "object");

    // Should have email and age properties
    assert!(json["properties"]["email"].is_object());
    assert!(json["properties"]["age"].is_object());

    // Email should have format and maxLength
    assert_eq!(json["properties"]["email"]["type"], "string");
    assert_eq!(json["properties"]["email"]["format"], "email");
    assert_eq!(json["properties"]["email"]["maxLength"].as_u64(), Some(255));

    // Age should have minimum and maximum
    assert_eq!(json["properties"]["age"]["type"], "integer");
    assert_eq!(json["properties"]["age"]["minimum"].as_f64(), Some(18.0));
    assert_eq!(json["properties"]["age"]["maximum"].as_f64(), Some(120.0));

    // Should have required fields
    let required = json["required"].as_array().unwrap();
    assert!(required.contains(&serde_json::json!("email")));
    assert!(required.contains(&serde_json::json!("age")));
}

#[test]
fn test_schema_name() {
    assert_eq!(SimpleUser::schema_name(), "SimpleUser");
}

// Test optional fields
#[derive(ToJsonSchema)]
#[allow(dead_code)]
struct UserWithOptional {
    #[validate(email)]
    email: String,

    #[validate(min_len = 1)]
    #[validate(max_len = 100)]
    nickname: Option<String>,
}

#[test]
fn test_optional_fields() {
    let schema = UserWithOptional::json_schema();
    let json = serde_json::to_value(&schema).unwrap();

    // Only email should be required (nickname is Option<T>)
    let required = json["required"].as_array().unwrap();
    assert!(required.contains(&serde_json::json!("email")));
    assert!(!required.contains(&serde_json::json!("nickname")));

    // Nickname property should still exist
    assert!(json["properties"]["nickname"].is_object());
    assert_eq!(
        json["properties"]["nickname"]["minLength"].as_u64(),
        Some(1)
    );
    assert_eq!(
        json["properties"]["nickname"]["maxLength"].as_u64(),
        Some(100)
    );
}

// Test nested types
#[derive(ToJsonSchema)]
#[allow(dead_code)]
struct Address {
    #[validate(min_len = 1)]
    #[validate(max_len = 100)]
    street: String,

    #[validate(min_len = 1)]
    #[validate(max_len = 50)]
    city: String,
}

#[derive(ToJsonSchema)]
#[allow(dead_code)]
struct UserWithAddress {
    #[validate(email)]
    email: String,

    #[validate(nested)]
    address: Address,
}

#[test]
fn test_nested_types() {
    let schema = UserWithAddress::json_schema();
    let json = serde_json::to_value(&schema).unwrap();
    eprintln!(
        "UserWithAddress JSON schema: {}",
        serde_json::to_string_pretty(&json).unwrap()
    );

    // Address should be an object with properties
    assert!(json["properties"]["address"].is_object());
    assert!(json["properties"]["address"]["properties"]["street"].is_object());
}

// Test collections
#[derive(ToJsonSchema)]
#[allow(dead_code)]
struct Team {
    #[validate(min_len = 1)]
    #[validate(max_len = 50)]
    name: String,

    #[validate(each(nested))]
    #[validate(min_items = 1)]
    #[validate(max_items = 10)]
    members: Vec<SimpleUser>,
}

#[test]
fn test_collections() {
    let schema = Team::json_schema();
    let json = serde_json::to_value(&schema).unwrap();
    eprintln!(
        "Team JSON schema: {}",
        serde_json::to_string_pretty(&json).unwrap()
    );

    // Members should be an array
    assert_eq!(json["properties"]["members"]["type"], "array");
    assert_eq!(json["properties"]["members"]["minItems"].as_u64(), Some(1));
    assert_eq!(json["properties"]["members"]["maxItems"].as_u64(), Some(10));
}

// Test schema hints
#[derive(ToJsonSchema)]
#[allow(dead_code)]
#[schema(description = "Product in the catalog")]
struct Product {
    #[validate(min_len = 1)]
    #[validate(max_len = 100)]
    #[schema(description = "Product name")]
    name: String,

    #[validate(range(min = 0, max = 1000000))]
    #[schema(description = "Price in cents")]
    price: i32,
}

#[test]
fn test_schema_hints() {
    let schema = Product::json_schema();
    let json = serde_json::to_value(&schema).unwrap();
    eprintln!(
        "Product JSON schema: {}",
        serde_json::to_string_pretty(&json).unwrap()
    );

    // Name should have description
    assert_eq!(json["properties"]["name"]["description"], "Product name");

    // Price should have description
    assert_eq!(json["properties"]["price"]["description"], "Price in cents");

    // Struct-level description
    assert_eq!(json["description"], "Product in the catalog");
}

// Test JSON Schema builder integration
#[test]
fn test_json_schema_builder_integration() {
    let doc = JsonSchemaBuilder::new()
        .title("Test Schema")
        .description("Schema with auto-derived types")
        .register::<SimpleUser>()
        .register::<Product>()
        .build();

    let json_string = serde_json::to_string_pretty(&doc).unwrap();
    let json: serde_json::Value = serde_json::from_str(&json_string).unwrap();

    eprintln!("Full JSON Schema doc: {}", json_string);

    // Should have the schema meta fields
    assert!(json["$schema"].as_str().unwrap().contains("2020-12"));

    // Should have title
    assert_eq!(json["title"], "Test Schema");

    // Should have definitions
    assert!(json["$defs"]["SimpleUser"].is_object());
    assert!(json["$defs"]["Product"].is_object());
}

// Test string pattern rules
#[derive(ToJsonSchema)]
#[allow(dead_code)]
struct PatternTest {
    #[validate(alphanumeric)]
    #[schema(description = "Alphanumeric only")]
    code: String,

    #[validate(ascii)]
    #[schema(description = "ASCII characters only")]
    text: String,

    #[validate(non_empty)]
    name: String,

    #[validate(non_blank)]
    description: String,
}

#[test]
fn test_pattern_rules() {
    let schema = PatternTest::json_schema();
    let json = serde_json::to_value(&schema).unwrap();
    eprintln!(
        "PatternTest JSON schema: {}",
        serde_json::to_string_pretty(&json).unwrap()
    );

    // Alphanumeric should have pattern
    assert_eq!(json["properties"]["code"]["pattern"], "^[a-zA-Z0-9]*$");

    // ASCII should have pattern
    assert_eq!(json["properties"]["text"]["pattern"], "^[\\x00-\\x7F]*$");

    // Non-empty should have minLength
    assert_eq!(json["properties"]["name"]["minLength"].as_u64(), Some(1));

    // Non-blank should have pattern
    assert!(json["properties"]["description"]["pattern"]
        .as_str()
        .unwrap()
        .contains("\\S"));
}

// Test numeric rules
#[derive(ToJsonSchema)]
#[allow(dead_code)]
struct NumericRulesTest {
    #[validate(positive)]
    positive_number: i32,

    #[validate(negative)]
    negative_number: i32,

    #[validate(range(min = 0, max = 100))]
    percentage: u8,
}

#[test]
fn test_numeric_rules() {
    let schema = NumericRulesTest::json_schema();
    let json = serde_json::to_value(&schema).unwrap();
    eprintln!(
        "NumericRulesTest JSON schema: {}",
        serde_json::to_string_pretty(&json).unwrap()
    );

    // Positive should have exclusiveMinimum of 0
    assert_eq!(
        json["properties"]["positive_number"]["exclusiveMinimum"].as_f64(),
        Some(0.0)
    );

    // Negative should have exclusiveMaximum of 0
    assert_eq!(
        json["properties"]["negative_number"]["exclusiveMaximum"].as_f64(),
        Some(0.0)
    );

    // Range should have minimum and maximum
    assert_eq!(
        json["properties"]["percentage"]["minimum"].as_f64(),
        Some(0.0)
    );
    assert_eq!(
        json["properties"]["percentage"]["maximum"].as_f64(),
        Some(100.0)
    );
}

// Test array rules
#[derive(ToJsonSchema)]
#[allow(dead_code)]
struct ArrayRulesTest {
    #[validate(min_items = 1)]
    #[validate(max_items = 5)]
    tags: Vec<String>,

    #[validate(unique)]
    unique_ids: Vec<i32>,
}

#[test]
fn test_array_rules() {
    let schema = ArrayRulesTest::json_schema();
    let json = serde_json::to_value(&schema).unwrap();
    eprintln!(
        "ArrayRulesTest JSON schema: {}",
        serde_json::to_string_pretty(&json).unwrap()
    );

    // tags should have minItems and maxItems
    assert_eq!(json["properties"]["tags"]["minItems"].as_u64(), Some(1));
    assert_eq!(json["properties"]["tags"]["maxItems"].as_u64(), Some(5));

    // unique_ids should have uniqueItems
    assert_eq!(json["properties"]["unique_ids"]["uniqueItems"], true);
}

// Test format rules
#[derive(ToJsonSchema)]
#[allow(dead_code)]
struct FormatRulesTest {
    #[validate(email)]
    email_field: String,

    #[validate(url)]
    url_field: String,
}

#[test]
fn test_format_rules() {
    let schema = FormatRulesTest::json_schema();
    let json = serde_json::to_value(&schema).unwrap();

    // email should have format: email
    assert_eq!(json["properties"]["email_field"]["format"], "email");

    // url should have format: uri
    assert_eq!(json["properties"]["url_field"]["format"], "uri");
}

// Test length rules
#[derive(ToJsonSchema)]
#[allow(dead_code)]
struct LengthRulesTest {
    #[validate(length(min = 3, max = 50))]
    username: String,

    #[validate(min_len = 8)]
    password: String,

    #[validate(max_len = 1000)]
    bio: String,
}

#[test]
fn test_length_rules() {
    let schema = LengthRulesTest::json_schema();
    let json = serde_json::to_value(&schema).unwrap();

    // username should have minLength and maxLength
    assert_eq!(
        json["properties"]["username"]["minLength"].as_u64(),
        Some(3)
    );
    assert_eq!(
        json["properties"]["username"]["maxLength"].as_u64(),
        Some(50)
    );

    // password should have minLength
    assert_eq!(
        json["properties"]["password"]["minLength"].as_u64(),
        Some(8)
    );

    // bio should have maxLength
    assert_eq!(json["properties"]["bio"]["maxLength"].as_u64(), Some(1000));
}

// Test regex pattern
#[derive(ToJsonSchema)]
#[allow(dead_code)]
struct RegexTest {
    #[validate(matches_regex = "^[A-Z]{2}\\d{4}$")]
    code: String,
}

#[test]
fn test_regex_pattern() {
    let schema = RegexTest::json_schema();
    let json = serde_json::to_value(&schema).unwrap();

    assert_eq!(json["properties"]["code"]["pattern"], "^[A-Z]{2}\\d{4}$");
}

// Test contains/starts_with/ends_with
#[derive(ToJsonSchema)]
#[allow(dead_code)]
struct StringContentTest {
    #[validate(contains = "@")]
    has_at: String,

    #[validate(starts_with = "https://")]
    secure_url: String,

    #[validate(ends_with = ".com")]
    domain: String,
}

#[test]
fn test_string_content_rules() {
    let schema = StringContentTest::json_schema();
    let json = serde_json::to_value(&schema).unwrap();

    // contains should generate a pattern
    assert!(json["properties"]["has_at"]["pattern"]
        .as_str()
        .unwrap()
        .contains("@"));

    // starts_with should generate a pattern starting with ^
    let starts_pattern = json["properties"]["secure_url"]["pattern"]
        .as_str()
        .unwrap();
    assert!(starts_pattern.starts_with("^"));
    assert!(starts_pattern.contains("https://"));

    // ends_with should generate a pattern ending with $
    let ends_pattern = json["properties"]["domain"]["pattern"].as_str().unwrap();
    assert!(ends_pattern.ends_with("$"));
    assert!(ends_pattern.contains(".com"));
}