structa-maestro 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- structa_maestro/__init__.py +294 -0
- structa_maestro/batch.py +564 -0
- structa_maestro/binding.py +206 -0
- structa_maestro/builder/__init__.py +0 -0
- structa_maestro/builder/result_builder.py +308 -0
- structa_maestro/cli.py +730 -0
- structa_maestro/confidence/__init__.py +49 -0
- structa_maestro/confidence/calibration.py +990 -0
- structa_maestro/confidence/normalizer.py +203 -0
- structa_maestro/config.py +111 -0
- structa_maestro/contracts/__init__.py +0 -0
- structa_maestro/contracts/enums.py +86 -0
- structa_maestro/contracts/models.py +229 -0
- structa_maestro/contracts/serialization.py +169 -0
- structa_maestro/documents/__init__.py +32 -0
- structa_maestro/documents/docx_parser.py +215 -0
- structa_maestro/documents/eml_parser.py +360 -0
- structa_maestro/documents/html_parser.py +337 -0
- structa_maestro/documents/inputs.py +277 -0
- structa_maestro/documents/loader.py +675 -0
- structa_maestro/documents/markdown_parser.py +225 -0
- structa_maestro/documents/models.py +33 -0
- structa_maestro/documents/normalization.py +24 -0
- structa_maestro/documents/pdf_parser.py +186 -0
- structa_maestro/documents/txt_parser.py +169 -0
- structa_maestro/engine.py +1201 -0
- structa_maestro/exceptions.py +38 -0
- structa_maestro/provenance/__init__.py +0 -0
- structa_maestro/provenance/associator.py +234 -0
- structa_maestro/py.typed +0 -0
- structa_maestro/review.py +1234 -0
- structa_maestro/schema/__init__.py +19 -0
- structa_maestro/schema/loader.py +616 -0
- structa_maestro/schema/models.py +269 -0
- structa_maestro/schema/pydantic_adapter.py +566 -0
- structa_maestro/schema/rules.py +526 -0
- structa_maestro/schema/serializer.py +156 -0
- structa_maestro/strategies/__init__.py +45 -0
- structa_maestro/strategies/base.py +101 -0
- structa_maestro/strategies/llm_anthropic.py +1142 -0
- structa_maestro/strategies/llm_config.py +154 -0
- structa_maestro/strategies/llm_gemini.py +1171 -0
- structa_maestro/strategies/llm_helpers.py +240 -0
- structa_maestro/strategies/llm_openai_compat.py +1183 -0
- structa_maestro/strategies/schema_compiler.py +248 -0
- structa_maestro/strategies/testing.py +71 -0
- structa_maestro/telemetry.py +800 -0
- structa_maestro/validation/__init__.py +0 -0
- structa_maestro/validation/cross_field.py +463 -0
- structa_maestro/validation/validator.py +531 -0
- structa_maestro-0.2.0.dist-info/METADATA +394 -0
- structa_maestro-0.2.0.dist-info/RECORD +55 -0
- structa_maestro-0.2.0.dist-info/WHEEL +4 -0
- structa_maestro-0.2.0.dist-info/entry_points.txt +2 -0
- structa_maestro-0.2.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
"""Dedicated non-coercing deterministic schema validator (FR-024..FR-026, Clarification Q4)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from collections.abc import Mapping
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from datetime import date, datetime
|
|
9
|
+
from decimal import Decimal
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
from structa_maestro.contracts.enums import (
|
|
13
|
+
ExtractionCardinality,
|
|
14
|
+
FieldPresence,
|
|
15
|
+
FieldType,
|
|
16
|
+
IssueSeverity,
|
|
17
|
+
PipelineStage,
|
|
18
|
+
ValidationStatus,
|
|
19
|
+
)
|
|
20
|
+
from structa_maestro.contracts.models import DiagnosticIssue, ValidationOutcome
|
|
21
|
+
from structa_maestro.schema.models import ExtractionSchema, FieldSpec
|
|
22
|
+
from structa_maestro.validation.cross_field import evaluate_cross_field_rules
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass(frozen=True, slots=True)
|
|
26
|
+
class RecordValidationResult:
|
|
27
|
+
"""Deterministic validation outcomes for one record and each of its fields."""
|
|
28
|
+
|
|
29
|
+
record_outcome: ValidationOutcome
|
|
30
|
+
field_outcomes: dict[str, ValidationOutcome]
|
|
31
|
+
warnings: tuple[DiagnosticIssue, ...] = ()
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _make_error(
|
|
35
|
+
*,
|
|
36
|
+
code: str,
|
|
37
|
+
message: str,
|
|
38
|
+
record_id: str | None,
|
|
39
|
+
field_path: str | None,
|
|
40
|
+
expected: str | None = None,
|
|
41
|
+
actual: str | None = None,
|
|
42
|
+
) -> DiagnosticIssue:
|
|
43
|
+
return DiagnosticIssue(
|
|
44
|
+
code=code,
|
|
45
|
+
severity=IssueSeverity.ERROR,
|
|
46
|
+
stage=PipelineStage.VALIDATION,
|
|
47
|
+
message=message,
|
|
48
|
+
record_id=record_id,
|
|
49
|
+
field_path=field_path,
|
|
50
|
+
expected=expected,
|
|
51
|
+
actual=actual,
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _safe_type_name(value: Any) -> str:
|
|
56
|
+
if value is None:
|
|
57
|
+
return "null"
|
|
58
|
+
if isinstance(value, bool):
|
|
59
|
+
return "boolean"
|
|
60
|
+
if isinstance(value, int):
|
|
61
|
+
return "integer"
|
|
62
|
+
if isinstance(value, Decimal):
|
|
63
|
+
return "decimal"
|
|
64
|
+
if isinstance(value, float):
|
|
65
|
+
return "float"
|
|
66
|
+
if isinstance(value, datetime):
|
|
67
|
+
return "datetime"
|
|
68
|
+
if isinstance(value, date):
|
|
69
|
+
return "date"
|
|
70
|
+
if isinstance(value, str):
|
|
71
|
+
return "string"
|
|
72
|
+
if isinstance(value, list):
|
|
73
|
+
return "array"
|
|
74
|
+
if isinstance(value, dict):
|
|
75
|
+
return "object"
|
|
76
|
+
return type(value).__name__
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class DeterministicValidator:
|
|
80
|
+
"""Evaluates extracted records strictly against ExtractionSchema with zero value coercion."""
|
|
81
|
+
|
|
82
|
+
def validate_record(
|
|
83
|
+
self,
|
|
84
|
+
fields: Mapping[str, tuple[FieldPresence, Any]],
|
|
85
|
+
schema: ExtractionSchema,
|
|
86
|
+
*,
|
|
87
|
+
record_id: str,
|
|
88
|
+
) -> RecordValidationResult:
|
|
89
|
+
"""Deterministically validate one record's fields against schema."""
|
|
90
|
+
field_outcomes: dict[str, ValidationOutcome] = {}
|
|
91
|
+
all_errors: list[DiagnosticIssue] = []
|
|
92
|
+
|
|
93
|
+
# 1. Validate every declared schema field in deterministic order
|
|
94
|
+
for field_name, spec in schema.fields.items():
|
|
95
|
+
presence, value = fields.get(field_name, (FieldPresence.MISSING, None))
|
|
96
|
+
errs = self._validate_value(
|
|
97
|
+
spec=spec,
|
|
98
|
+
presence=presence,
|
|
99
|
+
value=value,
|
|
100
|
+
record_id=record_id,
|
|
101
|
+
field_path=field_name,
|
|
102
|
+
)
|
|
103
|
+
outcome = ValidationOutcome(
|
|
104
|
+
status=ValidationStatus.INVALID if errs else ValidationStatus.VALID,
|
|
105
|
+
errors=errs,
|
|
106
|
+
)
|
|
107
|
+
field_outcomes[field_name] = outcome
|
|
108
|
+
all_errors.extend(errs)
|
|
109
|
+
|
|
110
|
+
# 2. Check unexpected extra fields (rejected by default per Clarification Q4)
|
|
111
|
+
for extra_name in sorted(set(fields.keys()) - set(schema.fields.keys())):
|
|
112
|
+
presence, _ = fields[extra_name]
|
|
113
|
+
if presence == FieldPresence.MISSING:
|
|
114
|
+
field_outcomes[extra_name] = ValidationOutcome(status=ValidationStatus.VALID)
|
|
115
|
+
continue
|
|
116
|
+
if not schema.allow_additional_fields:
|
|
117
|
+
err = _make_error(
|
|
118
|
+
code="unexpected_field",
|
|
119
|
+
message=f"Unexpected field '{extra_name}' is not declared in schema.",
|
|
120
|
+
record_id=record_id,
|
|
121
|
+
field_path=extra_name,
|
|
122
|
+
expected="declared schema field only",
|
|
123
|
+
actual=extra_name,
|
|
124
|
+
)
|
|
125
|
+
field_outcomes[extra_name] = ValidationOutcome(
|
|
126
|
+
status=ValidationStatus.INVALID,
|
|
127
|
+
errors=[err],
|
|
128
|
+
)
|
|
129
|
+
all_errors.append(err)
|
|
130
|
+
else:
|
|
131
|
+
field_outcomes[extra_name] = ValidationOutcome(status=ValidationStatus.VALID)
|
|
132
|
+
|
|
133
|
+
# 3. Evaluate declarative cross-field & conditional rules (record-level only per Spec 004)
|
|
134
|
+
rule_warnings: list[DiagnosticIssue] = []
|
|
135
|
+
if schema.rules:
|
|
136
|
+
cf_errors, cf_warnings = evaluate_cross_field_rules(
|
|
137
|
+
fields,
|
|
138
|
+
schema,
|
|
139
|
+
record_id=record_id,
|
|
140
|
+
)
|
|
141
|
+
all_errors.extend(cf_errors)
|
|
142
|
+
rule_warnings.extend(cf_warnings)
|
|
143
|
+
|
|
144
|
+
record_outcome = ValidationOutcome(
|
|
145
|
+
status=ValidationStatus.INVALID if all_errors else ValidationStatus.VALID,
|
|
146
|
+
errors=all_errors,
|
|
147
|
+
)
|
|
148
|
+
return RecordValidationResult(
|
|
149
|
+
record_outcome=record_outcome,
|
|
150
|
+
field_outcomes=field_outcomes,
|
|
151
|
+
warnings=tuple(rule_warnings),
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
def validate_result_cardinality(
|
|
155
|
+
self,
|
|
156
|
+
record_outcomes: list[ValidationOutcome],
|
|
157
|
+
schema: ExtractionSchema,
|
|
158
|
+
*,
|
|
159
|
+
has_records: bool,
|
|
160
|
+
failed_before_validation: bool,
|
|
161
|
+
) -> ValidationOutcome:
|
|
162
|
+
"""Aggregate record-level validation outcomes and check schema cardinality rules."""
|
|
163
|
+
if failed_before_validation and not has_records:
|
|
164
|
+
return ValidationOutcome(status=ValidationStatus.NOT_VALIDATED)
|
|
165
|
+
|
|
166
|
+
top_errors: list[DiagnosticIssue] = []
|
|
167
|
+
if schema.cardinality == ExtractionCardinality.SINGLE:
|
|
168
|
+
if len(record_outcomes) == 0:
|
|
169
|
+
top_errors.append(
|
|
170
|
+
_make_error(
|
|
171
|
+
code="missing_record",
|
|
172
|
+
message=(
|
|
173
|
+
"Single-record schema expected 1 record, but 0 records were extracted."
|
|
174
|
+
),
|
|
175
|
+
record_id=None,
|
|
176
|
+
field_path=None,
|
|
177
|
+
expected="1 record",
|
|
178
|
+
actual="0 records",
|
|
179
|
+
)
|
|
180
|
+
)
|
|
181
|
+
elif len(record_outcomes) > 1:
|
|
182
|
+
top_errors.append(
|
|
183
|
+
_make_error(
|
|
184
|
+
code="cardinality_violation",
|
|
185
|
+
message=(
|
|
186
|
+
f"Single-record schema expected 1 record, "
|
|
187
|
+
f"but {len(record_outcomes)} records were extracted."
|
|
188
|
+
),
|
|
189
|
+
record_id=None,
|
|
190
|
+
field_path=None,
|
|
191
|
+
expected="1 record",
|
|
192
|
+
actual=f"{len(record_outcomes)} records",
|
|
193
|
+
)
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
for rec_outcome in record_outcomes:
|
|
197
|
+
top_errors.extend(rec_outcome.errors)
|
|
198
|
+
|
|
199
|
+
return ValidationOutcome(
|
|
200
|
+
status=ValidationStatus.INVALID if top_errors else ValidationStatus.VALID,
|
|
201
|
+
errors=top_errors,
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
def _validate_value(
|
|
205
|
+
self,
|
|
206
|
+
*,
|
|
207
|
+
spec: FieldSpec,
|
|
208
|
+
presence: FieldPresence,
|
|
209
|
+
value: Any,
|
|
210
|
+
record_id: str,
|
|
211
|
+
field_path: str,
|
|
212
|
+
) -> list[DiagnosticIssue]:
|
|
213
|
+
if presence == FieldPresence.MISSING:
|
|
214
|
+
if spec.required:
|
|
215
|
+
return [
|
|
216
|
+
_make_error(
|
|
217
|
+
code="missing_required",
|
|
218
|
+
message=f"Required field '{field_path}' is missing.",
|
|
219
|
+
record_id=record_id,
|
|
220
|
+
field_path=field_path,
|
|
221
|
+
expected=f"required {spec.field_type.value}",
|
|
222
|
+
actual="missing",
|
|
223
|
+
)
|
|
224
|
+
]
|
|
225
|
+
return []
|
|
226
|
+
|
|
227
|
+
if value is None:
|
|
228
|
+
if spec.nullable:
|
|
229
|
+
return []
|
|
230
|
+
return [
|
|
231
|
+
_make_error(
|
|
232
|
+
code="disallowed_null",
|
|
233
|
+
message=f"Field '{field_path}' does not allow null values.",
|
|
234
|
+
record_id=record_id,
|
|
235
|
+
field_path=field_path,
|
|
236
|
+
expected=f"non-null {spec.field_type.value}",
|
|
237
|
+
actual="null",
|
|
238
|
+
)
|
|
239
|
+
]
|
|
240
|
+
|
|
241
|
+
errors: list[DiagnosticIssue] = []
|
|
242
|
+
ft = spec.field_type
|
|
243
|
+
c = spec.constraints
|
|
244
|
+
|
|
245
|
+
# Strict type checks (NO silent coercion!)
|
|
246
|
+
if ft == FieldType.STRING:
|
|
247
|
+
if not isinstance(value, str):
|
|
248
|
+
return [
|
|
249
|
+
_make_error(
|
|
250
|
+
code="type_mismatch",
|
|
251
|
+
message=(
|
|
252
|
+
f"Field '{field_path}' expected string, got {_safe_type_name(value)}."
|
|
253
|
+
),
|
|
254
|
+
record_id=record_id,
|
|
255
|
+
field_path=field_path,
|
|
256
|
+
expected="string",
|
|
257
|
+
actual=_safe_type_name(value),
|
|
258
|
+
)
|
|
259
|
+
]
|
|
260
|
+
if c.min_length is not None and len(value) < c.min_length:
|
|
261
|
+
errors.append(
|
|
262
|
+
_make_error(
|
|
263
|
+
code="constraint_violation",
|
|
264
|
+
message=(
|
|
265
|
+
f"Field '{field_path}' length is below min_length ({c.min_length})."
|
|
266
|
+
),
|
|
267
|
+
record_id=record_id,
|
|
268
|
+
field_path=field_path,
|
|
269
|
+
expected=f"length >= {c.min_length}",
|
|
270
|
+
actual=f"length {len(value)}",
|
|
271
|
+
)
|
|
272
|
+
)
|
|
273
|
+
if c.max_length is not None and len(value) > c.max_length:
|
|
274
|
+
errors.append(
|
|
275
|
+
_make_error(
|
|
276
|
+
code="constraint_violation",
|
|
277
|
+
message=(
|
|
278
|
+
f"Field '{field_path}' length exceeds max_length ({c.max_length})."
|
|
279
|
+
),
|
|
280
|
+
record_id=record_id,
|
|
281
|
+
field_path=field_path,
|
|
282
|
+
expected=f"length <= {c.max_length}",
|
|
283
|
+
actual=f"length {len(value)}",
|
|
284
|
+
)
|
|
285
|
+
)
|
|
286
|
+
if c.pattern is not None and re.search(c.pattern, value) is None:
|
|
287
|
+
errors.append(
|
|
288
|
+
_make_error(
|
|
289
|
+
code="constraint_violation",
|
|
290
|
+
message=f"Field '{field_path}' does not match required pattern.",
|
|
291
|
+
record_id=record_id,
|
|
292
|
+
field_path=field_path,
|
|
293
|
+
expected=f"pattern {c.pattern}",
|
|
294
|
+
actual="pattern mismatch",
|
|
295
|
+
)
|
|
296
|
+
)
|
|
297
|
+
|
|
298
|
+
elif ft == FieldType.INTEGER:
|
|
299
|
+
if isinstance(value, bool) or not isinstance(value, int):
|
|
300
|
+
return [
|
|
301
|
+
_make_error(
|
|
302
|
+
code="type_mismatch",
|
|
303
|
+
message=(
|
|
304
|
+
f"Field '{field_path}' expected integer, got {_safe_type_name(value)}."
|
|
305
|
+
),
|
|
306
|
+
record_id=record_id,
|
|
307
|
+
field_path=field_path,
|
|
308
|
+
expected="integer",
|
|
309
|
+
actual=_safe_type_name(value),
|
|
310
|
+
)
|
|
311
|
+
]
|
|
312
|
+
if c.min_value is not None and value < c.min_value:
|
|
313
|
+
errors.append(
|
|
314
|
+
_make_error(
|
|
315
|
+
code="constraint_violation",
|
|
316
|
+
message=f"Field '{field_path}' is below min_value ({c.min_value}).",
|
|
317
|
+
record_id=record_id,
|
|
318
|
+
field_path=field_path,
|
|
319
|
+
expected=f">= {c.min_value}",
|
|
320
|
+
actual="below minimum",
|
|
321
|
+
)
|
|
322
|
+
)
|
|
323
|
+
if c.max_value is not None and value > c.max_value:
|
|
324
|
+
errors.append(
|
|
325
|
+
_make_error(
|
|
326
|
+
code="constraint_violation",
|
|
327
|
+
message=f"Field '{field_path}' exceeds max_value ({c.max_value}).",
|
|
328
|
+
record_id=record_id,
|
|
329
|
+
field_path=field_path,
|
|
330
|
+
expected=f"<= {c.max_value}",
|
|
331
|
+
actual="above maximum",
|
|
332
|
+
)
|
|
333
|
+
)
|
|
334
|
+
|
|
335
|
+
elif ft == FieldType.DECIMAL:
|
|
336
|
+
if isinstance(value, bool) or not isinstance(value, (Decimal, int, float)):
|
|
337
|
+
return [
|
|
338
|
+
_make_error(
|
|
339
|
+
code="type_mismatch",
|
|
340
|
+
message=(
|
|
341
|
+
f"Field '{field_path}' expected decimal, got {_safe_type_name(value)}."
|
|
342
|
+
),
|
|
343
|
+
record_id=record_id,
|
|
344
|
+
field_path=field_path,
|
|
345
|
+
expected="decimal",
|
|
346
|
+
actual=_safe_type_name(value),
|
|
347
|
+
)
|
|
348
|
+
]
|
|
349
|
+
dec_val = value if isinstance(value, Decimal) else Decimal(str(value))
|
|
350
|
+
if dec_val.is_nan() or dec_val.is_infinite():
|
|
351
|
+
return [
|
|
352
|
+
_make_error(
|
|
353
|
+
code="type_mismatch",
|
|
354
|
+
message=f"Field '{field_path}' must be a finite decimal value.",
|
|
355
|
+
record_id=record_id,
|
|
356
|
+
field_path=field_path,
|
|
357
|
+
expected="finite decimal",
|
|
358
|
+
actual="non-finite",
|
|
359
|
+
)
|
|
360
|
+
]
|
|
361
|
+
if c.min_value is not None and dec_val < Decimal(str(c.min_value)):
|
|
362
|
+
errors.append(
|
|
363
|
+
_make_error(
|
|
364
|
+
code="constraint_violation",
|
|
365
|
+
message=f"Field '{field_path}' is below min_value ({c.min_value}).",
|
|
366
|
+
record_id=record_id,
|
|
367
|
+
field_path=field_path,
|
|
368
|
+
expected=f">= {c.min_value}",
|
|
369
|
+
actual="below minimum",
|
|
370
|
+
)
|
|
371
|
+
)
|
|
372
|
+
if c.max_value is not None and dec_val > Decimal(str(c.max_value)):
|
|
373
|
+
errors.append(
|
|
374
|
+
_make_error(
|
|
375
|
+
code="constraint_violation",
|
|
376
|
+
message=f"Field '{field_path}' exceeds max_value ({c.max_value}).",
|
|
377
|
+
record_id=record_id,
|
|
378
|
+
field_path=field_path,
|
|
379
|
+
expected=f"<= {c.max_value}",
|
|
380
|
+
actual="above maximum",
|
|
381
|
+
)
|
|
382
|
+
)
|
|
383
|
+
|
|
384
|
+
elif ft == FieldType.BOOLEAN:
|
|
385
|
+
if not isinstance(value, bool):
|
|
386
|
+
return [
|
|
387
|
+
_make_error(
|
|
388
|
+
code="type_mismatch",
|
|
389
|
+
message=(
|
|
390
|
+
f"Field '{field_path}' expected boolean, got {_safe_type_name(value)}."
|
|
391
|
+
),
|
|
392
|
+
record_id=record_id,
|
|
393
|
+
field_path=field_path,
|
|
394
|
+
expected="boolean",
|
|
395
|
+
actual=_safe_type_name(value),
|
|
396
|
+
)
|
|
397
|
+
]
|
|
398
|
+
|
|
399
|
+
elif ft == FieldType.DATE:
|
|
400
|
+
if isinstance(value, datetime) or not isinstance(value, date):
|
|
401
|
+
return [
|
|
402
|
+
_make_error(
|
|
403
|
+
code="type_mismatch",
|
|
404
|
+
message=(
|
|
405
|
+
f"Field '{field_path}' expected date, got {_safe_type_name(value)}."
|
|
406
|
+
),
|
|
407
|
+
record_id=record_id,
|
|
408
|
+
field_path=field_path,
|
|
409
|
+
expected="date",
|
|
410
|
+
actual=_safe_type_name(value),
|
|
411
|
+
)
|
|
412
|
+
]
|
|
413
|
+
|
|
414
|
+
elif ft == FieldType.ARRAY:
|
|
415
|
+
if not isinstance(value, list):
|
|
416
|
+
return [
|
|
417
|
+
_make_error(
|
|
418
|
+
code="type_mismatch",
|
|
419
|
+
message=(
|
|
420
|
+
f"Field '{field_path}' expected array, got {_safe_type_name(value)}."
|
|
421
|
+
),
|
|
422
|
+
record_id=record_id,
|
|
423
|
+
field_path=field_path,
|
|
424
|
+
expected="array",
|
|
425
|
+
actual=_safe_type_name(value),
|
|
426
|
+
)
|
|
427
|
+
]
|
|
428
|
+
if c.min_items is not None and len(value) < c.min_items:
|
|
429
|
+
errors.append(
|
|
430
|
+
_make_error(
|
|
431
|
+
code="constraint_violation",
|
|
432
|
+
message=f"Field '{field_path}' has fewer than min_items ({c.min_items}).",
|
|
433
|
+
record_id=record_id,
|
|
434
|
+
field_path=field_path,
|
|
435
|
+
expected=f"items >= {c.min_items}",
|
|
436
|
+
actual=f"{len(value)} items",
|
|
437
|
+
)
|
|
438
|
+
)
|
|
439
|
+
if c.max_items is not None and len(value) > c.max_items:
|
|
440
|
+
errors.append(
|
|
441
|
+
_make_error(
|
|
442
|
+
code="constraint_violation",
|
|
443
|
+
message=f"Field '{field_path}' exceeds max_items ({c.max_items}).",
|
|
444
|
+
record_id=record_id,
|
|
445
|
+
field_path=field_path,
|
|
446
|
+
expected=f"items <= {c.max_items}",
|
|
447
|
+
actual=f"{len(value)} items",
|
|
448
|
+
)
|
|
449
|
+
)
|
|
450
|
+
if spec.items is not None:
|
|
451
|
+
for idx, elem in enumerate(value):
|
|
452
|
+
errors.extend(
|
|
453
|
+
self._validate_value(
|
|
454
|
+
spec=spec.items,
|
|
455
|
+
presence=FieldPresence.PRESENT,
|
|
456
|
+
value=elem,
|
|
457
|
+
record_id=record_id,
|
|
458
|
+
field_path=f"{field_path}[{idx}]",
|
|
459
|
+
)
|
|
460
|
+
)
|
|
461
|
+
|
|
462
|
+
elif ft == FieldType.OBJECT:
|
|
463
|
+
if not isinstance(value, dict):
|
|
464
|
+
return [
|
|
465
|
+
_make_error(
|
|
466
|
+
code="type_mismatch",
|
|
467
|
+
message=(
|
|
468
|
+
f"Field '{field_path}' expected object, got {_safe_type_name(value)}."
|
|
469
|
+
),
|
|
470
|
+
record_id=record_id,
|
|
471
|
+
field_path=field_path,
|
|
472
|
+
expected="object",
|
|
473
|
+
actual=_safe_type_name(value),
|
|
474
|
+
)
|
|
475
|
+
]
|
|
476
|
+
props = spec.properties or {}
|
|
477
|
+
for prop_name, prop_spec in props.items():
|
|
478
|
+
if prop_name in value:
|
|
479
|
+
p_presence = FieldPresence.PRESENT
|
|
480
|
+
p_val = value[prop_name]
|
|
481
|
+
else:
|
|
482
|
+
p_presence = FieldPresence.MISSING
|
|
483
|
+
p_val = None
|
|
484
|
+
errors.extend(
|
|
485
|
+
self._validate_value(
|
|
486
|
+
spec=prop_spec,
|
|
487
|
+
presence=p_presence,
|
|
488
|
+
value=p_val,
|
|
489
|
+
record_id=record_id,
|
|
490
|
+
field_path=f"{field_path}.{prop_name}",
|
|
491
|
+
)
|
|
492
|
+
)
|
|
493
|
+
if not spec.allow_additional_fields:
|
|
494
|
+
for extra_key in sorted(set(value.keys()) - set(props.keys())):
|
|
495
|
+
errors.append(
|
|
496
|
+
_make_error(
|
|
497
|
+
code="unexpected_field",
|
|
498
|
+
message=(
|
|
499
|
+
f"Unexpected nested field '{field_path}.{extra_key}' "
|
|
500
|
+
"is not declared in schema."
|
|
501
|
+
),
|
|
502
|
+
record_id=record_id,
|
|
503
|
+
field_path=f"{field_path}.{extra_key}",
|
|
504
|
+
expected="declared nested property only",
|
|
505
|
+
actual=str(extra_key),
|
|
506
|
+
)
|
|
507
|
+
)
|
|
508
|
+
|
|
509
|
+
# Allowed values (enum) check
|
|
510
|
+
if c.allowed_values is not None and not _value_in_allowed(value, c.allowed_values):
|
|
511
|
+
errors.append(
|
|
512
|
+
_make_error(
|
|
513
|
+
code="constraint_violation",
|
|
514
|
+
message=f"Field '{field_path}' value is not in allowed_values.",
|
|
515
|
+
record_id=record_id,
|
|
516
|
+
field_path=field_path,
|
|
517
|
+
expected=f"one of {list(c.allowed_values)}",
|
|
518
|
+
actual="disallowed value",
|
|
519
|
+
)
|
|
520
|
+
)
|
|
521
|
+
|
|
522
|
+
return errors
|
|
523
|
+
|
|
524
|
+
|
|
525
|
+
def _value_in_allowed(value: Any, allowed: tuple[Any, ...]) -> bool:
|
|
526
|
+
for candidate in allowed:
|
|
527
|
+
if isinstance(value, bool) != isinstance(candidate, bool):
|
|
528
|
+
continue
|
|
529
|
+
if value == candidate:
|
|
530
|
+
return True
|
|
531
|
+
return False
|