Commit 5dcf0b4
committed
feat: add document-scoped format validators to prevent global state pollution
Add support for per-document format validators that are scoped to individual
OpenAPI specs instead of being shared globally. This solves the problem where
multiple specs in the same application cannot have different validation rules
for the same format name.
Problem:
DefineStringFormatValidator() uses global maps, causing issues when:
- Multiple OpenAPI specs are loaded in the same application
- Different specs need different validation logic for the same format name
- One spec's validator registration overwrites another's globally
Solution:
- Add format validator maps to openapi3.T (document-level storage)
- Add SetStringFormatValidator(), SetNumberFormatValidator(), and
SetIntegerFormatValidator() methods to openapi3.T
- Add ValidateSchemaJSON() convenience method that automatically applies
the document's validators
- Update schema validation logic to check: per-validation validators →
document-scoped validators → global validators
Usage:
specA := loader.LoadFromFile("spec-a.yaml")
specA.SetStringFormatValidator("custom-id", validatorA)
specB := loader.LoadFromFile("spec-b.yaml")
specB.SetStringFormatValidator("custom-id", validatorB)
// Each spec uses its own validators - no conflicts!
err := specA.ValidateSchemaJSON(schemaA, value)
err := specB.ValidateSchemaJSON(schemaB, value)
Changes:
- openapi3/openapi3.go: Add validator fields and methods to T
- openapi3/schema_validation_settings.go: Add per-validation validator options
- openapi3/schema.go: Update validation logic to check all validator sources
- openapi3/schema_formats_test.go: Add comprehensive tests
This change is fully backwards compatible. Existing code using global
validators continues to work unchanged.1 parent 45db2ad commit 5dcf0b4
File tree
4 files changed
+457
-3
lines changed- openapi3
4 files changed
+457
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
29 | 35 | | |
30 | 36 | | |
31 | 37 | | |
| |||
137 | 143 | | |
138 | 144 | | |
139 | 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 | + | |
140 | 204 | | |
141 | 205 | | |
142 | 206 | | |
| |||
203 | 267 | | |
204 | 268 | | |
205 | 269 | | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1527 | 1527 | | |
1528 | 1528 | | |
1529 | 1529 | | |
1530 | | - | |
| 1530 | + | |
| 1531 | + | |
| 1532 | + | |
| 1533 | + | |
| 1534 | + | |
| 1535 | + | |
1531 | 1536 | | |
1532 | 1537 | | |
1533 | 1538 | | |
| |||
1541 | 1546 | | |
1542 | 1547 | | |
1543 | 1548 | | |
1544 | | - | |
| 1549 | + | |
| 1550 | + | |
| 1551 | + | |
| 1552 | + | |
| 1553 | + | |
| 1554 | + | |
1545 | 1555 | | |
1546 | 1556 | | |
1547 | 1557 | | |
| |||
1767 | 1777 | | |
1768 | 1778 | | |
1769 | 1779 | | |
1770 | | - | |
| 1780 | + | |
| 1781 | + | |
| 1782 | + | |
| 1783 | + | |
| 1784 | + | |
| 1785 | + | |
1771 | 1786 | | |
1772 | 1787 | | |
1773 | 1788 | | |
| |||
0 commit comments