Skip to content

Commit

Permalink
privatizing
Browse files Browse the repository at this point in the history
  • Loading branch information
danicc097 committed Oct 20, 2022
1 parent a34bd19 commit b255af9
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions openapi3/validation_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,56 @@ package openapi3
import "context"

// ValidationOption allows the modification of how the OpenAPI document is validated.
type ValidationOption func(options *ValidationOptions)
type ValidationOption func(options *validationOptions)

// ExamplesValidation provides configuration for validating examples against their schema.
type ExamplesValidation struct {
type examplesValidation struct {
Disabled bool
AsReq, AsRes bool
}

// ValidationOptions provides configuration for validating OpenAPI documents.
type ValidationOptions struct {
type validationOptions struct {
SchemaFormatValidationEnabled bool
SchemaPatternValidationDisabled bool
ExamplesValidation *ExamplesValidation
ExamplesValidation *examplesValidation
}

func NewValidationOptions() *ValidationOptions {
return &ValidationOptions{
ExamplesValidation: &ExamplesValidation{},
// NewValidationOptions creates configuration for validating OpenAPI documents.
func NewValidationOptions() *validationOptions {
return &validationOptions{
ExamplesValidation: &examplesValidation{},
}
}

type validationOptionsKey struct{}

// EnableSchemaFormatValidation makes Validate not return an error when validating documents that mention schema formats that are not defined by the OpenAPIv3 specification.
func EnableSchemaFormatValidation() ValidationOption {
return func(options *ValidationOptions) {
return func(options *validationOptions) {
options.SchemaFormatValidationEnabled = true
}
}

// DisableSchemaPatternValidation makes Validate not return an error when validating patterns that are not supported by the Go regexp engine.
func DisableSchemaPatternValidation() ValidationOption {
return func(options *ValidationOptions) {
return func(options *validationOptions) {
options.SchemaPatternValidationDisabled = true
}
}

// DisableExamplesValidation disables all example schema validation.
func DisableExamplesValidation() ValidationOption {
return func(options *ValidationOptions) {
return func(options *validationOptions) {
options.ExamplesValidation.Disabled = true
}
}

// WithValidationOptions allows adding validation options to a context object that can be used when validationg any OpenAPI type.
func WithValidationOptions(ctx context.Context, options *ValidationOptions) context.Context {
func WithValidationOptions(ctx context.Context, options *validationOptions) context.Context {
return context.WithValue(ctx, validationOptionsKey{}, options)
}

func getValidationOptions(ctx context.Context) *ValidationOptions {
if options, ok := ctx.Value(validationOptionsKey{}).(*ValidationOptions); ok {
func getValidationOptions(ctx context.Context) *validationOptions {
if options, ok := ctx.Value(validationOptionsKey{}).(*validationOptions); ok {
return options
}
return NewValidationOptions()
Expand Down

0 comments on commit b255af9

Please sign in to comment.