Skip to content

Commit 0cff4e1

Browse files
committed
adjusted to new XMLValidator interface
1 parent 95e5972 commit 0cff4e1

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

functions/openapi/examples_schema.go

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ import (
2424
)
2525

2626
// ExamplesSchema will check anything that has an example, has a schema and it's valid.
27-
type ExamplesSchema struct {
28-
}
27+
type ExamplesSchema struct{}
2928

3029
// GetSchema returns a model.RuleFunctionSchema defining the schema of the ComponentDescription rule.
3130
func (es ExamplesSchema) GetSchema() model.RuleFunctionSchema {
@@ -41,7 +40,6 @@ var bannedErrors = []string{"if-then failed", "if-else failed", "allOf failed",
4140

4241
// RunRule will execute the ComponentDescription rule, based on supplied context and a supplied []*yaml.Node slice.
4342
func (es ExamplesSchema) RunRule(_ []*yaml.Node, ruleContext model.RuleFunctionContext) []model.RuleFunctionResult {
44-
4543
var results []model.RuleFunctionResult
4644

4745
if ruleContext.DrDocument == nil {
@@ -144,15 +142,16 @@ func (es ExamplesSchema) RunRule(_ []*yaml.Node, ruleContext model.RuleFunctionC
144142
}
145143

146144
validator := schema_validation.NewSchemaValidator()
145+
xmlValidator := schema_validation.NewXMLValidator()
147146
version := ruleContext.Document.GetSpecInfo().VersionNumeric
148147
validateSchema := func(iKey *int,
149148
sKey, label string,
150149
s *v3.Schema,
151150
obj v3.AcceptsRuleResults,
152151
node *yaml.Node,
153152
keyNode *yaml.Node,
154-
example any) []model.RuleFunctionResult {
155-
153+
example any,
154+
) []model.RuleFunctionResult {
156155
var rx []model.RuleFunctionResult
157156
if s != nil && s.Value != nil {
158157
var valid bool
@@ -303,16 +302,15 @@ func (es ExamplesSchema) RunRule(_ []*yaml.Node, ruleContext model.RuleFunctionC
303302
}
304303

305304
// createJSONValidator creates a validator for JSON examples
306-
createJSONValidator := func(
307-
iKey *int,
308-
sKey, label string,
309-
s *v3.Schema,
310-
obj v3.AcceptsRuleResults,
311-
node *yaml.Node,
312-
keyNode *yaml.Node,
313-
) exampleValidatorFunc {
305+
createJSONValidator := func(s *v3.Schema, ver float32) exampleValidatorFunc {
314306
return func(example any) (bool, []*errors.ValidationError) {
315-
return validateSchema(iKey, sKey, label, s, obj, node, keyNode, example)
307+
if s != nil && s.Value != nil {
308+
if ver > 0 {
309+
return validator.ValidateSchemaObjectWithVersion(s.Value, example, ver)
310+
}
311+
return validator.ValidateSchemaObject(s.Value, example)
312+
}
313+
return true, nil
316314
}
317315
}
318316

@@ -321,9 +319,9 @@ func (es ExamplesSchema) RunRule(_ []*yaml.Node, ruleContext model.RuleFunctionC
321319
return func(example any) (bool, []*errors.ValidationError) {
322320
if xmlStr, ok := example.(string); ok {
323321
if ver > 0 {
324-
return validator.ValidateXMLStringWithVersion(s.Value, xmlStr, ver)
322+
return xmlValidator.ValidateXMLStringWithVersion(s.Value, xmlStr, ver)
325323
}
326-
return validator.ValidateXMLString(s.Value, xmlStr)
324+
return xmlValidator.ValidateXMLString(s.Value, xmlStr)
327325
}
328326
return true, nil
329327
}
@@ -332,8 +330,8 @@ func (es ExamplesSchema) RunRule(_ []*yaml.Node, ruleContext model.RuleFunctionC
332330
parseExamples := func(s *v3.Schema,
333331
obj v3.AcceptsRuleResults,
334332
examples *orderedmap.Map[string, *v3Base.Example],
335-
validatorFunc exampleValidatorFunc) []model.RuleFunctionResult {
336-
333+
validatorFunc exampleValidatorFunc,
334+
) []model.RuleFunctionResult {
337335
var rx []model.RuleFunctionResult
338336
for examplesPairs := examples.First(); examplesPairs != nil; examplesPairs = examplesPairs.Next() {
339337
example := examplesPairs.Value()
@@ -379,15 +377,15 @@ func (es ExamplesSchema) RunRule(_ []*yaml.Node, ruleContext model.RuleFunctionC
379377
}
380378

381379
if p.Value.Examples.Len() >= 1 && p.SchemaProxy != nil {
382-
jsonValidator := createJSONValidator(nil, "", "examples", p.SchemaProxy.Schema, p, nil, nil)
380+
jsonValidator := createJSONValidator(p.SchemaProxy.Schema, version)
383381
expLock.Lock()
384382
if p.Value.Examples != nil && p.Value.Examples.Len() > 0 {
385383
results = append(results, parseExamples(p.SchemaProxy.Schema, p, p.Value.Examples, jsonValidator)...)
386384
}
387385
expLock.Unlock()
388386
} else {
389387
if p.Value.Example != nil && p.SchemaProxy != nil {
390-
jsonValidator := createJSONValidator(nil, "", "example", p.SchemaProxy.Schema, p, p.Value.Example, p.Value.GoLow().Example.GetKeyNode())
388+
jsonValidator := createJSONValidator(p.SchemaProxy.Schema, version)
391389
expLock.Lock()
392390
if p.Value.Examples != nil && p.Value.Examples.Len() > 0 {
393391
results = append(results, parseExample(p.SchemaProxy.Schema, p.Value.Example,
@@ -412,13 +410,13 @@ func (es ExamplesSchema) RunRule(_ []*yaml.Node, ruleContext model.RuleFunctionC
412410
}
413411

414412
if h.Value.Examples.Len() >= 1 && h.Schema != nil {
415-
jsonValidator := createJSONValidator(nil, "", "examples", h.Schema.Schema, h, nil, nil)
413+
jsonValidator := createJSONValidator(h.Schema.Schema, version)
416414
expLock.Lock()
417415
results = append(results, parseExamples(h.Schema.Schema, h, h.Value.Examples, jsonValidator)...)
418416
expLock.Unlock()
419417
} else {
420418
if h.Value.Example != nil && h.Schema != nil {
421-
jsonValidator := createJSONValidator(nil, "", "example", h.Schema.Schema, h, h.Value.Example, h.Value.GoLow().Example.GetKeyNode())
419+
jsonValidator := createJSONValidator(h.Schema.Schema, version)
422420
expLock.Lock()
423421
results = append(results, parseExample(h.Schema.Schema, h.Value.Example,
424422
h.Value.GoLow().Example.GetKeyNode(), jsonValidator)...)
@@ -430,7 +428,6 @@ func (es ExamplesSchema) RunRule(_ []*yaml.Node, ruleContext model.RuleFunctionC
430428
}
431429

432430
if ruleContext.DrDocument != nil && ruleContext.DrDocument.MediaTypes != nil {
433-
434431
for i := range ruleContext.DrDocument.MediaTypes {
435432
mt := ruleContext.DrDocument.MediaTypes[i]
436433
spawnWorker(func() {
@@ -450,7 +447,7 @@ func (es ExamplesSchema) RunRule(_ []*yaml.Node, ruleContext model.RuleFunctionC
450447
if isXML {
451448
exampleValidator = createXMLValidator(mt.SchemaProxy.Schema, version)
452449
} else {
453-
exampleValidator = createJSONValidator(nil, "", "examples", mt.SchemaProxy.Schema, mt, nil, nil)
450+
exampleValidator = createJSONValidator(mt.SchemaProxy.Schema, version)
454451
}
455452
expLock.Lock()
456453
results = append(results, parseExamples(mt.SchemaProxy.Schema, mt, mt.Value.Examples, exampleValidator)...)
@@ -461,7 +458,7 @@ func (es ExamplesSchema) RunRule(_ []*yaml.Node, ruleContext model.RuleFunctionC
461458
if isXML {
462459
exampleValidator = createXMLValidator(mt.SchemaProxy.Schema, version)
463460
} else {
464-
exampleValidator = createJSONValidator(nil, "", "example", mt.SchemaProxy.Schema, mt, mt.Value.Example, mt.Value.GoLow().Example.GetKeyNode())
461+
exampleValidator = createJSONValidator(mt.SchemaProxy.Schema, version)
465462
}
466463
expLock.Lock()
467464
results = append(results, parseExample(mt.SchemaProxy.Schema, mt.Value.Example,
@@ -471,7 +468,6 @@ func (es ExamplesSchema) RunRule(_ []*yaml.Node, ruleContext model.RuleFunctionC
471468
}
472469
})
473470
}
474-
475471
}
476472

477473
// wait for all workers to complete or context to timeout

0 commit comments

Comments
 (0)