schema_option.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. package validate
  15. // SchemaValidatorOptions defines optional rules for schema validation
  16. type SchemaValidatorOptions struct {
  17. EnableObjectArrayTypeCheck bool
  18. EnableArrayMustHaveItemsCheck bool
  19. }
  20. // Option sets optional rules for schema validation
  21. type Option func(*SchemaValidatorOptions)
  22. // EnableObjectArrayTypeCheck activates the swagger rule: an items must be in type: array
  23. func EnableObjectArrayTypeCheck(enable bool) Option {
  24. return func(svo *SchemaValidatorOptions) {
  25. svo.EnableObjectArrayTypeCheck = enable
  26. }
  27. }
  28. // EnableArrayMustHaveItemsCheck activates the swagger rule: an array must have items defined
  29. func EnableArrayMustHaveItemsCheck(enable bool) Option {
  30. return func(svo *SchemaValidatorOptions) {
  31. svo.EnableArrayMustHaveItemsCheck = enable
  32. }
  33. }
  34. // SwaggerSchema activates swagger schema validation rules
  35. func SwaggerSchema(enable bool) Option {
  36. return func(svo *SchemaValidatorOptions) {
  37. svo.EnableObjectArrayTypeCheck = enable
  38. svo.EnableArrayMustHaveItemsCheck = enable
  39. }
  40. }
  41. // Options returns current options
  42. func (svo SchemaValidatorOptions) Options() []Option {
  43. return []Option{
  44. EnableObjectArrayTypeCheck(svo.EnableObjectArrayTypeCheck),
  45. EnableArrayMustHaveItemsCheck(svo.EnableArrayMustHaveItemsCheck),
  46. }
  47. }