(** Typed error codes for HTML5 validation messages.
This module defines a comprehensive hierarchy of validation errors using
polymorphic variants, organized by error category. Each error type is
documented with the specific HTML5 conformance requirement it represents.
The error hierarchy is:
- {!t} is the top-level type containing all errors wrapped by category
- Each category (e.g., {!attr_error}, {!aria_error}) groups related errors
- Inline descriptors like [[\`Attr of string]] provide self-documenting parameters
{2 Example Usage}
{[
(* Category-level matching *)
let is_accessibility_error = function
| `Aria _ | `Li_role _ -> true
| _ -> false
(* Fine-grained matching *)
match err with
| `Attr (`Duplicate_id (`Id id)) -> handle_duplicate id
| `Img `Missing_alt -> suggest_alt_text ()
| _ -> default_handler err
]}
*)
(** {1 Severity} *)
(** Severity level of a validation message.
- [Error]: Conformance error that must be fixed
- [Warning]: Likely problem that should be reviewed
- [Info]: Suggestion for best practices *)
type severity = Error | Warning | Info
(** {1 Attribute Errors}
Errors related to HTML attributes: disallowed attributes, missing required
attributes, invalid attribute values, and duplicate IDs. *)
(** Attribute-related validation errors.
These errors occur when attributes violate HTML5 content model rules:
- Attributes used on elements where they're not allowed
- Required attributes that are missing
- Attribute values that don't match expected formats
- Duplicate ID attributes within a document *)
type attr_error = [
| `Not_allowed of [`Attr of string] * [`Elem of string]
(** Attribute is not in the set of allowed attributes for this element.
Per HTML5 spec, each element has a defined set of content attributes;
using attributes outside this set is a conformance error.
Example: [type] attribute on a [
] element. *)
| `Not_allowed_here of [`Attr of string]
(** Attribute is valid on this element type but not in this context.
Some attributes are only allowed under specific conditions, such as
the [download] attribute which requires specific ancestor elements. *)
| `Not_allowed_when of [`Attr of string] * [`Elem of string] * [`Condition of string]
(** Attribute conflicts with another attribute or element state.
Example: [readonly] and [disabled] together, or [multiple] on
certain input types where it's not supported. *)
| `Missing of [`Elem of string] * [`Attr of string]
(** Element is missing a required attribute.
Per HTML5, certain elements have required attributes for conformance.
Example: [] requires [src] or [srcset], [] requires [type]. *)
| `Missing_one_of of [`Elem of string] * [`Attrs of string list]
(** Element must have at least one of the listed attributes.
Some elements require at least one from a set of attributes.
Example: [] needs [href] or [target] (or both). *)
| `Bad_value of [`Elem of string] * [`Attr of string] * [`Value of string] * [`Reason of string]
(** Attribute value doesn't match the expected format or enumeration.
HTML5 defines specific value spaces for many attributes (enumerations,
URLs, integers, etc.). This error indicates the value is malformed. *)
| `Bad_value_generic of [`Message of string]
(** Generic bad attribute value with custom message.
Used when the specific validation failure requires a custom explanation
that doesn't fit the standard bad value template. *)
| `Duplicate_id of [`Id of string]
(** Document contains multiple elements with the same ID.
Per HTML5, the [id] attribute must be unique within a document.
Duplicate IDs cause problems with fragment navigation, label
association, and JavaScript DOM queries. *)
| `Data_invalid_name of [`Reason of string]
(** Custom data attribute name violates naming rules.
[data-*] attribute names must be valid XML NCNames (no colons,
must start with letter or underscore). The reason explains
the specific naming violation. *)
| `Data_uppercase
(** Custom data attribute name contains uppercase letters.
[data-*] attribute names must not contain ASCII uppercase letters
(A-Z) per HTML5. Use lowercase with hyphens instead. *)
]
(** {1 Element Structure Errors}
Errors related to element usage, nesting, and content models. *)
(** Element structure validation errors.
These errors occur when elements violate HTML5 content model rules:
- Obsolete elements that should be replaced
- Elements used in wrong contexts (invalid parent/child relationships)
- Missing required child elements
- Empty elements that must have content *)
type element_error = [
| `Obsolete of [`Elem of string] * [`Suggestion of string]
(** Element is obsolete and should not be used.
HTML5 obsoletes certain elements from HTML4 (e.g., [], [
]).
The suggestion provides the recommended modern alternative. *)
| `Obsolete_attr of [`Elem of string] * [`Attr of string] * [`Suggestion of string option]
(** Attribute on this element is obsolete.
Some attributes are obsolete on specific elements but may be valid
elsewhere. Example: [align] on [
] (use CSS instead). *)
| `Obsolete_global_attr of [`Attr of string] * [`Suggestion of string]
(** Global attribute is obsolete on all elements.
Attributes like [bgcolor] are obsolete everywhere in HTML5. *)
| `Not_allowed_as_child of [`Child of string] * [`Parent of string]
(** Element cannot be a child of the specified parent.
HTML5 defines content models for each element specifying which
children are allowed. Example: [
] inside [
] is invalid. *)
| `Unknown of [`Elem of string]
(** Element name is not recognized.
The element is not defined in HTML5, SVG, or MathML specs.
May be a typo or a custom element without hyphen. *)
| `Must_not_descend of [`Elem of string] * [`Attr of string option] * [`Ancestor of string]
(** Element must not appear as descendant of the specified ancestor.
Some elements have restrictions on their ancestry regardless of
direct parent. Example: [