ignite-core 0.6.1

Core types for Ignite, the admin panel for Rocket.rs: resources, fields, forms and runtime resources.
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
//! Form fields and table columns, as the panel renders them.

use std::str::FromStr;

use chrono::{NaiveDate, NaiveDateTime};

use crate::form::{FieldErrors, FormInput};

/// The kind of input a form field renders as, and how its value is read.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FieldKind {
    /// A single line of text.
    Text,
    /// Several lines of text.
    Textarea,
    /// A password. The form never shows the stored value.
    Password,
    /// A whole number, submitted as text. A resource's integer field is checked as a whole
    /// number. A profile field accepts decimals as well (see [`Field::check_number`]).
    Number,
    /// A decimal number, submitted as text, such as `9.5`. The string is the browser's
    /// step for the input: `"any"` accepts every value, and `"0.01"` steps by cents.
    Decimal(&'static str),
    /// A yes/no value. Read it with [`FormInput::checkbox`](crate::FormInput::checkbox).
    Checkbox,
    /// One choice from a list of `(value, label)` pairs.
    Select(&'static [(&'static str, &'static str)]),
    /// One choice from options that the resource loads from the database each time its
    /// form is shown, such as the rows of a related table. See
    /// [`IgniteResource::options`](crate::IgniteResource::options).
    Lookup,
    /// A calendar date, as `YYYY-MM-DD`.
    Date,
    /// A date and time, as `YYYY-MM-DDTHH:MM`, with seconds when given.
    DateTime,
    /// An email address, as Filament's `TextInput::make()->email()`. The value must be a
    /// valid address, as the browser's `type="email"` input defines one (see
    /// [`Field::check_email`]).
    Email,
    /// A URL, as Filament's `TextInput::make()->url()`. The value must parse as an absolute
    /// URL (see [`Field::check_url`]).
    Url,
    /// One choice from a list of `(value, label)` pairs, shown as radio buttons, as
    /// Filament's `Radio`. The value is checked as a [`Select`](Self::Select) is.
    Radio(&'static [(&'static str, &'static str)]),
    /// A color, as Filament's `ColorPicker`. The value is `#rrggbb`, stored in lowercase
    /// (see [`Field::check_color`]).
    Color,
    /// An image from the media library, as Filament's `FileUpload::make()->image()`. The
    /// value is the item's id, a positive whole number, stored as text. The panel shows the
    /// item through the app's [`MediaLibrary`](crate::MediaLibrary), when one is registered.
    Image,
    /// A file from the media library, as Filament's `FileUpload`. Stored as
    /// [`Image`](Self::Image) is.
    File,
    /// A link (ACF's link field): the JSON text `{"url": ..., "title": ..., "target": ...}`.
    /// The URL is required and must parse; `target` is `""` or `"_blank"` (see
    /// [`Field::check_link`]).
    Link,
    /// Rich text, as Filament's `RichEditor`. The panel renders a textarea marked with
    /// `data-ignite-editor`, and loads the editor script the app registers with
    /// [`register_editor`](crate::register_editor). Ignite bundles no editor. The value is
    /// the submitted HTML, unchanged.
    Wysiwyg,
    /// One post, chosen from options loaded each time the form is shown, as a
    /// [`Lookup`](Self::Lookup) is (ACF's post object field).
    PostObject,
    /// Several choices from options loaded each time the form is shown (ACF's relationship
    /// field): the multi-value [`PostObject`](Self::PostObject). The value is a JSON array
    /// of the chosen option values, as strings, such as `["12","40"]`.
    Relationship,
    /// One taxonomy term, chosen from loaded options, as a [`Lookup`](Self::Lookup) is.
    Taxonomy,
    /// One user, chosen from loaded options, as a [`Lookup`](Self::Lookup) is.
    User,
    /// Rows of the given sub-fields, as Filament's `Repeater`. The value is a JSON array
    /// with one object per row, keyed by sub-field name, such as
    /// `[{"title": "One"}, {"title": "Two"}]`. Each row is checked against the sub-fields.
    Repeater(&'static [Field]),
    /// The given sub-fields, kept together (ACF's group field). The value is one JSON
    /// object keyed by sub-field name.
    Group(&'static [Field]),
    /// Rows that each take one of the given blocks, as Filament's `Builder`. The value is a
    /// JSON array of `{"type": <block name>, "data": {<sub-field values>}}` objects, as
    /// Filament stores a builder.
    FlexibleContent(&'static [Block]),
    /// Several images from the media library (ACF's gallery field). The value is a JSON
    /// array of item ids, such as `[3,8]`.
    Gallery,
    /// The fields of another field group, reused (ACF's clone field). Ignite has no field
    /// groups of its own: the app resolves the group it clones and passes its fields here.
    /// The value is stored and checked as a [`Group`](Self::Group) of those fields is.
    Clone(&'static [Field]),
}

impl FieldKind {
    /// Whether the field's choices are loaded each time its form is shown, through
    /// [`IgniteResource::options`](crate::IgniteResource::options) or
    /// [`RuntimeData::options`](crate::RuntimeData::options): a lookup, post object,
    /// relationship, taxonomy or user field.
    pub fn loads_options(&self) -> bool {
        matches!(
            self,
            FieldKind::Lookup
                | FieldKind::PostObject
                | FieldKind::Relationship
                | FieldKind::Taxonomy
                | FieldKind::User
        )
    }
}

/// One block of a [`FieldKind::FlexibleContent`] field, as Filament's `Builder\Block` (an
/// ACF flexible content layout): a name, a label, and its sub-fields.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Block {
    /// The block's name, stored as the `type` of each row that uses it.
    pub name: &'static str,
    /// The label shown when choosing the block.
    pub label: &'static str,
    /// The block's sub-fields, in display order.
    pub fields: &'static [Field],
}

/// One input on a create or edit form.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Field {
    /// The form field name. It matches a key of the record.
    pub name: &'static str,
    /// The label shown next to the input.
    pub label: &'static str,
    /// The input type, and how the value is read.
    pub kind: FieldKind,
    /// Whether the form rejects a blank value.
    pub required: bool,
}

impl Field {
    /// Returns the trimmed value of a required field. If it is missing or
    /// blank, records "<label> is required" in `errors` under the field name
    /// and returns `None`.
    pub fn check_required<'a>(
        &self,
        form: &'a FormInput,
        errors: &mut FieldErrors,
    ) -> Option<&'a str> {
        check_required(self.name, self.label, form, errors)
    }

    /// Parses `text` as a whole number of type `T`. If it does not parse, records
    /// "<label> must be a whole number." in `errors` and returns `None`. Resources and
    /// profile fields both check numbers this way.
    pub fn check_whole_number<T: FromStr>(
        &self,
        text: &str,
        errors: &mut FieldErrors,
    ) -> Option<T> {
        check_whole_number(self.name, self.label, text, errors)
    }

    /// Parses `text` as a number, whole or decimal, such as `42` or `42.5`. Values that are
    /// not finite (`inf`, `NaN`) are refused. If it does not parse, records
    /// "<label> must be a number." in `errors` and returns `None`.
    pub fn check_number(&self, text: &str, errors: &mut FieldErrors) -> Option<f64> {
        check_number(self.name, self.label, text, errors)
    }

    /// Parses `text` as a decimal number of type `T` (`f32` or `f64`), such as `9.5`. A
    /// value that is not finite (`inf`, `NaN`, or an `f32` too large) is refused. If it
    /// does not parse, records "<label> must be a number." in `errors` and returns `None`.
    pub fn check_decimal<T: FromStr + Into<f64> + Copy>(
        &self,
        text: &str,
        errors: &mut FieldErrors,
    ) -> Option<T> {
        check_decimal(self.name, self.label, text, errors)
    }

    /// Parses `text` as a date and time: `YYYY-MM-DDTHH:MM` as a date-time input sends it,
    /// or with a space in place of the `T`, and with seconds when given. If it does not
    /// parse, records "<label> must be a date and time, as YYYY-MM-DD HH:MM." in `errors`
    /// and returns `None`.
    pub fn check_datetime(&self, text: &str, errors: &mut FieldErrors) -> Option<NaiveDateTime> {
        check_datetime(self.name, self.label, text, errors)
    }

    /// Checks that `text` is the value of one of `choices`. If it is not, records
    /// "Choose one of the options." in `errors` and returns false.
    pub fn check_choice(
        &self,
        text: &str,
        choices: &[(&str, &str)],
        errors: &mut FieldErrors,
    ) -> bool {
        check_choice(self.name, text, choices, errors)
    }

    /// Parses `text` as a `YYYY-MM-DD` date. If it does not parse, records
    /// "<label> must be a date, as YYYY-MM-DD." in `errors` and returns `None`.
    pub fn check_date(&self, text: &str, errors: &mut FieldErrors) -> Option<NaiveDate> {
        check_date(self.name, self.label, text, errors)
    }

    /// Checks that `text`, trimmed, is an email address, as the browser's `type="email"`
    /// input defines one: a local part of letters, digits and ``.!#$%&'*+/=?^_`{|}~-``, an
    /// `@`, and a domain of dot-separated labels of letters, digits and inner hyphens. If it
    /// is not, records "<label> must be a valid email address." and returns `None`.
    pub fn check_email(&self, text: &str, errors: &mut FieldErrors) -> Option<String> {
        check_email(self.name, self.label, text, errors)
    }

    /// Checks that `text`, trimmed, is a URL as ACF's URL field accepts one: an absolute URL
    /// that parses, such as `https://example.com/about`, or a protocol-relative one with a
    /// host, such as `//cdn.example.com/lib.js`. The `javascript:`, `data:` and `vbscript:`
    /// schemes are refused in any case and with any whitespace or control characters around
    /// them, because a link to one runs code. Otherwise records "<label> must be a valid URL."
    /// and returns `None`.
    pub fn check_url(&self, text: &str, errors: &mut FieldErrors) -> Option<String> {
        check_url(self.name, self.label, text, errors)
    }

    /// Checks that `text`, trimmed, is a color as `#rrggbb`, and returns it in lowercase. If
    /// it is not, records "<label> must be a color, as #rrggbb." and returns `None`.
    pub fn check_color(&self, text: &str, errors: &mut FieldErrors) -> Option<String> {
        check_color(self.name, self.label, text, errors)
    }

    /// Checks that `text` is the JSON of a link, `{"url": ..., "title": ..., "target": ...}`,
    /// whose target is `""` or `"_blank"` and whose URL is one ACF's link picker stores: a URL
    /// that passes [`check_url`](Self::check_url), a root-relative path (`/about`), an anchor
    /// (`#top`) or a query (`?a=b`). A blank URL, `//` with no host and a script scheme are
    /// refused. The title and target may be left out. Returns the link as JSON text with
    /// all three keys. Otherwise records "<label> must be a link with a valid URL." and
    /// returns `None`.
    pub fn check_link(&self, text: &str, errors: &mut FieldErrors) -> Option<String> {
        check_link(self.name, self.label, text, errors)
    }

    /// Checks `text` against the field's kind, and returns the text to store, for the kinds
    /// whose value is text: email, URL, radio, color, image, file, link, wysiwyg, post
    /// object, relationship, taxonomy, user, repeater, group, flexible content, gallery and
    /// clone. The structured kinds (relationship, repeater, group, flexible content, gallery,
    /// clone) take JSON text, check each value against its sub-field, and return the checked
    /// JSON; reading it back gives the same structure. An empty list or group is blank, so a
    /// required field records "<label> is required.". A required field must first pass
    /// [`check_required`](Self::check_required).
    ///
    /// The other kinds have checks of their own (such as
    /// [`check_whole_number`](Self::check_whole_number)); for them `text` is returned
    /// unchanged.
    pub fn check_value(&self, text: &str, errors: &mut FieldErrors) -> Option<String> {
        let kind = crate::FieldKindDef::from(self.kind);
        crate::value::check_value(self.name, self.label, self.required, &kind, text, errors)
    }
}

// The checks themselves, by field name and label, so a static `Field` and a runtime
// `FieldDef` run the same code. `name` is the key of the error in `errors`.

/// See [`Field::check_required`].
pub(crate) fn check_required<'a>(
    name: &str,
    label: &str,
    form: &'a FormInput,
    errors: &mut FieldErrors,
) -> Option<&'a str> {
    match form.get(name).map(str::trim) {
        Some(value) if !value.is_empty() => Some(value),
        _ => {
            errors.insert(name.to_string(), format!("{label} is required."));
            None
        }
    }
}

/// See [`Field::check_whole_number`].
pub(crate) fn check_whole_number<T: FromStr>(
    name: &str,
    label: &str,
    text: &str,
    errors: &mut FieldErrors,
) -> Option<T> {
    match text.parse::<T>() {
        Ok(value) => Some(value),
        Err(_) => {
            errors.insert(name.to_string(), format!("{label} must be a whole number."));
            None
        }
    }
}

/// See [`Field::check_number`].
pub(crate) fn check_number(
    name: &str,
    label: &str,
    text: &str,
    errors: &mut FieldErrors,
) -> Option<f64> {
    match text.parse::<f64>() {
        Ok(value) if value.is_finite() => Some(value),
        _ => {
            errors.insert(name.to_string(), format!("{label} must be a number."));
            None
        }
    }
}

/// See [`Field::check_decimal`].
pub(crate) fn check_decimal<T: FromStr + Into<f64> + Copy>(
    name: &str,
    label: &str,
    text: &str,
    errors: &mut FieldErrors,
) -> Option<T> {
    match text.parse::<T>() {
        Ok(value) if Into::<f64>::into(value).is_finite() => Some(value),
        _ => {
            errors.insert(name.to_string(), format!("{label} must be a number."));
            None
        }
    }
}

/// See [`Field::check_datetime`].
pub(crate) fn check_datetime(
    name: &str,
    label: &str,
    text: &str,
    errors: &mut FieldErrors,
) -> Option<NaiveDateTime> {
    const FORMATS: [&str; 4] = [
        "%Y-%m-%dT%H:%M",
        "%Y-%m-%dT%H:%M:%S",
        "%Y-%m-%d %H:%M",
        "%Y-%m-%d %H:%M:%S",
    ];
    let parsed = FORMATS
        .iter()
        .find_map(|format| NaiveDateTime::parse_from_str(text, format).ok());
    if parsed.is_none() {
        errors.insert(
            name.to_string(),
            format!("{label} must be a date and time, as YYYY-MM-DD HH:MM."),
        );
    }
    parsed
}

/// See [`Field::check_choice`].
pub(crate) fn check_choice(
    name: &str,
    text: &str,
    choices: &[(&str, &str)],
    errors: &mut FieldErrors,
) -> bool {
    if choices.iter().any(|(value, _)| *value == text) {
        return true;
    }
    errors.insert(name.to_string(), "Choose one of the options.".to_string());
    false
}

/// See [`Field::check_date`].
pub(crate) fn check_date(
    name: &str,
    label: &str,
    text: &str,
    errors: &mut FieldErrors,
) -> Option<NaiveDate> {
    match NaiveDate::parse_from_str(text, "%Y-%m-%d") {
        Ok(value) => Some(value),
        Err(_) => {
            errors.insert(
                name.to_string(),
                format!("{label} must be a date, as YYYY-MM-DD."),
            );
            None
        }
    }
}

/// See [`Field::check_email`].
pub(crate) fn check_email(
    name: &str,
    label: &str,
    text: &str,
    errors: &mut FieldErrors,
) -> Option<String> {
    let text = text.trim();
    if is_email(text) {
        return Some(text.to_string());
    }
    errors.insert(
        name.to_string(),
        format!("{label} must be a valid email address."),
    );
    None
}

/// Whether `text` is a valid email address, as the HTML standard defines one for
/// `<input type="email">`.
pub(crate) fn is_email(text: &str) -> bool {
    let Some((local, domain)) = text.split_once('@') else {
        return false;
    };
    let local_ok = !local.is_empty()
        && local
            .bytes()
            .all(|b| b.is_ascii_alphanumeric() || b".!#$%&'*+/=?^_`{|}~-".contains(&b));
    let domain_ok = !domain.is_empty()
        && domain.split('.').all(|part| {
            !part.is_empty()
                && part.len() <= 63
                && !part.starts_with('-')
                && !part.ends_with('-')
                && part.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-')
        });
    local_ok && domain_ok
}

/// See [`Field::check_url`].
pub(crate) fn check_url(
    name: &str,
    label: &str,
    text: &str,
    errors: &mut FieldErrors,
) -> Option<String> {
    let text = text.trim();
    if is_url(text) {
        return Some(text.to_string());
    }
    errors.insert(name.to_string(), format!("{label} must be a valid URL."));
    None
}

/// Whether `text` is an absolute URL that parses, or a protocol-relative URL (`//host/...`)
/// with a host, and its scheme cannot run code.
pub(crate) fn is_url(text: &str) -> bool {
    if has_script_scheme(text) {
        return false;
    }
    if let Some(rest) = text.strip_prefix("//") {
        // `//` must be followed by a host: not by another slash, which a browser skips.
        if rest.is_empty() || rest.starts_with(['/', '\\']) {
            return false;
        }
        return url::Url::parse(&format!("https:{text}"))
            .is_ok_and(|url| url.host_str().is_some_and(|host| !host.is_empty()));
    }
    match url::Url::parse(text) {
        Ok(url) => !matches!(url.scheme(), "javascript" | "data" | "vbscript"),
        Err(_) => false,
    }
}

/// Whether `text` is a link target as ACF's link picker stores one: a URL that passes
/// [`is_url`], a root-relative path (`/about`), an anchor (`#top`) or a query (`?a=b`). A
/// path may not start with `//` or `/\`, which a browser reads as another host.
pub(crate) fn is_link_url(text: &str) -> bool {
    if is_url(text) {
        return true;
    }
    let relative = match text.strip_prefix('/') {
        Some(rest) => !rest.starts_with(['/', '\\']),
        None => text.starts_with(['#', '?']),
    };
    relative && !text.chars().any(|c| c.is_control() || c.is_whitespace())
}

/// Whether `text` starts with the `javascript:`, `data:` or `vbscript:` scheme, in any case.
/// Whitespace and control characters are ignored, as a browser strips them from a URL
/// before it reads the scheme.
fn has_script_scheme(text: &str) -> bool {
    let scheme: String = text
        .chars()
        .filter(|c| !c.is_whitespace() && !c.is_control())
        .take_while(|c| *c != ':')
        .collect::<String>()
        .to_ascii_lowercase();
    let has_colon = text.contains(':');
    has_colon && matches!(scheme.as_str(), "javascript" | "data" | "vbscript")
}

/// See [`Field::check_color`].
pub(crate) fn check_color(
    name: &str,
    label: &str,
    text: &str,
    errors: &mut FieldErrors,
) -> Option<String> {
    let text = text.trim();
    if is_color(text) {
        return Some(text.to_ascii_lowercase());
    }
    errors.insert(
        name.to_string(),
        format!("{label} must be a color, as #rrggbb."),
    );
    None
}

/// Whether `text` is a color as `#rrggbb`, in either case.
pub(crate) fn is_color(text: &str) -> bool {
    text.len() == 7 && text.starts_with('#') && text[1..].bytes().all(|b| b.is_ascii_hexdigit())
}

/// See [`Field::check_link`].
pub(crate) fn check_link(
    name: &str,
    label: &str,
    text: &str,
    errors: &mut FieldErrors,
) -> Option<String> {
    let link = serde_json::from_str::<serde_json::Value>(text)
        .ok()
        .and_then(|value| link_value(&value));
    match link {
        Some(link) => Some(link.to_string()),
        None => {
            errors.insert(
                name.to_string(),
                format!("{label} must be a link with a valid URL."),
            );
            None
        }
    }
}

/// The link in `value`, with its three keys, if it is a link object whose URL is valid and
/// whose target is `""` or `"_blank"`.
pub(crate) fn link_value(value: &serde_json::Value) -> Option<serde_json::Value> {
    let object = value.as_object()?;
    let text = |key: &str| match object.get(key) {
        None | Some(serde_json::Value::Null) => Some(String::new()),
        Some(serde_json::Value::String(text)) => Some(text.trim().to_string()),
        Some(_) => None,
    };
    let url = text("url")?;
    let title = text("title")?;
    let target = text("target")?;
    if !is_link_url(&url) || !matches!(target.as_str(), "" | "_blank") {
        return None;
    }
    Some(serde_json::json!({ "url": url, "title": title, "target": target }))
}

/// One column of the list view.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Column {
    /// The record key the column shows. It matches a field name.
    pub key: &'static str,
    /// The column heading.
    pub label: &'static str,
    /// Whether the heading links to a sort by this column.
    pub sortable: bool,
    /// Whether the search box matches this column.
    pub searchable: bool,
}

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

    const TITLE: Field = Field {
        name: "title",
        label: "Title",
        kind: FieldKind::Text,
        required: true,
    };

    fn form(pairs: &[(&str, &str)]) -> FormInput {
        FormInput::new(
            pairs
                .iter()
                .map(|(k, v)| (k.to_string(), v.to_string()))
                .collect::<HashMap<_, _>>(),
        )
    }

    #[test]
    fn required_field_returns_trimmed_value() {
        let mut errors = FieldErrors::new();
        let input = form(&[("title", "  Hello  ")]);

        assert_eq!(TITLE.check_required(&input, &mut errors), Some("Hello"));
        assert!(errors.is_empty());
    }

    #[test]
    fn missing_required_field_records_a_message() {
        let mut errors = FieldErrors::new();

        assert_eq!(TITLE.check_required(&form(&[]), &mut errors), None);
        assert_eq!(
            errors.get("title").map(String::as_str),
            Some("Title is required.")
        );
    }

    const PRICE: Field = Field {
        name: "price",
        label: "Price",
        kind: FieldKind::Decimal("any"),
        required: true,
    };

    const LAUNCH: Field = Field {
        name: "launch",
        label: "Launch",
        kind: FieldKind::DateTime,
        required: true,
    };

    #[test]
    fn a_decimal_accepts_whole_and_fractional_numbers() {
        let mut errors = FieldErrors::new();

        assert_eq!(PRICE.check_decimal::<f64>("9.5", &mut errors), Some(9.5));
        assert_eq!(PRICE.check_decimal::<f64>("-3", &mut errors), Some(-3.0));
        assert_eq!(PRICE.check_decimal::<f32>("0.25", &mut errors), Some(0.25));
        assert!(errors.is_empty());
    }

    #[test]
    fn a_decimal_refuses_text_and_values_that_are_not_finite() {
        for text in ["abc", "9,5", "NaN", "inf", ""] {
            let mut errors = FieldErrors::new();
            assert_eq!(
                PRICE.check_decimal::<f64>(text, &mut errors),
                None,
                "{text}"
            );
            assert_eq!(
                errors.get("price").map(String::as_str),
                Some("Price must be a number."),
                "{text}"
            );
        }
        let mut errors = FieldErrors::new();
        assert_eq!(PRICE.check_decimal::<f32>("1e300", &mut errors), None);
        assert!(errors.contains_key("price"));
    }

    #[test]
    fn a_datetime_accepts_the_input_format_with_or_without_seconds() {
        let mut errors = FieldErrors::new();
        let expected = chrono::NaiveDate::from_ymd_opt(2026, 11, 10)
            .unwrap()
            .and_hms_opt(14, 30, 0)
            .unwrap();

        assert_eq!(
            LAUNCH.check_datetime("2026-11-10T14:30", &mut errors),
            Some(expected)
        );
        assert_eq!(
            LAUNCH.check_datetime("2026-11-10 14:30:00", &mut errors),
            Some(expected)
        );
        assert!(errors.is_empty());
    }

    #[test]
    fn a_datetime_refuses_what_is_not_a_date_and_time() {
        for text in ["soon", "2026-11-10", "2026-13-40T10:00", "10/11/2026 14:30"] {
            let mut errors = FieldErrors::new();
            assert_eq!(LAUNCH.check_datetime(text, &mut errors), None, "{text}");
            assert_eq!(
                errors.get("launch").map(String::as_str),
                Some("Launch must be a date and time, as YYYY-MM-DD HH:MM."),
                "{text}"
            );
        }
    }

    #[test]
    fn a_choice_must_be_one_of_the_choices() {
        let choices = [("draft", "Draft"), ("published", "Published")];
        let mut errors = FieldErrors::new();

        assert!(TITLE.check_choice("draft", &choices, &mut errors));
        assert!(errors.is_empty());
        assert!(!TITLE.check_choice("archived", &choices, &mut errors));
        assert_eq!(
            errors.get("title").map(String::as_str),
            Some("Choose one of the options.")
        );
    }

    #[test]
    fn blank_required_field_is_missing() {
        let mut errors = FieldErrors::new();

        assert_eq!(
            TITLE.check_required(&form(&[("title", "   ")]), &mut errors),
            None
        );
        assert!(errors.contains_key("title"));
    }
}