| 1 |
// Copyright 2023-2025 Princess Beef Heavy Industries, LLC / Dave Shanley |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package strict |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
|
| 10 |
"github.com/pb33f/libopenapi/datamodel/high/base" |
| 11 |
"github.com/pb33f/libopenapi/utils" |
| 12 |
"github.com/santhosh-tekuri/jsonschema/v6" |
| 13 |
|
| 14 |
"github.com/pb33f/libopenapi-validator/helpers" |
| 15 |
) |
| 16 |
|
| 17 |
// dataMatchesSchema checks if the given data matches the schema using |
| 18 |
// JSON Schema validation. This is used for: |
| 19 |
// - oneOf/anyOf variant selection (finding which variant the data matches) |
| 20 |
// - if/then/else condition evaluation |
| 21 |
// - additionalProperties schema matching |
| 22 |
// |
| 23 |
// The method uses version-aware schema compilation to handle OpenAPI 3.0 vs 3.1 |
| 24 |
// differences (especially nullable handling). |
| 25 |
// |
| 26 |
// Returns (true, nil) if data matches the schema. |
| 27 |
// Returns (false, nil) if data does not match the schema. |
| 28 |
// Returns (false, error) if schema compilation failed. |
| 29 |
func (v *Validator) dataMatchesSchema(schema *base.Schema, data any) (bool, error) { |
| 30 |
if schema == nil { |
| 31 |
return true, nil // No schema means anything matches |
| 32 |
} |
| 33 |
|
| 34 |
compiled, err := v.getCompiledSchema(schema) |
| 35 |
if err != nil { |
| 36 |
return false, err |
| 37 |
} |
| 38 |
return compiled.Validate(data) == nil, nil |
| 39 |
} |
| 40 |
|
| 41 |
// getCompiledSchema returns a compiled JSON Schema for the given high-level schema. |
| 42 |
// It checks multiple cache levels: |
| 43 |
// 1. Global SchemaCache (if configured in options) |
| 44 |
// 2. Local instance cache (for reuse within this validation call) |
| 45 |
// 3. Compiles on-the-fly if not cached |
| 46 |
// |
| 47 |
// Returns the compiled schema and nil error on success. |
| 48 |
// Returns nil schema and nil error if the input schema is nil. |
| 49 |
// Returns nil schema and error if compilation failed. |
| 50 |
func (v *Validator) getCompiledSchema(schema *base.Schema) (*jsonschema.Schema, error) { |
| 51 |
if schema == nil || schema.GoLow() == nil { |
| 52 |
return nil, nil |
| 53 |
} |
| 54 |
|
| 55 |
hash := schema.GoLow().Hash() |
| 56 |
hashKey := fmt.Sprintf("%x", hash) |
| 57 |
|
| 58 |
// try global cache first (if available) |
| 59 |
if v.options != nil && v.options.SchemaCache != nil { |
| 60 |
if cached, ok := v.options.SchemaCache.Load(hash); ok && cached != nil && cached.CompiledSchema != nil { |
| 61 |
return cached.CompiledSchema, nil |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
// try local instance cache |
| 66 |
if compiled, ok := v.localCache[hashKey]; ok { |
| 67 |
return compiled, nil |
| 68 |
} |
| 69 |
|
| 70 |
// cache miss - compile on-the-fly with context-aware rendering |
| 71 |
compiled, err := v.compileSchema(schema) |
| 72 |
if err != nil { |
| 73 |
return nil, err |
| 74 |
} |
| 75 |
if compiled != nil { |
| 76 |
v.localCache[hashKey] = compiled |
| 77 |
} |
| 78 |
|
| 79 |
return compiled, nil |
| 80 |
} |
| 81 |
|
| 82 |
// compileSchema renders and compiles a schema for validation. |
| 83 |
// Uses RenderInlineWithContext for safe cycle handling. |
| 84 |
// |
| 85 |
// Returns the compiled schema and nil error on success. |
| 86 |
// Returns nil schema and error if any step fails (render, conversion, compilation). |
| 87 |
func (v *Validator) compileSchema(schema *base.Schema) (*jsonschema.Schema, error) { |
| 88 |
if schema == nil { |
| 89 |
return nil, nil |
| 90 |
} |
| 91 |
|
| 92 |
schemaHash := fmt.Sprintf("%x", schema.GoLow().Hash()) |
| 93 |
|
| 94 |
// use RenderInlineWithContext for safe cycle handling |
| 95 |
renderedSchema, err := schema.RenderInlineWithContext(v.renderCtx) |
| 96 |
if err != nil { |
| 97 |
return nil, fmt.Errorf("strict: schema render failed (hash=%s): %w", schemaHash, err) |
| 98 |
} |
| 99 |
|
| 100 |
jsonSchema, convErr := utils.ConvertYAMLtoJSON(renderedSchema) |
| 101 |
if convErr != nil { |
| 102 |
return nil, fmt.Errorf("strict: YAML to JSON conversion failed: %w", convErr) |
| 103 |
} |
| 104 |
if len(jsonSchema) == 0 { |
| 105 |
return nil, errors.New("strict: schema rendered to empty JSON") |
| 106 |
} |
| 107 |
|
| 108 |
schemaName := fmt.Sprintf("strict-match-%s", schemaHash) |
| 109 |
compiled, err := helpers.NewCompiledSchemaWithVersion( |
| 110 |
schemaName, |
| 111 |
jsonSchema, |
| 112 |
v.options, |
| 113 |
v.version, |
| 114 |
) |
| 115 |
if err != nil { |
| 116 |
return nil, fmt.Errorf("strict: schema compilation failed (name=%s): %w", schemaName, err) |
| 117 |
} |
| 118 |
|
| 119 |
return compiled, nil |
| 120 |
} |