diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ diff --git a/parameters/parameters.go b/parameters/parameters.go index 6ea572b..76392df 100644 --- a/parameters/parameters.go +++ b/parameters/parameters.go @@ -4,6 +4,7 @@ package parameters import ( + "github.com/santhosh-tekuri/jsonschema/v6" "net/http" v3 "github.com/pb33f/libopenapi/datamodel/high/v3" @@ -65,11 +66,16 @@ type ParameterValidator interface { ValidateSecurityWithPathItem(request *http.Request, pathItem *v3.PathItem, pathValue string) (bool, []*errors.ValidationError) } +type Config struct { + Document *v3.Document + RegexEngine jsonschema.RegexpEngine +} + // NewParameterValidator will create a new ParameterValidator from an OpenAPI 3+ document -func NewParameterValidator(document *v3.Document) ParameterValidator { - return ¶mValidator{document: document} +func NewParameterValidator(config Config) ParameterValidator { + return ¶mValidator{config} } type paramValidator struct { - document *v3.Document + Config } diff --git a/requests/request_body.go b/requests/request_body.go index 3c534a6..d79672b 100644 --- a/requests/request_body.go +++ b/requests/request_body.go @@ -4,6 +4,7 @@ package requests import ( + "github.com/santhosh-tekuri/jsonschema/v6" "net/http" "sync" @@ -29,9 +30,14 @@ type RequestBodyValidator interface { ValidateRequestBodyWithPathItem(request *http.Request, pathItem *v3.PathItem, pathValue string) (bool, []*errors.ValidationError) } +type Config struct { + Document *v3.Document + RegexEngine jsonschema.RegexpEngine +} + // NewRequestBodyValidator will create a new RequestBodyValidator from an OpenAPI 3+ document -func NewRequestBodyValidator(document *v3.Document) RequestBodyValidator { - return &requestBodyValidator{document: document, schemaCache: &sync.Map{}} +func NewRequestBodyValidator(config Config) RequestBodyValidator { + return &requestBodyValidator{Config: config, schemaCache: &sync.Map{}} } type schemaCache struct { @@ -41,6 +47,6 @@ type schemaCache struct { } type requestBodyValidator struct { - document *v3.Document + Config schemaCache *sync.Map } diff --git a/validator.go b/validator.go index f90af9c..29d6ccb 100644 --- a/validator.go +++ b/validator.go @@ -4,6 +4,7 @@ package validator import ( + "github.com/santhosh-tekuri/jsonschema/v6" "net/http" "sync" @@ -62,24 +63,38 @@ type Validator interface { GetResponseBodyValidator() responses.ResponseBodyValidator } +// Configuration Holds any Validator configuration overrides. +type Configuration struct { + // Use this regex engine in place of the standard Go (RE2) pattern processor + RegexEngine jsonschema.RegexpEngine +} + // NewValidator will create a new Validator from an OpenAPI 3+ document -func NewValidator(document libopenapi.Document) (Validator, []error) { +func NewValidator(document libopenapi.Document, config ...Configuration) (Validator, []error) { m, errs := document.BuildV3Model() if errs != nil { return nil, errs } - v := NewValidatorFromV3Model(&m.Model) + v := NewValidatorFromV3Model(&m.Model, config...) v.(*validator).document = document return v, nil } // NewValidatorFromV3Model will create a new Validator from an OpenAPI Model -func NewValidatorFromV3Model(m *v3.Document) Validator { +func NewValidatorFromV3Model(m *v3.Document, config ...Configuration) Validator { + + // Assume a default configuration + cfg := Configuration{} + + if len(config) > 0 { + cfg = config[0] + } + // create a new parameter validator - paramValidator := parameters.NewParameterValidator(m) + paramValidator := parameters.NewParameterValidator(parameters.Config{Document: m, RegexEngine: cfg.RegexEngine}) // create a new request body validator - reqBodyValidator := requests.NewRequestBodyValidator(m) + reqBodyValidator := requests.NewRequestBodyValidator(requests.Config{Document: m, RegexEngine: cfg.RegexEngine}) // create a response body validator respBodyValidator := responses.NewResponseBodyValidator(m)