allowthem-server 0.0.5

HTTP server and middleware for allowthem
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
use std::collections::HashMap;

use serde::Serialize;
use serde_json::Value;

/// Describes a custom field for template rendering.
#[derive(Debug, Clone, Serialize)]
pub struct CustomFieldDescriptor {
    pub name: String,
    pub label: String,
    pub field_type: FieldType,
    pub required: bool,
    pub help_text: Option<String>,
    pub min_length: Option<u64>,
    pub max_length: Option<u64>,
    pub minimum: Option<f64>,
    pub maximum: Option<f64>,
    pub default_value: Option<Value>,
    pub enum_values: Option<Vec<Value>>,
}

#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum FieldType {
    Text,
    Email,
    Url,
    Textarea,
    Number,
    Checkbox,
    Select,
}

/// Pre-compiled schema + validator + field descriptors.
pub struct CustomSchemaConfig {
    pub schema: Value,
    pub validator: jsonschema::Validator,
    pub fields: Vec<CustomFieldDescriptor>,
}

/// Validate that a schema is a flat object (no nested objects/arrays in properties).
///
/// The schema must have `"type": "object"` and a `"properties"` map where each
/// property's type is a scalar (`string`, `integer`, `number`, `boolean`).
/// Returns `Err` with a description if the schema is unsuitable.
pub fn validate_custom_schema(schema: &Value) -> Result<(), String> {
    let obj = schema
        .as_object()
        .ok_or_else(|| "schema must be a JSON object".to_string())?;

    match obj.get("type").and_then(Value::as_str) {
        Some("object") => {}
        Some(other) => return Err(format!("schema type must be \"object\", got \"{other}\"")),
        None => return Err("schema must have \"type\": \"object\"".to_string()),
    }

    let props = match obj.get("properties").and_then(Value::as_object) {
        Some(p) => p,
        None => return Ok(()), // no properties is valid (empty form)
    };

    for (name, prop) in props {
        let prop_obj = prop
            .as_object()
            .ok_or_else(|| format!("property \"{name}\" must be a JSON object"))?;
        if let Some(ty) = prop_obj.get("type").and_then(Value::as_str) {
            match ty {
                "string" | "integer" | "number" | "boolean" => {}
                "object" | "array" => {
                    return Err(format!(
                        "property \"{name}\" has type \"{ty}\"; nested objects/arrays are not supported"
                    ));
                }
                other => {
                    return Err(format!(
                        "property \"{name}\" has unsupported type \"{other}\""
                    ));
                }
            }
        }
    }

    Ok(())
}

/// Extract field descriptors from a JSON Schema for template rendering.
///
/// Properties are iterated in insertion order (requires `preserve_order`
/// feature on `serde_json`).
pub fn extract_field_descriptors(schema: &Value) -> Vec<CustomFieldDescriptor> {
    let obj = match schema.as_object() {
        Some(o) => o,
        None => return Vec::new(),
    };

    let props = match obj.get("properties").and_then(Value::as_object) {
        Some(p) => p,
        None => return Vec::new(),
    };

    let required_set: Vec<&str> = obj
        .get("required")
        .and_then(Value::as_array)
        .map(|arr| arr.iter().filter_map(Value::as_str).collect())
        .unwrap_or_default();

    props
        .iter()
        .map(|(name, prop)| {
            let prop_obj = prop.as_object();
            let ty = prop_obj
                .and_then(|o| o.get("type"))
                .and_then(Value::as_str)
                .unwrap_or("string");
            let format = prop_obj
                .and_then(|o| o.get("format"))
                .and_then(Value::as_str);
            let has_enum = prop_obj
                .and_then(|o| o.get("enum"))
                .and_then(Value::as_array)
                .is_some();

            let field_type = match (ty, format, has_enum) {
                ("string", _, true) => FieldType::Select,
                ("string", Some("email"), _) => FieldType::Email,
                ("string", Some("uri"), _) => FieldType::Url,
                ("string", Some("textarea"), _) => FieldType::Textarea,
                ("integer" | "number", _, _) => FieldType::Number,
                ("boolean", _, _) => FieldType::Checkbox,
                _ => FieldType::Text,
            };

            let label = prop_obj
                .and_then(|o| o.get("title"))
                .and_then(Value::as_str)
                .map(String::from)
                .unwrap_or_else(|| title_case(name));

            CustomFieldDescriptor {
                name: name.clone(),
                label,
                field_type,
                required: required_set.contains(&name.as_str()),
                help_text: prop_obj
                    .and_then(|o| o.get("description"))
                    .and_then(Value::as_str)
                    .map(String::from),
                min_length: prop_obj
                    .and_then(|o| o.get("minLength"))
                    .and_then(Value::as_u64),
                max_length: prop_obj
                    .and_then(|o| o.get("maxLength"))
                    .and_then(Value::as_u64),
                minimum: prop_obj
                    .and_then(|o| o.get("minimum"))
                    .and_then(Value::as_f64),
                maximum: prop_obj
                    .and_then(|o| o.get("maximum"))
                    .and_then(Value::as_f64),
                default_value: prop_obj.and_then(|o| o.get("default")).cloned(),
                enum_values: prop_obj
                    .and_then(|o| o.get("enum"))
                    .and_then(Value::as_array)
                    .cloned(),
            }
        })
        .collect()
}

/// Extract custom_data fields from form body, coerce types per schema, return as Value.
///
/// HTML forms submit everything as strings. This function:
/// - Looks for keys starting with `custom_data[` and strips the prefix/suffix.
/// - Coerces values based on the schema property type.
/// - For booleans, iterates schema properties so that absent checkboxes map to `false`.
/// - Omits empty optional string fields from the result.
pub fn extract_and_coerce_custom_data(
    form_data: &HashMap<String, String>,
    schema: &Value,
) -> Value {
    let props = match schema
        .as_object()
        .and_then(|o| o.get("properties"))
        .and_then(Value::as_object)
    {
        Some(p) => p,
        None => return Value::Object(serde_json::Map::new()),
    };

    let required_set: Vec<&str> = schema
        .as_object()
        .and_then(|o| o.get("required"))
        .and_then(Value::as_array)
        .map(|arr| arr.iter().filter_map(Value::as_str).collect())
        .unwrap_or_default();

    // Build a map of custom_data[key] -> value from the form submission
    let mut custom_values: HashMap<&str, &str> = HashMap::new();
    for (key, value) in form_data {
        if let Some(field_name) = key
            .strip_prefix("custom_data[")
            .and_then(|s| s.strip_suffix(']'))
        {
            custom_values.insert(field_name, value.as_str());
        }
    }

    let mut result = serde_json::Map::new();

    for (name, prop) in props {
        let ty = prop
            .as_object()
            .and_then(|o| o.get("type"))
            .and_then(Value::as_str)
            .unwrap_or("string");

        match ty {
            "boolean" => {
                // Checkbox: present means true, absent means false
                let checked = custom_values
                    .get(name.as_str())
                    .is_some_and(|v| !v.is_empty());
                result.insert(name.clone(), Value::Bool(checked));
            }
            "integer" => {
                if let Some(raw) = custom_values.get(name.as_str()) {
                    if raw.is_empty() {
                        // Skip empty optional fields
                        if required_set.contains(&name.as_str()) {
                            result.insert(name.clone(), Value::Null);
                        }
                    } else if let Ok(n) = raw.parse::<i64>() {
                        result.insert(name.clone(), Value::Number(n.into()));
                    } else {
                        // Store as string; validation will catch the type mismatch
                        result.insert(name.clone(), Value::String((*raw).to_string()));
                    }
                }
            }
            "number" => {
                if let Some(raw) = custom_values.get(name.as_str()) {
                    if raw.is_empty() {
                        if required_set.contains(&name.as_str()) {
                            result.insert(name.clone(), Value::Null);
                        }
                    } else if let Ok(n) = raw.parse::<f64>() {
                        if let Some(num) = serde_json::Number::from_f64(n) {
                            result.insert(name.clone(), Value::Number(num));
                        } else {
                            result.insert(name.clone(), Value::String((*raw).to_string()));
                        }
                    } else {
                        result.insert(name.clone(), Value::String((*raw).to_string()));
                    }
                }
            }
            _ => {
                // string types
                if let Some(raw) = custom_values.get(name.as_str()) {
                    if raw.is_empty() && !required_set.contains(&name.as_str()) {
                        // Omit empty optional strings
                    } else {
                        result.insert(name.clone(), Value::String((*raw).to_string()));
                    }
                }
            }
        }
    }

    Value::Object(result)
}

/// Map jsonschema validation errors to field-level error messages.
///
/// Each error is mapped to `(field_name, message)`. For root-level errors
/// (e.g., missing required fields), the field name is extracted from the
/// instance path.
pub fn format_validation_errors(
    errors: &[jsonschema::error::ValidationError<'_>],
) -> Vec<(String, String)> {
    errors
        .iter()
        .map(|err| {
            let path = err.instance_path().as_str();
            // instance_path is a JSON pointer like "/field_name"
            let field = path.strip_prefix('/').unwrap_or(path);
            (field.to_string(), err.to_string())
        })
        .collect()
}

/// Convert "snake_case" or "kebab-case" field names to title case labels.
fn title_case(s: &str) -> String {
    s.split(['_', '-'])
        .filter(|w| !w.is_empty())
        .map(|word| {
            let mut chars = word.chars();
            match chars.next() {
                Some(first) => {
                    let upper: String = first.to_uppercase().collect();
                    let rest: String = chars.collect();
                    format!("{upper}{rest}")
                }
                None => String::new(),
            }
        })
        .collect::<Vec<_>>()
        .join(" ")
}

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

    #[test]
    fn validate_custom_schema_rejects_nested_objects() {
        let schema = json!({
            "type": "object",
            "properties": {
                "address": {
                    "type": "object",
                    "properties": {
                        "street": { "type": "string" }
                    }
                }
            }
        });
        let result = validate_custom_schema(&schema);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("nested objects/arrays"));
    }

    #[test]
    fn validate_custom_schema_rejects_arrays() {
        let schema = json!({
            "type": "object",
            "properties": {
                "tags": {
                    "type": "array",
                    "items": { "type": "string" }
                }
            }
        });
        let result = validate_custom_schema(&schema);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("nested objects/arrays"));
    }

    #[test]
    fn validate_custom_schema_accepts_flat_object() {
        let schema = json!({
            "type": "object",
            "properties": {
                "company": { "type": "string" },
                "age": { "type": "integer" },
                "score": { "type": "number" },
                "active": { "type": "boolean" }
            }
        });
        assert!(validate_custom_schema(&schema).is_ok());
    }

    #[test]
    fn validate_custom_schema_rejects_non_object_type() {
        let schema = json!({
            "type": "string"
        });
        let result = validate_custom_schema(&schema);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("must be \"object\""));
    }

    #[test]
    fn validate_custom_schema_accepts_empty_properties() {
        let schema = json!({
            "type": "object",
            "properties": {}
        });
        assert!(validate_custom_schema(&schema).is_ok());
    }

    #[test]
    fn validate_custom_schema_accepts_no_properties() {
        let schema = json!({
            "type": "object"
        });
        assert!(validate_custom_schema(&schema).is_ok());
    }

    #[test]
    fn extract_field_descriptors_produces_correct_types() {
        let schema = json!({
            "type": "object",
            "required": ["email", "company"],
            "properties": {
                "company": {
                    "type": "string",
                    "title": "Company Name",
                    "description": "Your company"
                },
                "contact_email": {
                    "type": "string",
                    "format": "email"
                },
                "website": {
                    "type": "string",
                    "format": "uri"
                },
                "bio": {
                    "type": "string",
                    "format": "textarea",
                    "maxLength": 500
                },
                "age": {
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 150
                },
                "score": {
                    "type": "number"
                },
                "newsletter": {
                    "type": "boolean"
                },
                "plan": {
                    "type": "string",
                    "enum": ["free", "pro", "enterprise"]
                }
            }
        });

        let fields = extract_field_descriptors(&schema);
        assert_eq!(fields.len(), 8);

        let company = &fields[0];
        assert_eq!(company.name, "company");
        assert_eq!(company.label, "Company Name");
        assert_eq!(company.field_type, FieldType::Text);
        assert!(company.required);
        assert_eq!(company.help_text.as_deref(), Some("Your company"));

        let contact = &fields[1];
        assert_eq!(contact.name, "contact_email");
        assert_eq!(contact.field_type, FieldType::Email);
        assert!(!contact.required);
        // Auto-generated label from field name
        assert_eq!(contact.label, "Contact Email");

        let website = &fields[2];
        assert_eq!(website.field_type, FieldType::Url);

        let bio = &fields[3];
        assert_eq!(bio.field_type, FieldType::Textarea);
        assert_eq!(bio.max_length, Some(500));

        let age = &fields[4];
        assert_eq!(age.field_type, FieldType::Number);
        assert_eq!(age.minimum, Some(0.0));
        assert_eq!(age.maximum, Some(150.0));

        let score = &fields[5];
        assert_eq!(score.field_type, FieldType::Number);

        let newsletter = &fields[6];
        assert_eq!(newsletter.field_type, FieldType::Checkbox);

        let plan = &fields[7];
        assert_eq!(plan.field_type, FieldType::Select);
        assert!(plan.enum_values.is_some());
        assert_eq!(plan.enum_values.as_ref().map(|v| v.len()), Some(3));
    }

    #[test]
    fn extract_and_coerce_string_fields() {
        let schema = json!({
            "type": "object",
            "properties": {
                "company": { "type": "string" }
            }
        });

        let mut form = HashMap::new();
        form.insert("custom_data[company]".to_string(), "Acme Corp".to_string());

        let result = extract_and_coerce_custom_data(&form, &schema);
        assert_eq!(result["company"], "Acme Corp");
    }

    #[test]
    fn extract_and_coerce_integer_fields() {
        let schema = json!({
            "type": "object",
            "properties": {
                "age": { "type": "integer" }
            }
        });

        let mut form = HashMap::new();
        form.insert("custom_data[age]".to_string(), "25".to_string());

        let result = extract_and_coerce_custom_data(&form, &schema);
        assert_eq!(result["age"], 25);
        assert!(result["age"].is_i64());
    }

    #[test]
    fn extract_and_coerce_number_fields() {
        let schema = json!({
            "type": "object",
            "properties": {
                "score": { "type": "number" }
            }
        });

        let mut form = HashMap::new();
        form.insert("custom_data[score]".to_string(), "3.14".to_string());

        let result = extract_and_coerce_custom_data(&form, &schema);
        assert_eq!(result["score"], 3.14);
    }

    #[test]
    fn extract_and_coerce_checkbox_present() {
        let schema = json!({
            "type": "object",
            "properties": {
                "newsletter": { "type": "boolean" }
            }
        });

        let mut form = HashMap::new();
        form.insert("custom_data[newsletter]".to_string(), "true".to_string());

        let result = extract_and_coerce_custom_data(&form, &schema);
        assert_eq!(result["newsletter"], true);
    }

    #[test]
    fn extract_and_coerce_checkbox_absent_is_false() {
        let schema = json!({
            "type": "object",
            "properties": {
                "newsletter": { "type": "boolean" }
            }
        });

        // No custom_data[newsletter] in form — checkbox was unchecked
        let form: HashMap<String, String> = HashMap::new();

        let result = extract_and_coerce_custom_data(&form, &schema);
        assert_eq!(result["newsletter"], false);
    }

    #[test]
    fn extract_omits_empty_optional_strings() {
        let schema = json!({
            "type": "object",
            "required": ["name"],
            "properties": {
                "name": { "type": "string" },
                "bio": { "type": "string" }
            }
        });

        let mut form = HashMap::new();
        form.insert("custom_data[name]".to_string(), "Alice".to_string());
        form.insert("custom_data[bio]".to_string(), String::new());

        let result = extract_and_coerce_custom_data(&form, &schema);
        assert_eq!(result["name"], "Alice");
        // bio is empty and optional, so it should be omitted
        assert!(result.get("bio").is_none());
    }

    #[test]
    fn extract_includes_empty_required_strings() {
        let schema = json!({
            "type": "object",
            "required": ["name"],
            "properties": {
                "name": { "type": "string" }
            }
        });

        let mut form = HashMap::new();
        form.insert("custom_data[name]".to_string(), String::new());

        let result = extract_and_coerce_custom_data(&form, &schema);
        // Required field submitted as empty should still be included
        // so that validation catches it
        assert_eq!(result["name"], "");
    }

    #[test]
    fn format_validation_errors_maps_to_fields() {
        let schema = json!({
            "type": "object",
            "required": ["name"],
            "properties": {
                "name": { "type": "string", "minLength": 1 },
                "age": { "type": "integer", "minimum": 0 }
            }
        });

        let validator = jsonschema::validator_for(&schema).expect("valid schema");
        let instance = json!({ "age": -1 });

        let errors: Vec<_> = validator.iter_errors(&instance).collect();
        assert!(!errors.is_empty());

        let formatted = format_validation_errors(&errors);
        assert!(!formatted.is_empty());
        // Each error should have a field name and message
        for (field, msg) in &formatted {
            assert!(!msg.is_empty(), "error message should not be empty");
            // field may be empty for root-level errors (like missing required)
            let _ = field;
        }
    }

    #[test]
    fn title_case_conversion() {
        assert_eq!(title_case("company_name"), "Company Name");
        assert_eq!(title_case("contact-email"), "Contact Email");
        assert_eq!(title_case("simple"), "Simple");
        assert_eq!(title_case("already_Good"), "Already Good");
    }

    #[test]
    fn extract_ignores_non_custom_data_keys() {
        let schema = json!({
            "type": "object",
            "properties": {
                "company": { "type": "string" }
            }
        });

        let mut form = HashMap::new();
        form.insert("email".to_string(), "test@example.com".to_string());
        form.insert("password".to_string(), "secret".to_string());
        form.insert("custom_data[company]".to_string(), "Acme".to_string());

        let result = extract_and_coerce_custom_data(&form, &schema);
        let obj = result.as_object().expect("should be object");
        assert_eq!(obj.len(), 1);
        assert_eq!(result["company"], "Acme");
    }
}