| 1 |
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package schema_validation |
| 5 |
|
| 6 |
import ( |
| 7 |
"log/slog" |
| 8 |
"os" |
| 9 |
|
| 10 |
"github.com/pb33f/libopenapi/datamodel/high/base" |
| 11 |
|
| 12 |
"github.com/pb33f/libopenapi-validator/config" |
| 13 |
liberrors "github.com/pb33f/libopenapi-validator/errors" |
| 14 |
) |
| 15 |
|
| 16 |
// XMLValidator is an interface that defines methods for validating XML against OpenAPI schemas. |
| 17 |
// There are 2 methods for validating XML: |
| 18 |
// |
| 19 |
// ValidateXMLString validates an XML string against a schema, applying OpenAPI xml object transformations. |
| 20 |
// ValidateXMLStringWithVersion - version-aware XML validation that allows OpenAPI 3.0 keywords when version is specified. |
| 21 |
type XMLValidator interface { |
| 22 |
// ValidateXMLString validates an XML string against an OpenAPI schema, applying xml object transformations. |
| 23 |
// Uses OpenAPI 3.1+ validation by default (strict JSON Schema compliance). |
| 24 |
ValidateXMLString(schema *base.Schema, xmlString string) (bool, []*liberrors.ValidationError) |
| 25 |
|
| 26 |
// ValidateXMLStringWithVersion validates an XML string with version-specific rules. |
| 27 |
// When version is 3.0, OpenAPI 3.0-specific keywords like 'nullable' are allowed and processed. |
| 28 |
// When version is 3.1+, OpenAPI 3.0-specific keywords like 'nullable' will cause validation to fail. |
| 29 |
ValidateXMLStringWithVersion(schema *base.Schema, xmlString string, version float32) (bool, []*liberrors.ValidationError) |
| 30 |
} |
| 31 |
|
| 32 |
type xmlValidator struct { |
| 33 |
schemaValidator *schemaValidator |
| 34 |
logger *slog.Logger |
| 35 |
} |
| 36 |
|
| 37 |
// NewXMLValidatorWithLogger creates a new XMLValidator instance with a custom logger. |
| 38 |
func NewXMLValidatorWithLogger(logger *slog.Logger, opts ...config.Option) XMLValidator { |
| 39 |
options := config.NewValidationOptions(opts...) |
| 40 |
// Create an internal schema validator for JSON validation after XML transformation |
| 41 |
sv := &schemaValidator{options: options, logger: logger} |
| 42 |
return &xmlValidator{schemaValidator: sv, logger: logger} |
| 43 |
} |
| 44 |
|
| 45 |
// NewXMLValidator creates a new XMLValidator instance with default logging configuration. |
| 46 |
func NewXMLValidator(opts ...config.Option) XMLValidator { |
| 47 |
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ |
| 48 |
Level: slog.LevelError, |
| 49 |
})) |
| 50 |
return NewXMLValidatorWithLogger(logger, opts...) |
| 51 |
} |
| 52 |
|
| 53 |
func (x *xmlValidator) ValidateXMLString(schema *base.Schema, xmlString string) (bool, []*liberrors.ValidationError) { |
| 54 |
return x.validateXMLWithVersion(schema, xmlString, x.logger, 3.1) |
| 55 |
} |
| 56 |
|
| 57 |
func (x *xmlValidator) ValidateXMLStringWithVersion(schema *base.Schema, xmlString string, version float32) (bool, []*liberrors.ValidationError) { |
| 58 |
return x.validateXMLWithVersion(schema, xmlString, x.logger, version) |
| 59 |
} |