Skip to content

Commit

Permalink
Config PoC: DO NOT MERGE
Browse files Browse the repository at this point in the history
  • Loading branch information
JemDay committed Dec 4, 2024
1 parent c7a822d commit 45fc254
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
12 changes: 9 additions & 3 deletions parameters/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package parameters

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

v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
Expand Down Expand Up @@ -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 &paramValidator{document: document}
func NewParameterValidator(config Config) ParameterValidator {
return &paramValidator{config}
}

type paramValidator struct {
document *v3.Document
Config
}
12 changes: 9 additions & 3 deletions requests/request_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package requests

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

Expand All @@ -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 {
Expand All @@ -41,6 +47,6 @@ type schemaCache struct {
}

type requestBodyValidator struct {
document *v3.Document
Config
schemaCache *sync.Map
}
25 changes: 20 additions & 5 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package validator

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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 45fc254

Please sign in to comment.