Skip to content

Commit

Permalink
Modified to be backward compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
JemDay committed Dec 4, 2024
1 parent 45fc254 commit e43d7a5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
13 changes: 10 additions & 3 deletions parameters/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,22 @@ type ParameterValidator interface {
}

type Config struct {
Document *v3.Document
RegexEngine jsonschema.RegexpEngine
}

// NewParameterValidator will create a new ParameterValidator from an OpenAPI 3+ document
func NewParameterValidator(config Config) ParameterValidator {
return &paramValidator{config}
func NewParameterValidator(document *v3.Document, cfg ...Config) ParameterValidator {

config := Config{}

if len(cfg) > 0 {
config = cfg[0]
}

return &paramValidator{config, document}
}

type paramValidator struct {
Config
document *v3.Document
}
13 changes: 10 additions & 3 deletions requests/request_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ type RequestBodyValidator interface {
}

type Config struct {
Document *v3.Document
RegexEngine jsonschema.RegexpEngine
}

// NewRequestBodyValidator will create a new RequestBodyValidator from an OpenAPI 3+ document
func NewRequestBodyValidator(config Config) RequestBodyValidator {
return &requestBodyValidator{Config: config, schemaCache: &sync.Map{}}
func NewRequestBodyValidator(document *v3.Document, cfg ...Config) RequestBodyValidator {

config := Config{} // Default

if len(cfg) > 0 {
config = cfg[0]
}

return &requestBodyValidator{Config: config, document: document, schemaCache: &sync.Map{}}
}

type schemaCache struct {
Expand All @@ -48,5 +54,6 @@ type schemaCache struct {

type requestBodyValidator struct {
Config
document *v3.Document
schemaCache *sync.Map
}
17 changes: 15 additions & 2 deletions responses/response_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package responses

import (
"github.com/santhosh-tekuri/jsonschema/v6"
"net/http"
"sync"

Expand All @@ -30,9 +31,20 @@ type ResponseBodyValidator interface {
ValidateResponseBodyWithPathItem(request *http.Request, response *http.Response, pathItem *v3.PathItem, pathFound string) (bool, []*errors.ValidationError)
}

type Config struct {
RegexEngine jsonschema.RegexpEngine
}

// NewResponseBodyValidator will create a new ResponseBodyValidator from an OpenAPI 3+ document
func NewResponseBodyValidator(document *v3.Document) ResponseBodyValidator {
return &responseBodyValidator{document: document, schemaCache: &sync.Map{}}
func NewResponseBodyValidator(document *v3.Document, cfg ...Config) ResponseBodyValidator {

config := Config{} // Default

if len(cfg) > 0 {
config = cfg[0]
}

return &responseBodyValidator{Config: config, document: document, schemaCache: &sync.Map{}}
}

type schemaCache struct {
Expand All @@ -42,6 +54,7 @@ type schemaCache struct {
}

type responseBodyValidator struct {
Config
document *v3.Document
schemaCache *sync.Map
}
4 changes: 2 additions & 2 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ func NewValidatorFromV3Model(m *v3.Document, config ...Configuration) Validator
}

// create a new parameter validator
paramValidator := parameters.NewParameterValidator(parameters.Config{Document: m, RegexEngine: cfg.RegexEngine})
paramValidator := parameters.NewParameterValidator(m, parameters.Config{RegexEngine: cfg.RegexEngine})

// create a new request body validator
reqBodyValidator := requests.NewRequestBodyValidator(requests.Config{Document: m, RegexEngine: cfg.RegexEngine})
reqBodyValidator := requests.NewRequestBodyValidator(m, requests.Config{RegexEngine: cfg.RegexEngine})

// create a response body validator
respBodyValidator := responses.NewResponseBodyValidator(m)
Expand Down

0 comments on commit e43d7a5

Please sign in to comment.