| 1 |
// Copyright 2023-2025 Princess Beef Heavy Industries, LLC / Dave Shanley |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package strict |
| 5 |
|
| 6 |
import ( |
| 7 |
"strconv" |
| 8 |
|
| 9 |
"github.com/pb33f/libopenapi/datamodel/high/base" |
| 10 |
) |
| 11 |
|
| 12 |
// validateArray checks an array value against a schema for undeclared properties |
| 13 |
// within array items. It handles: |
| 14 |
// - items (schema for all items or boolean) |
| 15 |
// - prefixItems (tuple validation with positional schemas) |
| 16 |
// - unevaluatedItems (items not covered by items/prefixItems) |
| 17 |
func (v *Validator) validateArray(ctx *traversalContext, schema *base.Schema, data []any) []UndeclaredValue { |
| 18 |
if len(data) == 0 { |
| 19 |
return nil |
| 20 |
} |
| 21 |
|
| 22 |
var undeclared []UndeclaredValue |
| 23 |
|
| 24 |
// Check for items: false |
| 25 |
// When items: false, no items are allowed. If base validation passed, the |
| 26 |
// array should be empty. But we explicitly check in case it wasn't caught. |
| 27 |
if schema.Items != nil && schema.Items.IsB() && !schema.Items.B { |
| 28 |
for i := range data { |
| 29 |
itemPath := buildArrayPath(ctx.path, i) |
| 30 |
undeclared = append(undeclared, |
| 31 |
newUndeclaredItem(itemPath, strconv.Itoa(i), data[i], ctx.direction)) |
| 32 |
} |
| 33 |
return undeclared |
| 34 |
} |
| 35 |
|
| 36 |
prefixLen := 0 |
| 37 |
|
| 38 |
// handle prefixItems first (tuple validation) |
| 39 |
if len(schema.PrefixItems) > 0 { |
| 40 |
for i, itemProxy := range schema.PrefixItems { |
| 41 |
if i >= len(data) { |
| 42 |
break |
| 43 |
} |
| 44 |
|
| 45 |
itemPath := buildArrayPath(ctx.path, i) |
| 46 |
itemCtx := ctx.withPath(itemPath) |
| 47 |
|
| 48 |
if itemCtx.shouldIgnore() { |
| 49 |
prefixLen++ |
| 50 |
continue |
| 51 |
} |
| 52 |
|
| 53 |
itemSchema := itemProxy.Schema() |
| 54 |
if itemSchema != nil { |
| 55 |
undeclared = append(undeclared, v.validateValue(itemCtx, itemSchema, data[i])...) |
| 56 |
} |
| 57 |
prefixLen++ |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
// handle items for remaining elements (after prefixItems) |
| 62 |
if schema.Items != nil && schema.Items.A != nil { |
| 63 |
itemProxy := schema.Items.A |
| 64 |
itemSchema := itemProxy.Schema() |
| 65 |
|
| 66 |
if itemSchema != nil { |
| 67 |
for i := prefixLen; i < len(data); i++ { |
| 68 |
itemPath := buildArrayPath(ctx.path, i) |
| 69 |
itemCtx := ctx.withPath(itemPath) |
| 70 |
|
| 71 |
if itemCtx.shouldIgnore() { |
| 72 |
continue |
| 73 |
} |
| 74 |
|
| 75 |
undeclared = append(undeclared, v.validateValue(itemCtx, itemSchema, data[i])...) |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
// handle unevaluatedItems with schema. |
| 81 |
// unevaluatedItems: false is handled by base validation. |
| 82 |
// unevaluatedItems: {schema} means items matching the schema are valid. |
| 83 |
// note: this doesn't account for items evaluated by `contains`. for strict |
| 84 |
// validation this is acceptable as we check conservatively. |
| 85 |
if schema.UnevaluatedItems != nil && schema.UnevaluatedItems.Schema() != nil { |
| 86 |
// this applies to items not covered by items or prefixItems. |
| 87 |
// if there's no items schema, unevaluatedItems applies to: |
| 88 |
// - items after prefixItems (if prefixItems exists) |
| 89 |
// - all items (if neither items nor prefixItems exists) |
| 90 |
if schema.Items == nil { |
| 91 |
unevalSchema := schema.UnevaluatedItems.Schema() |
| 92 |
startIndex := len(schema.PrefixItems) // 0 if no prefixItems |
| 93 |
for i := startIndex; i < len(data); i++ { |
| 94 |
itemPath := buildArrayPath(ctx.path, i) |
| 95 |
itemCtx := ctx.withPath(itemPath) |
| 96 |
|
| 97 |
if itemCtx.shouldIgnore() { |
| 98 |
continue |
| 99 |
} |
| 100 |
|
| 101 |
undeclared = append(undeclared, v.validateValue(itemCtx, unevalSchema, data[i])...) |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
return undeclared |
| 107 |
} |