@@ -348,6 +348,100 @@ func (st SchemaTypeCheck) validateObject(schema *v3.Schema, context *model.RuleF
348
348
}
349
349
}
350
350
351
- // TODO: DependentRequired
351
+ // Validate DependentRequired
352
+ dependentRequiredResults := st .validateDependentRequired (schema , context )
353
+ results = append (results , dependentRequiredResults ... )
354
+
352
355
return results
353
356
}
357
+
358
+ func (st SchemaTypeCheck ) validateDependentRequired (schema * v3.Schema , context * model.RuleFunctionContext ) []model.RuleFunctionResult {
359
+ var results []model.RuleFunctionResult
360
+
361
+ // Check if DependentRequired is present
362
+ if schema .Value .DependentRequired == nil {
363
+ return results
364
+ }
365
+
366
+ // Iterate through all dependent required entries
367
+ for pair := schema .Value .DependentRequired .First (); pair != nil ; pair = pair .Next () {
368
+ triggerProp := pair .Key ()
369
+ requiredProps := pair .Value ()
370
+
371
+ // Validate that trigger property exists in schema properties if properties are defined
372
+ if schema .Value .Properties != nil && schema .Value .Properties .GetOrZero (triggerProp ) == nil {
373
+ // Check if the property exists in polymorphic schemas (anyOf, oneOf, allOf)
374
+ polyDefined := st .checkPolymorphicProperty (schema , triggerProp )
375
+ if ! polyDefined {
376
+ result := st .buildResult (
377
+ fmt .Sprintf ("property `%s` referenced in `dependentRequired` does not exist in schema `properties`" , triggerProp ),
378
+ schema .GenerateJSONPath (), "dependentRequired" , - 1 ,
379
+ schema , schema .Value .GoLow ().DependentRequired .KeyNode , context )
380
+ results = append (results , result )
381
+ }
382
+ }
383
+
384
+ // Validate that all dependent properties exist in schema
385
+ for _ , reqProp := range requiredProps {
386
+ if schema .Value .Properties != nil && schema .Value .Properties .GetOrZero (reqProp ) == nil {
387
+ // Check if the property exists in polymorphic schemas
388
+ polyDefined := st .checkPolymorphicProperty (schema , reqProp )
389
+ if ! polyDefined {
390
+ result := st .buildResult (
391
+ fmt .Sprintf ("property `%s` referenced in `dependentRequired` does not exist in schema `properties`" , reqProp ),
392
+ schema .GenerateJSONPath (), "dependentRequired" , - 1 ,
393
+ schema , schema .Value .GoLow ().DependentRequired .KeyNode , context )
394
+ results = append (results , result )
395
+ }
396
+ }
397
+ }
398
+
399
+ // Validate no self-referential dependencies
400
+ for _ , reqProp := range requiredProps {
401
+ if reqProp == triggerProp {
402
+ result := st .buildResult (
403
+ fmt .Sprintf ("circular dependency detected: property `%s` requires itself in `dependentRequired`" , triggerProp ),
404
+ schema .GenerateJSONPath (), "dependentRequired" , - 1 ,
405
+ schema , schema .Value .GoLow ().DependentRequired .KeyNode , context )
406
+ results = append (results , result )
407
+ }
408
+ }
409
+ }
410
+
411
+ return results
412
+ }
413
+
414
+ // checkPolymorphicProperty checks if a property is defined in anyOf, oneOf, or allOf schemas
415
+ func (st SchemaTypeCheck ) checkPolymorphicProperty (schema * v3.Schema , propertyName string ) bool {
416
+ // Check in AnyOf schemas
417
+ if schema .Value .AnyOf != nil {
418
+ for _ , anyOfSchema := range schema .Value .AnyOf {
419
+ if anyOfSchema .Schema () != nil && anyOfSchema .Schema ().Properties != nil &&
420
+ anyOfSchema .Schema ().Properties .GetOrZero (propertyName ) != nil {
421
+ return true
422
+ }
423
+ }
424
+ }
425
+
426
+ // Check in OneOf schemas
427
+ if schema .Value .OneOf != nil {
428
+ for _ , oneOfSchema := range schema .Value .OneOf {
429
+ if oneOfSchema .Schema () != nil && oneOfSchema .Schema ().Properties != nil &&
430
+ oneOfSchema .Schema ().Properties .GetOrZero (propertyName ) != nil {
431
+ return true
432
+ }
433
+ }
434
+ }
435
+
436
+ // Check in AllOf schemas
437
+ if schema .Value .AllOf != nil {
438
+ for _ , allOfSchema := range schema .Value .AllOf {
439
+ if allOfSchema .Schema () != nil && allOfSchema .Schema ().Properties != nil &&
440
+ allOfSchema .Schema ().Properties .GetOrZero (propertyName ) != nil {
441
+ return true
442
+ }
443
+ }
444
+ }
445
+
446
+ return false
447
+ }
0 commit comments