Debian Codegraph @

strict/schema_walker.go

raw
1 // Copyright 2023-2025 Princess Beef Heavy Industries, LLC / Dave Shanley
2 // SPDX-License-Identifier: MIT
3
4 package strict
5
6 import (
7 "github.com/pb33f/libopenapi/datamodel/high/base"
8 )
9
10 // validateValue is the main entry point for validating a value against a schema.
11 // It dispatches to the appropriate handler based on the value type.
12 func (v *Validator) validateValue(ctx *traversalContext, schema *base.Schema, data any) []UndeclaredValue {
13 if schema == nil || data == nil {
14 return nil
15 }
16
17 if ctx.shouldIgnore() {
18 return nil
19 }
20
21 if ctx.exceedsDepth() {
22 return nil
23 }
24
25 // check for cycles using schema hash
26 schemaKey := v.getSchemaKey(schema)
27 if ctx.checkAndMarkVisited(schemaKey) {
28 return nil
29 }
30
31 // switch on data type
32 switch val := data.(type) {
33 case map[string]any:
34 return v.validateObject(ctx, schema, val)
35 case []any:
36 return v.validateArray(ctx, schema, val)
37 default:
38 return nil
39 }
40 }
41
42 // validateObject checks an object value against a schema for undeclared properties.
43 func (v *Validator) validateObject(ctx *traversalContext, schema *base.Schema, data map[string]any) []UndeclaredValue {
44 var undeclared []UndeclaredValue
45
46 if len(schema.AllOf) > 0 || len(schema.OneOf) > 0 || len(schema.AnyOf) > 0 {
47 return v.validatePolymorphic(ctx, schema, data)
48 }
49
50 if !v.shouldReportUndeclared(schema) {
51 // additionalProperties: false - base validation catches this, no strict check needed
52 // Still need to recurse into declared properties
53 return v.recurseIntoDeclaredProperties(ctx, schema, data)
54 }
55
56 declared, patterns := v.collectDeclaredProperties(schema, data)
57
58 // check each property in the data
59 for propName, propValue := range data {
60 propPath := buildPath(ctx.path, propName)
61 propCtx := ctx.withPath(propPath)
62
63 if propCtx.shouldIgnore() {
64 continue
65 }
66
67 if !isPropertyDeclared(propName, declared, patterns) {
68 undeclared = append(undeclared,
69 newUndeclaredProperty(propPath, propName, propValue, getDeclaredPropertyNames(declared), ctx.direction, schema))
70
71 // even if undeclared, recurse into additionalProperties schema if present
72 if schema.AdditionalProperties != nil && schema.AdditionalProperties.IsA() {
73 addlProxy := schema.AdditionalProperties.A
74 if addlProxy != nil {
75 addlSchema := addlProxy.Schema()
76 if addlSchema != nil {
77 undeclared = append(undeclared, v.validateValue(propCtx, addlSchema, propValue)...)
78 }
79 }
80 }
81 continue
82 }
83
84 // property is declared, recurse into it
85 propProxy := getPropertySchema(propName, declared)
86 if propProxy == nil {
87 propProxy = v.getPatternPropertySchema(schema, propName)
88 }
89
90 if propProxy != nil {
91 propSchema := propProxy.Schema()
92 if propSchema != nil {
93 if violation, ok := v.checkReadWriteOnlyViolation(propPath, propName, propValue, propSchema, ctx.direction); ok {
94 undeclared = append(undeclared, violation)
95 continue
96 }
97 if v.shouldSkipProperty(propSchema, ctx.direction) {
98 continue
99 }
100 undeclared = append(undeclared, v.validateValue(propCtx, propSchema, propValue)...)
101 }
102 }
103 }
104
105 return undeclared
106 }
107
108 // shouldReportUndeclared determines if strict mode should report undeclared
109 // properties for this schema.
110 func (v *Validator) shouldReportUndeclared(schema *base.Schema) bool {
111 if schema == nil {
112 return false
113 }
114
115 // SHORT-CIRCUIT: If additionalProperties: false, base validation already catches extras.
116 if schema.AdditionalProperties != nil && schema.AdditionalProperties.IsB() && !schema.AdditionalProperties.B {
117 return false
118 }
119
120 // STRICT OVERRIDE: Even if additionalProperties: true, report undeclared.
121 if schema.AdditionalProperties != nil {
122 if schema.AdditionalProperties.IsB() && schema.AdditionalProperties.B {
123 return true
124 }
125 if schema.AdditionalProperties.IsA() {
126 // additionalProperties with schema - properties matching schema are
127 // technically "declared" but we still want to flag them as not in
128 // the explicit schema. They will be recursed into.
129 return true
130 }
131 }
132
133 // STRICT OVERRIDE: unevaluatedProperties: false with implicit additionalProperties: true
134 // Standard JSON Schema would catch via unevaluatedProperties, but strict reports
135 // even when additionalProperties: true would normally allow extras.
136 if schema.UnevaluatedProperties != nil && schema.UnevaluatedProperties.IsB() && !schema.UnevaluatedProperties.B {
137 // unevaluatedProperties: false means base validation catches extras
138 // BUT if there's no additionalProperties: false, strict should report
139 return true
140 }
141
142 // default: no additionalProperties means implicit true in JSON Schema
143 // Strict reports undeclared in this case
144 return true
145 }
146
147 // getPatternPropertySchema finds the schema for a property that matches
148 // a patternProperties regex. Uses cached compiled patterns.
149 func (v *Validator) getPatternPropertySchema(schema *base.Schema, propName string) *base.SchemaProxy {
150 if schema.PatternProperties == nil {
151 return nil
152 }
153
154 for pair := schema.PatternProperties.First(); pair != nil; pair = pair.Next() {
155 pattern := v.getCompiledPattern(pair.Key())
156 if pattern == nil {
157 continue
158 }
159 if pattern.MatchString(propName) {
160 return pair.Value()
161 }
162 }
163
164 return nil
165 }
166
167 // recurseIntoDeclaredProperties recurses into declared properties without
168 // checking for undeclared (used when additionalProperties: false).
169 // This includes both explicit properties and patternProperties matches.
170 func (v *Validator) recurseIntoDeclaredProperties(ctx *traversalContext, schema *base.Schema, data map[string]any) []UndeclaredValue {
171 var undeclared []UndeclaredValue
172
173 processed := make(map[string]bool)
174
175 // process explicit properties
176 if schema.Properties != nil {
177 for pair := schema.Properties.First(); pair != nil; pair = pair.Next() {
178 propName := pair.Key()
179 propProxy := pair.Value()
180
181 propValue, exists := data[propName]
182 if !exists {
183 continue
184 }
185
186 processed[propName] = true
187
188 propPath := buildPath(ctx.path, propName)
189 propCtx := ctx.withPath(propPath)
190
191 if propCtx.shouldIgnore() {
192 continue
193 }
194
195 propSchema := propProxy.Schema()
196 if propSchema != nil {
197 if violation, ok := v.checkReadWriteOnlyViolation(propPath, propName, propValue, propSchema, ctx.direction); ok {
198 undeclared = append(undeclared, violation)
199 continue
200 }
201 if v.shouldSkipProperty(propSchema, ctx.direction) {
202 continue
203 }
204 undeclared = append(undeclared, v.validateValue(propCtx, propSchema, propValue)...)
205 }
206 }
207 }
208
209 // process patternProperties - recurse into any data properties that match patterns
210 if schema.PatternProperties != nil {
211 for propName, propValue := range data {
212 if processed[propName] {
213 continue
214 }
215
216 propProxy := v.getPatternPropertySchema(schema, propName)
217 if propProxy == nil {
218 continue
219 }
220
221 processed[propName] = true
222
223 propPath := buildPath(ctx.path, propName)
224 propCtx := ctx.withPath(propPath)
225
226 if propCtx.shouldIgnore() {
227 continue
228 }
229
230 propSchema := propProxy.Schema()
231 if propSchema != nil {
232 if violation, ok := v.checkReadWriteOnlyViolation(propPath, propName, propValue, propSchema, ctx.direction); ok {
233 undeclared = append(undeclared, violation)
234 continue
235 }
236 if v.shouldSkipProperty(propSchema, ctx.direction) {
237 continue
238 }
239 undeclared = append(undeclared, v.validateValue(propCtx, propSchema, propValue)...)
240 }
241 }
242 }
243
244 return undeclared
245 }