...

Source file src/github.com/go-openapi/validate/schema_option.go

Documentation: github.com/go-openapi/validate

     1  // Copyright 2015 go-swagger maintainers
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package validate
    16  
    17  // SchemaValidatorOptions defines optional rules for schema validation
    18  type SchemaValidatorOptions struct {
    19  	EnableObjectArrayTypeCheck    bool
    20  	EnableArrayMustHaveItemsCheck bool
    21  	recycleValidators             bool
    22  	recycleResult                 bool
    23  	skipSchemataResult            bool
    24  }
    25  
    26  // Option sets optional rules for schema validation
    27  type Option func(*SchemaValidatorOptions)
    28  
    29  // EnableObjectArrayTypeCheck activates the swagger rule: an items must be in type: array
    30  func EnableObjectArrayTypeCheck(enable bool) Option {
    31  	return func(svo *SchemaValidatorOptions) {
    32  		svo.EnableObjectArrayTypeCheck = enable
    33  	}
    34  }
    35  
    36  // EnableArrayMustHaveItemsCheck activates the swagger rule: an array must have items defined
    37  func EnableArrayMustHaveItemsCheck(enable bool) Option {
    38  	return func(svo *SchemaValidatorOptions) {
    39  		svo.EnableArrayMustHaveItemsCheck = enable
    40  	}
    41  }
    42  
    43  // SwaggerSchema activates swagger schema validation rules
    44  func SwaggerSchema(enable bool) Option {
    45  	return func(svo *SchemaValidatorOptions) {
    46  		svo.EnableObjectArrayTypeCheck = enable
    47  		svo.EnableArrayMustHaveItemsCheck = enable
    48  	}
    49  }
    50  
    51  // WithRecycleValidators saves memory allocations and makes validators
    52  // available for a single use of Validate() only.
    53  //
    54  // When a validator is recycled, called MUST not call the Validate() method twice.
    55  func WithRecycleValidators(enable bool) Option {
    56  	return func(svo *SchemaValidatorOptions) {
    57  		svo.recycleValidators = enable
    58  	}
    59  }
    60  
    61  func withRecycleResults(enable bool) Option {
    62  	return func(svo *SchemaValidatorOptions) {
    63  		svo.recycleResult = enable
    64  	}
    65  }
    66  
    67  // WithSkipSchemataResult skips the deep audit payload stored in validation Result
    68  func WithSkipSchemataResult(enable bool) Option {
    69  	return func(svo *SchemaValidatorOptions) {
    70  		svo.skipSchemataResult = enable
    71  	}
    72  }
    73  
    74  // Options returns the current set of options
    75  func (svo SchemaValidatorOptions) Options() []Option {
    76  	return []Option{
    77  		EnableObjectArrayTypeCheck(svo.EnableObjectArrayTypeCheck),
    78  		EnableArrayMustHaveItemsCheck(svo.EnableArrayMustHaveItemsCheck),
    79  		WithRecycleValidators(svo.recycleValidators),
    80  		withRecycleResults(svo.recycleResult),
    81  		WithSkipSchemataResult(svo.skipSchemataResult),
    82  	}
    83  }
    84  

View as plain text