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
//! Content validation (text content, facets, error helpers) for DOM validation.
use crate::error::{ErrorLevel, StructuredError, ValidationErrorType};
use crate::node::{NodeType, XmlNode};
use crate::schema::types::{ContentModel, ElementDef, SimpleType, TypeDef};
use crate::schema::xsd::facets::{FacetConstraints, FacetValidator};
use crate::schema::xsd::primitive::PrimitiveKind;
use super::DomSchemaValidator;
impl DomSchemaValidator {
/// Collects text content from child nodes.
pub(crate) fn collect_text_content(&self, node: &XmlNode) -> String {
let mut text = String::new();
for child in node.get_child_nodes() {
match child.get_type() {
NodeType::Text | NodeType::CData => {
if let Some(content) = child.get_content() {
text.push_str(&content);
}
}
_ => {}
}
}
text
}
/// Validates text content against the element's type.
pub(crate) fn validate_text_content(
&self,
node: &XmlNode,
elem: &ElementDef,
errors: &mut Vec<StructuredError>,
) {
let text_content = self.collect_text_content(node);
// Get type definition
let type_def = if let Some(ref type_ref) = elem.type_ref {
self.schema.get_type(type_ref).cloned()
} else {
elem.inline_type.clone()
};
match type_def {
Some(TypeDef::Simple(simple)) => {
// Run both facet and primitive (lexical/value-space) checks.
// We do not skip on empty text — primitives like xs:integer
// must reject empty content, while xs:string-derived types
// resolve to no PrimitiveKind and pass through unchanged.
self.validate_simple_type_facets(
node,
&simple,
&text_content,
elem.nillable,
errors,
);
}
Some(TypeDef::Complex(complex)) => {
// Check for SimpleContent with base type
if let ContentModel::SimpleContent { base_type } = &complex.content {
if let Some(TypeDef::Simple(simple)) = self.schema.get_type(base_type) {
let simple = simple.clone();
self.validate_simple_type_facets(
node,
&simple,
&text_content,
elem.nillable,
errors,
);
}
} else if !complex.mixed && !text_content.is_empty() {
// Non-mixed complex types shouldn't have text content
if let ContentModel::Sequence(_)
| ContentModel::Choice(_)
| ContentModel::All(_)
| ContentModel::ComplexExtension { .. } = &complex.content
{
let trimmed = text_content.trim();
if !trimmed.is_empty() {
let node_name = node.get_name();
let error = self
.make_error(
ValidationErrorType::InvalidContent,
format!(
"element '{}' has element-only content but contains text",
node_name
),
node,
)
.with_node_name(&node_name)
.with_level(ErrorLevel::Error);
if self.should_add_error(errors) {
errors.push(error);
}
}
}
}
}
None => {}
}
}
/// Validates text content against simple type facets and (where the
/// type resolves to a built-in XSD primitive) its lexical/value space.
pub(crate) fn validate_simple_type_facets(
&self,
node: &XmlNode,
simple: &SimpleType,
text_content: &str,
nillable: bool,
errors: &mut Vec<StructuredError>,
) {
// User-declared facets (minLength, pattern, enumeration, …).
// Skip on empty content — facet constraints like minLength=0 would
// pass, but more importantly we don't want a spurious extra error on
// top of any primitive-level "empty value" error.
if !text_content.is_empty() {
let constraints = self.create_facet_constraints(simple);
let validator = FacetValidator::new(&constraints);
if let Err(facet_error) = validator.validate(text_content) {
let node_name = node.get_name();
let error = self
.make_error(
ValidationErrorType::InvalidTextContent,
format!("element '{}': {}", node_name, facet_error),
node,
)
.with_node_name(&node_name)
.with_level(ErrorLevel::Error);
if self.should_add_error(errors) {
errors.push(error);
}
}
}
// Built-in primitive lexical/value-space check (e.g., xs:integer
// rejecting "1.5" or "", xs:int rejecting 2147483648). Skip for an
// empty, nillable element: `xsi:nil="true"` legitimately leaves the
// content empty and it must not be checked against the primitive type.
if text_content.is_empty() && nillable {
return;
}
if let Some(kind) = PrimitiveKind::resolve(&self.schema, simple)
&& let Err(prim_error) = kind.validate(text_content)
{
let node_name = node.get_name();
let error = self
.make_error(
ValidationErrorType::InvalidTextContent,
format!("element '{}': {}", node_name, prim_error),
node,
)
.with_node_name(&node_name)
.with_level(ErrorLevel::Error);
if self.should_add_error(errors) {
errors.push(error);
}
}
}
/// Creates FacetConstraints from a SimpleType definition.
pub(crate) fn create_facet_constraints(&self, simple: &SimpleType) -> FacetConstraints {
let mut constraints = FacetConstraints::new();
if let Some(min_len) = simple.min_length {
constraints = constraints.with_min_length(min_len as usize);
}
if let Some(max_len) = simple.max_length {
constraints = constraints.with_max_length(max_len as usize);
}
if let Some(ref min_inc) = simple.min_inclusive {
constraints = constraints.with_min_inclusive(min_inc.clone());
}
if let Some(ref max_inc) = simple.max_inclusive {
constraints = constraints.with_max_inclusive(max_inc.clone());
}
if !simple.enumeration.is_empty() {
constraints = constraints.with_enumeration(simple.enumeration.clone());
}
if let Some(ref pattern) = simple.pattern {
constraints = constraints.with_pattern(pattern.clone());
}
constraints
}
/// Creates a structured error with context.
pub(crate) fn make_error(
&self,
error_type: ValidationErrorType,
message: impl Into<String>,
node: &XmlNode,
) -> StructuredError {
let mut error = StructuredError::new(message, error_type);
if let Some(line) = node.line() {
error = error.with_line(line);
}
if let Some(column) = node.column() {
error = error.with_column(column);
}
error
}
/// Checks if we should add more errors.
pub(crate) fn should_add_error(&self, errors: &[StructuredError]) -> bool {
self.max_errors == 0 || errors.len() < self.max_errors
}
}