Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package config

import (
"github.com/pb33f/libopenapi-validator/cache"
"github.com/santhosh-tekuri/jsonschema/v6"

"github.com/pb33f/libopenapi-validator/cache"
)

// RegexCache can be set to enable compiled regex caching.
Expand Down
3 changes: 1 addition & 2 deletions parameters/header_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import (
"strconv"
"strings"

lowbase "github.com/pb33f/libopenapi/datamodel/low/base"

"github.com/pb33f/libopenapi/datamodel/high/base"

v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"

"github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/libopenapi-validator/helpers"
Expand Down
10 changes: 5 additions & 5 deletions parameters/validate_parameter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"sync"
"testing"

"github.com/pb33f/libopenapi-validator/config"
"github.com/pb33f/libopenapi-validator/helpers"
lowv3 "github.com/pb33f/libopenapi/datamodel/low/v3"

"github.com/pb33f/libopenapi"
"github.com/stretchr/testify/assert"

"github.com/stretchr/testify/require"

lowv3 "github.com/pb33f/libopenapi/datamodel/low/v3"

"github.com/pb33f/libopenapi-validator/config"
"github.com/pb33f/libopenapi-validator/helpers"
)

func Test_ForceCompilerError(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions requests/validate_body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (
"testing"

"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi-validator/config"
"github.com/pb33f/libopenapi-validator/paths"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/pb33f/libopenapi-validator/config"
"github.com/pb33f/libopenapi-validator/paths"
)

func TestValidateBody_NotRequiredBody(t *testing.T) {
Expand Down
11 changes: 6 additions & 5 deletions requests/validate_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ import (
"regexp"
"strconv"

"github.com/pb33f/libopenapi-validator/cache"
"github.com/pb33f/libopenapi-validator/config"
"github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/libopenapi-validator/helpers"
"github.com/pb33f/libopenapi-validator/schema_validation"
"github.com/pb33f/libopenapi/datamodel/high/base"
"github.com/pb33f/libopenapi/utils"
"github.com/santhosh-tekuri/jsonschema/v6"
"go.yaml.in/yaml/v4"
"golang.org/x/text/language"
"golang.org/x/text/message"

"github.com/pb33f/libopenapi-validator/cache"
"github.com/pb33f/libopenapi-validator/config"
"github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/libopenapi-validator/helpers"
"github.com/pb33f/libopenapi-validator/schema_validation"
)

var instanceLocationRegex = regexp.MustCompile(`^/(\d+)`)
Expand Down
3 changes: 2 additions & 1 deletion requests/validate_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"testing"

"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi-validator/config"
"github.com/pb33f/libopenapi/datamodel/high/base"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/pb33f/libopenapi-validator/config"
)

func TestValidateRequestSchema(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions responses/validate_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"net/http"
"strings"

"github.com/pb33f/libopenapi-validator/helpers"
"github.com/pb33f/libopenapi-validator/parameters"
"github.com/pb33f/libopenapi/orderedmap"

v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
lowv3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/orderedmap"

"github.com/pb33f/libopenapi-validator/config"
"github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/libopenapi-validator/helpers"
"github.com/pb33f/libopenapi-validator/parameters"
)

// ValidateResponseHeaders validates the response headers against the OpenAPI spec.
Expand Down
11 changes: 6 additions & 5 deletions responses/validate_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ import (
"regexp"
"strconv"

"github.com/pb33f/libopenapi-validator/cache"
"github.com/pb33f/libopenapi-validator/config"
"github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/libopenapi-validator/helpers"
"github.com/pb33f/libopenapi-validator/schema_validation"
"github.com/pb33f/libopenapi/datamodel/high/base"
"github.com/pb33f/libopenapi/utils"
"github.com/santhosh-tekuri/jsonschema/v6"
"go.yaml.in/yaml/v4"
"golang.org/x/text/language"
"golang.org/x/text/message"

"github.com/pb33f/libopenapi-validator/cache"
"github.com/pb33f/libopenapi-validator/config"
"github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/libopenapi-validator/helpers"
"github.com/pb33f/libopenapi-validator/schema_validation"
)

var instanceLocationRegex = regexp.MustCompile(`^/(\d+)`)
Expand Down
3 changes: 2 additions & 1 deletion responses/validate_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"testing"

"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi-validator/config"
"github.com/pb33f/libopenapi/datamodel/high/base"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/pb33f/libopenapi-validator/config"
)

func TestValidateResponseSchema(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions schema_validation/property_locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"regexp"
"strings"

liberrors "github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/libopenapi-validator/helpers"
"github.com/santhosh-tekuri/jsonschema/v6"
"go.yaml.in/yaml/v4"

liberrors "github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/libopenapi-validator/helpers"
)

// PropertyNameInfo contains extracted information about a property name validation error
Expand Down
12 changes: 1 addition & 11 deletions schema_validation/validate_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ import (
)

// SchemaValidator is an interface that defines the methods for validating a *base.Schema (V3+ Only) object.
// There are 8 methods for validating a schema:
// There are 6 methods for validating a schema:
//
// ValidateSchemaString accepts a schema object to validate against, and a JSON/YAML blob that is defined as a string.
// ValidateSchemaObject accepts a schema object to validate against, and an object, created from unmarshalled JSON/YAML.
// ValidateSchemaBytes accepts a schema object to validate against, and a JSON/YAML blob that is defined as a byte array.
// ValidateSchemaStringWithVersion - version-aware validation that allows OpenAPI 3.0 keywords when version is specified.
// ValidateSchemaObjectWithVersion - version-aware validation that allows OpenAPI 3.0 keywords when version is specified.
// ValidateSchemaBytesWithVersion - version-aware validation that allows OpenAPI 3.0 keywords when version is specified.
// ValidateXMLString - validates XML string against schema, applying OpenAPI xml object transformations.
// ValidateXMLStringWithVersion - version-aware XML validation.
type SchemaValidator interface {
// ValidateSchemaString accepts a schema object to validate against, and a JSON/YAML blob that is defined as a string.
// Uses OpenAPI 3.1+ validation by default (strict JSON Schema compliance).
Expand Down Expand Up @@ -67,14 +65,6 @@ type SchemaValidator interface {
// When version is 3.0, OpenAPI 3.0-specific keywords like 'nullable' are allowed and processed.
// When version is 3.1+, OpenAPI 3.0-specific keywords like 'nullable' will cause validation to fail.
ValidateSchemaBytesWithVersion(schema *base.Schema, payload []byte, version float32) (bool, []*liberrors.ValidationError)

// ValidateXMLString validates an XML string against an OpenAPI schema, applying xml object transformations.
// Uses OpenAPI 3.1+ validation by default.
ValidateXMLString(schema *base.Schema, xmlString string) (bool, []*liberrors.ValidationError)

// ValidateXMLStringWithVersion validates an XML string with version-specific rules.
// When version is 3.0, OpenAPI 3.0-specific keywords like 'nullable' are allowed.
ValidateXMLStringWithVersion(schema *base.Schema, xmlString string, version float32) (bool, []*liberrors.ValidationError)
}

var instanceLocationRegex = regexp.MustCompile(`^/(\d+)`)
Expand Down
3 changes: 2 additions & 1 deletion schema_validation/validate_schema_coercion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"testing"

"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi-validator/config"
"github.com/stretchr/testify/assert"

"github.com/pb33f/libopenapi-validator/config"
)

func TestSchemaValidator_ScalarCoercion_Boolean(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion schema_validation/validate_schema_openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"testing"

"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi-validator/config"
"github.com/stretchr/testify/assert"

"github.com/pb33f/libopenapi-validator/config"
)

func TestSchemaValidator_NullableKeyword_OpenAPI30_Success(t *testing.T) {
Expand Down
20 changes: 4 additions & 16 deletions schema_validation/validate_xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,15 @@ import (
"log/slog"
"strings"

xj "github.com/basgys/goxml2json"
"github.com/pb33f/libopenapi/datamodel/high/base"

xj "github.com/basgys/goxml2json"

liberrors "github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/libopenapi-validator/helpers"
)

// ValidateXMLString validates an XML string against an OpenAPI schema,
// applying xml object transformations before validation.
// uses openapi 3.1+ validation by default.
func (s *schemaValidator) ValidateXMLString(schema *base.Schema, xmlString string) (bool, []*liberrors.ValidationError) {
return s.validateXMLWithVersion(schema, xmlString, s.logger, 3.1)
}

// ValidateXMLStringWithVersion validates an XML string with version-specific rules.
// when version is 3.0, openapi 3.0-specific keywords like 'nullable' are allowed.
func (s *schemaValidator) ValidateXMLStringWithVersion(schema *base.Schema, xmlString string, version float32) (bool, []*liberrors.ValidationError) {
return s.validateXMLWithVersion(schema, xmlString, s.logger, version)
}

func (s *schemaValidator) validateXMLWithVersion(schema *base.Schema, xmlString string, log *slog.Logger, version float32) (bool, []*liberrors.ValidationError) {
func (x *xmlValidator) validateXMLWithVersion(schema *base.Schema, xmlString string, log *slog.Logger, version float32) (bool, []*liberrors.ValidationError) {
var validationErrors []*liberrors.ValidationError

if schema == nil {
Expand Down Expand Up @@ -58,7 +46,7 @@ func (s *schemaValidator) validateXMLWithVersion(schema *base.Schema, xmlString
}

// validate transformed json against schema using existing validator
return s.validateSchemaWithVersion(schema, nil, transformedJSON, log, version)
return x.schemaValidator.validateSchemaWithVersion(schema, nil, transformedJSON, log, version)
}

// transformXMLToSchemaJSON converts xml to json structure matching openapi schema.
Expand Down
Loading