molten_document/error.rs
1//! Defines error types specific to document validation within the `molten-document` crate.
2//!
3//! This module provides a comprehensive set of error variants encapsulated by
4//! `DocumentValidationError`, each detailing specific reasons why a document
5//! might fail validation against its `FormDefinition`.
6use serde::{Deserialize, Serialize};
7use thiserror::Error;
8
9/// Represents errors encountered during the document validation process.
10#[derive(Error, Debug, PartialEq, Serialize, Deserialize)]
11pub enum DocumentValidationError {
12 /// Indicates that a required field was missing from the document or its value was null.
13 #[error("Field '{0}' is required but was missing or null")]
14 MissingRequiredField(String),
15
16 /// Indicates a type mismatch for a field's value.
17 #[error("Field '{field_id}' expected type '{expected_type}', but got '{got_type}'")]
18 InvalidType {
19 /// The ID of the field that had an invalid type.
20 field_id: String,
21 /// The expected data type for the field.
22 expected_type: String,
23 /// The actual data type received for the field.
24 got_type: String,
25 },
26
27 /// Indicates that a numerical field's value is below its specified minimum.
28 #[error("Field '{field_id}' value {value} is less than minimum {min}")]
29 ValueTooLow {
30 /// The ID of the numerical field.
31 field_id: String,
32 /// The value that was too low.
33 value: f64,
34 /// The minimum allowed value for the field.
35 min: f64,
36 },
37
38 /// Indicates that a numerical field's value is above its specified maximum.
39 #[error("Field '{field_id}' value {value} is greater than maximum {max}")]
40 ValueTooHigh {
41 /// The ID of the numerical field.
42 field_id: String,
43 /// The value that was too high.
44 value: f64,
45 /// The maximum allowed value for the field.
46 max: f64,
47 },
48
49 /// Indicates that a selection field's value is not among the allowed options.
50 #[error("Field '{field_id}' value '{value}' is not a valid option. Allowed: {allowed:?}")]
51 InvalidSelection {
52 /// The ID of the selection field.
53 field_id: String,
54 /// The invalid value received.
55 value: String,
56 /// The list of allowed options for the field.
57 allowed: Vec<String>,
58 },
59
60 /// Indicates that a date/time field's value is not in a valid ISO 8601 format.
61 #[error("Field '{field_id}' expected a date string (ISO 8601), but got '{value}'")]
62 InvalidDateFormat {
63 /// The ID of the date field.
64 field_id: String,
65 /// The invalid date string received.
66 value: String,
67 },
68
69 /// Indicates that the document's `form_id` does not match the `FormDefinition`'s ID.
70 #[error("Document form_id '{doc_form}' does not match definition id '{def_id}'")]
71 FormIdMismatch {
72 /// The `form_id` specified in the document.
73 doc_form: String,
74 /// The `id` specified in the `FormDefinition`.
75 def_id: String,
76 },
77}