Skip to main content

ito_core/validate/
issue.rs

1//! Helpers for constructing validation issues.
2//!
3//! Validation runs typically accumulate multiple issues. These helpers keep call
4//! sites compact while producing consistent JSON-serializable issue shapes.
5
6use super::{LEVEL_ERROR, LEVEL_INFO, LEVEL_WARNING, ValidationIssue, ValidationLevel};
7
8/// Construct a [`ValidationIssue`] with a fixed `level`, `path`, and message.
9pub fn issue(
10    level: ValidationLevel,
11    path: impl AsRef<str>,
12    message: impl Into<String>,
13) -> ValidationIssue {
14    ValidationIssue {
15        level: level.to_string(),
16        path: path.as_ref().to_string(),
17        message: message.into(),
18        line: None,
19        column: None,
20        metadata: None,
21    }
22}
23
24/// Convenience constructor for an `ERROR` issue.
25pub fn error(path: impl AsRef<str>, message: impl Into<String>) -> ValidationIssue {
26    issue(LEVEL_ERROR, path, message)
27}
28
29/// Convenience constructor for a `WARNING` issue.
30pub fn warning(path: impl AsRef<str>, message: impl Into<String>) -> ValidationIssue {
31    issue(LEVEL_WARNING, path, message)
32}
33
34/// Convenience constructor for an `INFO` issue.
35pub fn info(path: impl AsRef<str>, message: impl Into<String>) -> ValidationIssue {
36    issue(LEVEL_INFO, path, message)
37}
38
39/// Attach a 1-based line number to an existing issue.
40pub fn with_line(mut i: ValidationIssue, line: u32) -> ValidationIssue {
41    i.line = Some(line);
42    i
43}
44
45/// Attach a 1-based line + column location to an existing issue.
46pub fn with_loc(mut i: ValidationIssue, line: u32, column: u32) -> ValidationIssue {
47    i.line = Some(line);
48    i.column = Some(column);
49    i
50}
51
52/// Attach structured metadata to an existing issue.
53pub fn with_metadata(mut i: ValidationIssue, metadata: serde_json::Value) -> ValidationIssue {
54    i.metadata = Some(metadata);
55    i
56}