Update a few dependencies

github.com/go-openapi/*
github.com/asaskevich/govalidator
This commit is contained in:
Mikhail Mazurskiy
2018-10-18 23:33:10 +11:00
parent b8731a76f0
commit 8763223ab9
191 changed files with 18648 additions and 1800 deletions

26
vendor/github.com/go-openapi/validate/.editorconfig generated vendored Normal file
View File

@@ -0,0 +1,26 @@
# top-most EditorConfig file
root = true
# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
# Set default charset
[*.{js,py,go,scala,rb,java,html,css,less,sass,md}]
charset = utf-8
# Tab indentation (no size specified)
[*.go]
indent_style = tab
[*.md]
trim_trailing_whitespace = false
# Matches the exact files either package.json or .travis.yml
[{package.json,.travis.yml}]
indent_style = space
indent_size = 2

View File

@@ -2,3 +2,4 @@ secrets.yml
coverage.out
*.cov
*.out
playground

20
vendor/github.com/go-openapi/validate/.golangci.yml generated vendored Normal file
View File

@@ -0,0 +1,20 @@
linters-settings:
govet:
check-shadowing: true
golint:
min-confidence: 0
gocyclo:
min-complexity: 25
maligned:
suggest-new: true
dupl:
threshold: 100
goconst:
min-len: 2
min-occurrences: 2
linters:
enable-all: true
disable:
- maligned
- lll

View File

@@ -1,6 +1,14 @@
language: go
after_success:
- bash <(curl -s https://codecov.io/bash)
env:
- TEST_COMMAND="go test -v -race -covermode=atomic"
- TEST_COMMAND="go test -v -timeout=20m -cover -coverprofile=coverage.txt -args -enable-long"
- TEST_COMMAND="go test -v -timeout=30m -args -enable-go-swagger"
- TEST_COMMAND="go test -v -timeout=30m github.com/go-openapi/runtime/..."
go:
- 1.7
- '1.9'
- 1.10.x
- 1.11.x
install:
- go get -u github.com/axw/gocov/gocov
- go get -u gopkg.in/matm/v1/gocov-html
@@ -8,15 +16,20 @@ install:
- go get -u github.com/stretchr/testify/assert
- go get -u github.com/kr/pretty
- go get -u gopkg.in/yaml.v2
- go get -u github.com/go-openapi/spec
- go get -u github.com/go-openapi/analysis
- go get -u github.com/go-openapi/errors
- go get -u github.com/go-openapi/loads
- go get -u github.com/go-openapi/strfmt
- go get -u github.com/go-openapi/runtime
script:
- go test -v -race -cover -coverprofile=coverage.txt -covermode=atomic
after_success:
- bash <(curl -s https://codecov.io/bash)
- go get -u github.com/docker/go-units
language: go
matrix:
exclude:
- env: TEST_COMMAND="go test -v -timeout=30m -args -enable-go-swagger"
go: 1.8
notifications:
slack:
secure: EmObnQuM9Mw8J9vpFaKKHqSMN4Wsr/A9+v7ewAD5cEhA0T1P4m7MbJMiJOhxUhj/X+BFh2DamW+P2lT8mybj5wg8wnkQ2BteKA8Tawi6f9PRw2NRheO8tAi8o/npLnlmet0kc93mn+oLuqHw36w4+j5mkOl2FghkfGiUVhwrhkCP7KXQN+3TU87e+/HzQumlJ3nsE+6terVxkH3PmaUTsS5ONaODZfuxFpfb7RsoEl3skHf6d+tr+1nViLxxly7558Nc33C+W1mr0qiEvMLZ+kJ/CpGWBJ6CUJM3jm6hNe2eMuIPwEK2hxZob8c7n22VPap4K6a0bBRoydoDXaba+2sD7Ym6ivDO/DVyL44VeBBLyIiIBylDGQdZH+6SoWm90Qe/i7tnY/T5Ao5igT8f3cfQY1c3EsTfqmlDfrhmACBmwSlgkdVBLTprHL63JMY24LWmh4jhxsmMRZhCL4dze8su1w6pLN/pD1pGHtKYCEVbdTmaM3PblNRFf12XB7qosmQsgUndH4Vq3bTbU0s1pKjeDhRyLvFzvR0TBbo0pDLEoF1A/i5GVFWa7yLZNUDudQERRh7qv/xBl2excIaQ1sV4DSVm7bAE9l6Kp+yeHQJW2uN6Y3X8wu9gB9nv9l5HBze7wh8KE6PyWAOLYYqZg9/sAtsv/2GcQqXcKFF1zcA=
script:
- (eval "$TEST_COMMAND")

View File

@@ -3,13 +3,22 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = [
"debug.go",
"default_validator.go",
"doc.go",
"example_validator.go",
"formats.go",
"helpers.go",
"object_validator.go",
"options.go",
"result.go",
"rexp.go",
"schema.go",
"schema_messages.go",
"schema_props.go",
"slice_validator.go",
"spec.go",
"spec_messages.go",
"type.go",
"validator.go",
"values.go",

View File

@@ -1,3 +1,6 @@
# Validation helpers [![Build Status](https://travis-ci.org/go-openapi/validate.svg?branch=master)](https://travis-ci.org/go-openapi/validate) [![codecov](https://codecov.io/gh/go-openapi/validate/branch/master/graph/badge.svg)](https://codecov.io/gh/go-openapi/validate) [![Slack Status](https://slackin.goswagger.io/badge.svg)](https://slackin.goswagger.io)
[![license](http://img.shields.io/badge/license-Apache%20v2-orange.svg)](https://raw.githubusercontent.com/go-openapi/validate/master/LICENSE) [![GoDoc](https://godoc.org/github.com/go-openapi/validate?status.svg)](http://godoc.org/github.com/go-openapi/validate)
[![license](http://img.shields.io/badge/license-Apache%20v2-orange.svg)](https://raw.githubusercontent.com/go-openapi/validate/master/LICENSE)
[![GoDoc](https://godoc.org/github.com/go-openapi/validate?status.svg)](http://godoc.org/github.com/go-openapi/validate)
[![GolangCI](https://golangci.com/badges/github.com/go-openapi/validate.svg)](https://golangci.com)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-openapi/validate)](https://goreportcard.com/report/github.com/go-openapi/validate)

47
vendor/github.com/go-openapi/validate/debug.go generated vendored Normal file
View File

@@ -0,0 +1,47 @@
// Copyright 2015 go-swagger maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package validate
import (
"fmt"
"log"
"os"
"path/filepath"
"runtime"
)
var (
// Debug is true when the SWAGGER_DEBUG env var is not empty.
// It enables a more verbose logging of validators.
Debug = os.Getenv("SWAGGER_DEBUG") != ""
// validateLogger is a debug logger for this package
validateLogger *log.Logger
)
func init() {
debugOptions()
}
func debugOptions() {
validateLogger = log.New(os.Stdout, "validate:", log.LstdFlags)
}
func debugLog(msg string, args ...interface{}) {
// A private, trivial trace logger, based on go-openapi/spec/expander.go:debugLog()
if Debug {
_, file1, pos1, _ := runtime.Caller(1)
validateLogger.Printf("%s:%d: %s", filepath.Base(file1), pos1, fmt.Sprintf(msg, args...))
}
}

View File

@@ -0,0 +1,278 @@
// Copyright 2015 go-swagger maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package validate
import (
"fmt"
"strings"
"github.com/go-openapi/spec"
)
// defaultValidator validates default values in a spec.
// According to Swagger spec, default values MUST validate their schema.
type defaultValidator struct {
SpecValidator *SpecValidator
visitedSchemas map[string]bool
}
// resetVisited resets the internal state of visited schemas
func (d *defaultValidator) resetVisited() {
d.visitedSchemas = map[string]bool{}
}
// beingVisited asserts a schema is being visited
func (d *defaultValidator) beingVisited(path string) {
d.visitedSchemas[path] = true
}
// isVisited tells if a path has already been visited
func (d *defaultValidator) isVisited(path string) bool {
found := d.visitedSchemas[path]
if !found {
// search for overlapping paths
frags := strings.Split(path, ".")
if len(frags) < 2 {
// shortcut exit on smaller paths
return found
}
last := len(frags) - 1
var currentFragStr, parent string
for i := range frags {
if i == 0 {
currentFragStr = frags[last]
} else {
currentFragStr = strings.Join([]string{frags[last-i], currentFragStr}, ".")
}
if i < last {
parent = strings.Join(frags[0:last-i], ".")
} else {
parent = ""
}
if strings.HasSuffix(parent, currentFragStr) {
found = true
break
}
}
}
return found
}
// Validate validates the default values declared in the swagger spec
func (d *defaultValidator) Validate() (errs *Result) {
errs = new(Result)
if d == nil || d.SpecValidator == nil {
return errs
}
d.resetVisited()
errs.Merge(d.validateDefaultValueValidAgainstSchema()) // error -
return errs
}
func (d *defaultValidator) validateDefaultValueValidAgainstSchema() *Result {
// every default value that is specified must validate against the schema for that property
// headers, items, parameters, schema
res := new(Result)
s := d.SpecValidator
for method, pathItem := range s.analyzer.Operations() {
if pathItem != nil { // Safeguard
for path, op := range pathItem {
// parameters
for _, param := range paramHelp.safeExpandedParamsFor(path, method, op.ID, res, s) {
if param.Default != nil && param.Required {
res.AddWarnings(requiredHasDefaultMsg(param.Name, param.In))
}
// reset explored schemas to get depth-first recursive-proof exploration
d.resetVisited()
// Check simple parameters first
// default values provided must validate against their inline definition (no explicit schema)
if param.Default != nil && param.Schema == nil {
// check param default value is valid
red := NewParamValidator(&param, s.KnownFormats).Validate(param.Default)
if red.HasErrorsOrWarnings() {
res.AddErrors(defaultValueDoesNotValidateMsg(param.Name, param.In))
res.Merge(red)
}
}
// Recursively follows Items and Schemas
if param.Items != nil {
red := d.validateDefaultValueItemsAgainstSchema(param.Name, param.In, &param, param.Items)
if red.HasErrorsOrWarnings() {
res.AddErrors(defaultValueItemsDoesNotValidateMsg(param.Name, param.In))
res.Merge(red)
}
}
if param.Schema != nil {
// Validate default value against schema
red := d.validateDefaultValueSchemaAgainstSchema(param.Name, param.In, param.Schema)
if red.HasErrorsOrWarnings() {
res.AddErrors(defaultValueDoesNotValidateMsg(param.Name, param.In))
res.Merge(red)
}
}
}
if op.Responses != nil {
if op.Responses.Default != nil {
// Same constraint on default Response
res.Merge(d.validateDefaultInResponse(op.Responses.Default, "default", path, 0, op.ID))
}
// Same constraint on regular Responses
if op.Responses.StatusCodeResponses != nil { // Safeguard
for code, r := range op.Responses.StatusCodeResponses {
res.Merge(d.validateDefaultInResponse(&r, "response", path, code, op.ID))
}
}
} else {
// Empty op.ID means there is no meaningful operation: no need to report a specific message
if op.ID != "" {
res.AddErrors(noValidResponseMsg(op.ID))
}
}
}
}
}
if s.spec.Spec().Definitions != nil { // Safeguard
// reset explored schemas to get depth-first recursive-proof exploration
d.resetVisited()
for nm, sch := range s.spec.Spec().Definitions {
res.Merge(d.validateDefaultValueSchemaAgainstSchema(fmt.Sprintf("definitions.%s", nm), "body", &sch))
}
}
return res
}
func (d *defaultValidator) validateDefaultInResponse(resp *spec.Response, responseType, path string, responseCode int, operationID string) *Result {
s := d.SpecValidator
response, res := responseHelp.expandResponseRef(resp, path, s)
if !res.IsValid() {
return res
}
responseName, responseCodeAsStr := responseHelp.responseMsgVariants(responseType, responseCode)
if response.Headers != nil { // Safeguard
for nm, h := range response.Headers {
// reset explored schemas to get depth-first recursive-proof exploration
d.resetVisited()
if h.Default != nil {
red := NewHeaderValidator(nm, &h, s.KnownFormats).Validate(h.Default)
if red.HasErrorsOrWarnings() {
res.AddErrors(defaultValueHeaderDoesNotValidateMsg(operationID, nm, responseName))
res.Merge(red)
}
}
// Headers have inline definition, like params
if h.Items != nil {
red := d.validateDefaultValueItemsAgainstSchema(nm, "header", &h, h.Items)
if red.HasErrorsOrWarnings() {
res.AddErrors(defaultValueHeaderItemsDoesNotValidateMsg(operationID, nm, responseName))
res.Merge(red)
}
}
if _, err := compileRegexp(h.Pattern); err != nil {
res.AddErrors(invalidPatternInHeaderMsg(operationID, nm, responseName, h.Pattern, err))
}
// Headers don't have schema
}
}
if response.Schema != nil {
// reset explored schemas to get depth-first recursive-proof exploration
d.resetVisited()
red := d.validateDefaultValueSchemaAgainstSchema(responseCodeAsStr, "response", response.Schema)
if red.HasErrorsOrWarnings() {
// Additional message to make sure the context of the error is not lost
res.AddErrors(defaultValueInDoesNotValidateMsg(operationID, responseName))
res.Merge(red)
}
}
return res
}
func (d *defaultValidator) validateDefaultValueSchemaAgainstSchema(path, in string, schema *spec.Schema) *Result {
if schema == nil || d.isVisited(path) {
// Avoids recursing if we are already done with that check
return nil
}
d.beingVisited(path)
res := new(Result)
s := d.SpecValidator
if schema.Default != nil {
res.Merge(NewSchemaValidator(schema, s.spec.Spec(), path+".default", s.KnownFormats).Validate(schema.Default))
}
if schema.Items != nil {
if schema.Items.Schema != nil {
res.Merge(d.validateDefaultValueSchemaAgainstSchema(path+".items.default", in, schema.Items.Schema))
}
// Multiple schemas in items
if schema.Items.Schemas != nil { // Safeguard
for i, sch := range schema.Items.Schemas {
res.Merge(d.validateDefaultValueSchemaAgainstSchema(fmt.Sprintf("%s.items[%d].default", path, i), in, &sch))
}
}
}
if _, err := compileRegexp(schema.Pattern); err != nil {
res.AddErrors(invalidPatternInMsg(path, in, schema.Pattern))
}
if schema.AdditionalItems != nil && schema.AdditionalItems.Schema != nil {
// NOTE: we keep validating values, even though additionalItems is not supported by Swagger 2.0 (and 3.0 as well)
res.Merge(d.validateDefaultValueSchemaAgainstSchema(fmt.Sprintf("%s.additionalItems", path), in, schema.AdditionalItems.Schema))
}
for propName, prop := range schema.Properties {
res.Merge(d.validateDefaultValueSchemaAgainstSchema(path+"."+propName, in, &prop))
}
for propName, prop := range schema.PatternProperties {
res.Merge(d.validateDefaultValueSchemaAgainstSchema(path+"."+propName, in, &prop))
}
if schema.AdditionalProperties != nil && schema.AdditionalProperties.Schema != nil {
res.Merge(d.validateDefaultValueSchemaAgainstSchema(fmt.Sprintf("%s.additionalProperties", path), in, schema.AdditionalProperties.Schema))
}
if schema.AllOf != nil {
for i, aoSch := range schema.AllOf {
res.Merge(d.validateDefaultValueSchemaAgainstSchema(fmt.Sprintf("%s.allOf[%d]", path, i), in, &aoSch))
}
}
return res
}
func (d *defaultValidator) validateDefaultValueItemsAgainstSchema(path, in string, root interface{}, items *spec.Items) *Result {
res := new(Result)
s := d.SpecValidator
if items != nil {
if items.Default != nil {
res.Merge(newItemsValidator(path, in, items, root, s.KnownFormats).Validate(0, items.Default))
}
if items.Items != nil {
res.Merge(d.validateDefaultValueItemsAgainstSchema(path+"[0].default", in, root, items.Items))
}
if _, err := compileRegexp(items.Pattern); err != nil {
res.AddErrors(invalidPatternInMsg(path, in, items.Pattern))
}
}
return res
}

85
vendor/github.com/go-openapi/validate/doc.go generated vendored Normal file
View File

@@ -0,0 +1,85 @@
// Copyright 2015 go-swagger maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
Package validate provides methods to validate a swagger specification,
as well as tools to validate data against their schema.
This package follows Swagger 2.0. specification (aka OpenAPI 2.0). Reference
can be found here: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md.
Validating a specification
Validates a spec document (from JSON or YAML) against the JSON schema for swagger,
then checks a number of extra rules that can't be expressed in JSON schema.
Entry points:
- Spec()
- NewSpecValidator()
- SpecValidator.Validate()
Reported as errors:
[x] definition can't declare a property that's already defined by one of its ancestors
[x] definition's ancestor can't be a descendant of the same model
[x] path uniqueness: each api path should be non-verbatim (account for path param names) unique per method
[x] each security reference should contain only unique scopes
[x] each security scope in a security definition should be unique
[x] parameters in path must be unique
[x] each path parameter must correspond to a parameter placeholder and vice versa
[x] each referenceable definition must have references
[x] each definition property listed in the required array must be defined in the properties of the model
[x] each parameter should have a unique `name` and `type` combination
[x] each operation should have only 1 parameter of type body
[x] each reference must point to a valid object
[x] every default value that is specified must validate against the schema for that property
[x] items property is required for all schemas/definitions of type `array`
[x] path parameters must be declared a required
[x] headers must not contain $ref
[x] schema and property examples provided must validate against their respective object's schema
[x] examples provided must validate their schema
Reported as warnings:
[x] path parameters should not contain any of [{,},\w]
[x] empty path
[x] unused definitions
[x] unsupported validation of examples on non-JSON media types
[x] examples in response without schema
[x] readOnly properties should not be required
Validating a schema
The schema validation toolkit validates data against JSON-schema-draft 04 schema.
It is tested against the full json-schema-testing-suite (https://github.com/json-schema-org/JSON-Schema-Test-Suite),
except for the optional part (bignum, ECMA regexp, ...).
It supports the complete JSON-schema vocabulary, including keywords not supported by Swagger (e.g. additionalItems, ...)
Entry points:
- AgainstSchema()
- ...
Known limitations
With the current version of this package, the following aspects of swagger are not yet supported:
[ ] errors and warnings are not reported with key/line number in spec
[ ] default values and examples on responses only support application/json producer type
[ ] invalid numeric constraints (such as Minimum, etc..) are not checked except for default and example values
[ ] rules for collectionFormat are not implemented
[ ] no validation rule for polymorphism support (discriminator) [not done here]
[ ] valid js ECMA regexp not supported by Go regexp engine are considered invalid
[ ] arbitrary large numbers are not supported: max is math.MaxFloat64
*/
package validate

View File

@@ -0,0 +1,299 @@
// Copyright 2015 go-swagger maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package validate
import (
"fmt"
"strings"
"github.com/go-openapi/spec"
)
// ExampleValidator validates example values defined in a spec
type exampleValidator struct {
SpecValidator *SpecValidator
visitedSchemas map[string]bool
}
// resetVisited resets the internal state of visited schemas
func (ex *exampleValidator) resetVisited() {
ex.visitedSchemas = map[string]bool{}
}
// beingVisited asserts a schema is being visited
func (ex *exampleValidator) beingVisited(path string) {
ex.visitedSchemas[path] = true
}
// isVisited tells if a path has already been visited
func (ex *exampleValidator) isVisited(path string) bool {
found := ex.visitedSchemas[path]
if !found {
// search for overlapping paths
frags := strings.Split(path, ".")
if len(frags) < 2 {
// shortcut exit on smaller paths
return found
}
last := len(frags) - 1
var currentFragStr, parent string
for i := range frags {
if i == 0 {
currentFragStr = frags[last]
} else {
currentFragStr = strings.Join([]string{frags[last-i], currentFragStr}, ".")
}
if i < last {
parent = strings.Join(frags[0:last-i], ".")
} else {
parent = ""
}
if strings.HasSuffix(parent, currentFragStr) {
found = true
break
}
}
}
return found
}
// Validate validates the example values declared in the swagger spec
// Example values MUST conform to their schema.
//
// With Swagger 2.0, examples are supported in:
// - schemas
// - individual property
// - responses
//
func (ex *exampleValidator) Validate() (errs *Result) {
errs = new(Result)
if ex == nil || ex.SpecValidator == nil {
return errs
}
ex.resetVisited()
errs.Merge(ex.validateExampleValueValidAgainstSchema()) // error -
return errs
}
func (ex *exampleValidator) validateExampleValueValidAgainstSchema() *Result {
// every example value that is specified must validate against the schema for that property
// in: schemas, properties, object, items
// not in: headers, parameters without schema
res := new(Result)
s := ex.SpecValidator
for method, pathItem := range s.analyzer.Operations() {
if pathItem != nil { // Safeguard
for path, op := range pathItem {
// parameters
for _, param := range paramHelp.safeExpandedParamsFor(path, method, op.ID, res, s) {
// As of swagger 2.0, Examples are not supported in simple parameters
// However, it looks like it is supported by go-openapi
// reset explored schemas to get depth-first recursive-proof exploration
ex.resetVisited()
// Check simple parameters first
// default values provided must validate against their inline definition (no explicit schema)
if param.Example != nil && param.Schema == nil {
// check param default value is valid
red := NewParamValidator(&param, s.KnownFormats).Validate(param.Example)
if red.HasErrorsOrWarnings() {
res.AddWarnings(exampleValueDoesNotValidateMsg(param.Name, param.In))
res.MergeAsWarnings(red)
}
}
// Recursively follows Items and Schemas
if param.Items != nil {
red := ex.validateExampleValueItemsAgainstSchema(param.Name, param.In, &param, param.Items)
if red.HasErrorsOrWarnings() {
res.AddWarnings(exampleValueItemsDoesNotValidateMsg(param.Name, param.In))
res.Merge(red)
}
}
if param.Schema != nil {
// Validate example value against schema
red := ex.validateExampleValueSchemaAgainstSchema(param.Name, param.In, param.Schema)
if red.HasErrorsOrWarnings() {
res.AddWarnings(exampleValueDoesNotValidateMsg(param.Name, param.In))
res.Merge(red)
}
}
}
if op.Responses != nil {
if op.Responses.Default != nil {
// Same constraint on default Response
res.Merge(ex.validateExampleInResponse(op.Responses.Default, "default", path, 0, op.ID))
}
// Same constraint on regular Responses
if op.Responses.StatusCodeResponses != nil { // Safeguard
for code, r := range op.Responses.StatusCodeResponses {
res.Merge(ex.validateExampleInResponse(&r, "response", path, code, op.ID))
}
}
} else {
// Empty op.ID means there is no meaningful operation: no need to report a specific message
if op.ID != "" {
res.AddErrors(noValidResponseMsg(op.ID))
}
}
}
}
}
if s.spec.Spec().Definitions != nil { // Safeguard
// reset explored schemas to get depth-first recursive-proof exploration
ex.resetVisited()
for nm, sch := range s.spec.Spec().Definitions {
res.Merge(ex.validateExampleValueSchemaAgainstSchema(fmt.Sprintf("definitions.%s", nm), "body", &sch))
}
}
return res
}
func (ex *exampleValidator) validateExampleInResponse(resp *spec.Response, responseType, path string, responseCode int, operationID string) *Result {
s := ex.SpecValidator
response, res := responseHelp.expandResponseRef(resp, path, s)
if !res.IsValid() { // Safeguard
return res
}
responseName, responseCodeAsStr := responseHelp.responseMsgVariants(responseType, responseCode)
if response.Headers != nil { // Safeguard
for nm, h := range response.Headers {
// reset explored schemas to get depth-first recursive-proof exploration
ex.resetVisited()
if h.Example != nil {
red := NewHeaderValidator(nm, &h, s.KnownFormats).Validate(h.Example)
if red.HasErrorsOrWarnings() {
res.AddWarnings(exampleValueHeaderDoesNotValidateMsg(operationID, nm, responseName))
res.MergeAsWarnings(red)
}
}
// Headers have inline definition, like params
if h.Items != nil {
red := ex.validateExampleValueItemsAgainstSchema(nm, "header", &h, h.Items)
if red.HasErrorsOrWarnings() {
res.AddWarnings(exampleValueHeaderItemsDoesNotValidateMsg(operationID, nm, responseName))
res.MergeAsWarnings(red)
}
}
if _, err := compileRegexp(h.Pattern); err != nil {
res.AddErrors(invalidPatternInHeaderMsg(operationID, nm, responseName, h.Pattern, err))
}
// Headers don't have schema
}
}
if response.Schema != nil {
// reset explored schemas to get depth-first recursive-proof exploration
ex.resetVisited()
red := ex.validateExampleValueSchemaAgainstSchema(responseCodeAsStr, "response", response.Schema)
if red.HasErrorsOrWarnings() {
// Additional message to make sure the context of the error is not lost
res.AddWarnings(exampleValueInDoesNotValidateMsg(operationID, responseName))
res.Merge(red)
}
}
if response.Examples != nil {
if response.Schema != nil {
if example, ok := response.Examples["application/json"]; ok {
res.MergeAsWarnings(NewSchemaValidator(response.Schema, s.spec.Spec(), path, s.KnownFormats).Validate(example))
} else {
// TODO: validate other media types too
res.AddWarnings(examplesMimeNotSupportedMsg(operationID, responseName))
}
} else {
res.AddWarnings(examplesWithoutSchemaMsg(operationID, responseName))
}
}
return res
}
func (ex *exampleValidator) validateExampleValueSchemaAgainstSchema(path, in string, schema *spec.Schema) *Result {
if schema == nil || ex.isVisited(path) {
// Avoids recursing if we are already done with that check
return nil
}
ex.beingVisited(path)
s := ex.SpecValidator
res := new(Result)
if schema.Example != nil {
res.MergeAsWarnings(NewSchemaValidator(schema, s.spec.Spec(), path+".example", s.KnownFormats).Validate(schema.Example))
}
if schema.Items != nil {
if schema.Items.Schema != nil {
res.Merge(ex.validateExampleValueSchemaAgainstSchema(path+".items.example", in, schema.Items.Schema))
}
// Multiple schemas in items
if schema.Items.Schemas != nil { // Safeguard
for i, sch := range schema.Items.Schemas {
res.Merge(ex.validateExampleValueSchemaAgainstSchema(fmt.Sprintf("%s.items[%d].example", path, i), in, &sch))
}
}
}
if _, err := compileRegexp(schema.Pattern); err != nil {
res.AddErrors(invalidPatternInMsg(path, in, schema.Pattern))
}
if schema.AdditionalItems != nil && schema.AdditionalItems.Schema != nil {
// NOTE: we keep validating values, even though additionalItems is unsupported in Swagger 2.0 (and 3.0 as well)
res.Merge(ex.validateExampleValueSchemaAgainstSchema(fmt.Sprintf("%s.additionalItems", path), in, schema.AdditionalItems.Schema))
}
for propName, prop := range schema.Properties {
res.Merge(ex.validateExampleValueSchemaAgainstSchema(path+"."+propName, in, &prop))
}
for propName, prop := range schema.PatternProperties {
res.Merge(ex.validateExampleValueSchemaAgainstSchema(path+"."+propName, in, &prop))
}
if schema.AdditionalProperties != nil && schema.AdditionalProperties.Schema != nil {
res.Merge(ex.validateExampleValueSchemaAgainstSchema(fmt.Sprintf("%s.additionalProperties", path), in, schema.AdditionalProperties.Schema))
}
if schema.AllOf != nil {
for i, aoSch := range schema.AllOf {
res.Merge(ex.validateExampleValueSchemaAgainstSchema(fmt.Sprintf("%s.allOf[%d]", path, i), in, &aoSch))
}
}
return res
}
func (ex *exampleValidator) validateExampleValueItemsAgainstSchema(path, in string, root interface{}, items *spec.Items) *Result {
res := new(Result)
s := ex.SpecValidator
if items != nil {
if items.Example != nil {
res.MergeAsWarnings(newItemsValidator(path, in, items, root, s.KnownFormats).Validate(0, items.Example))
}
if items.Items != nil {
res.Merge(ex.validateExampleValueItemsAgainstSchema(path+"[0].example", in, root, items.Items))
}
if _, err := compileRegexp(items.Pattern); err != nil {
res.AddErrors(invalidPatternInMsg(path, in, items.Pattern))
}
}
return res
}

View File

@@ -15,7 +15,6 @@
package validate
import (
"log"
"reflect"
"github.com/go-openapi/spec"
@@ -48,18 +47,20 @@ func (f *formatValidator) Applies(source interface{}, kind reflect.Kind) bool {
case *spec.Schema:
sch := source.(*spec.Schema)
return kind == reflect.String && f.KnownFormats.ContainsName(sch.Format)
case *spec.Header:
hdr := source.(*spec.Header)
return kind == reflect.String && f.KnownFormats.ContainsName(hdr.Format)
}
return false
}
r := doit()
if Debug {
log.Printf("format validator for %q applies %t for %T (kind: %v)\n", f.Path, r, source, kind)
}
debugLog("format validator for %q applies %t for %T (kind: %v)\n", f.Path, r, source, kind)
return r
}
func (f *formatValidator) Validate(val interface{}) *Result {
result := new(Result)
debugLog("validating \"%v\" against format: %s", val, f.Format)
if err := FormatOf(f.Path, f.In, f.Format, val.(string), f.KnownFormats); err != nil {
result.AddErrors(err)

14
vendor/github.com/go-openapi/validate/go.mod generated vendored Normal file
View File

@@ -0,0 +1,14 @@
module github.com/go-openapi/validate
require (
github.com/go-openapi/analysis v0.17.0
github.com/go-openapi/errors v0.17.0
github.com/go-openapi/jsonpointer v0.17.0
github.com/go-openapi/loads v0.17.0
github.com/go-openapi/runtime v0.0.0-20180920151709-4f900dc2ade9
github.com/go-openapi/spec v0.17.0
github.com/go-openapi/strfmt v0.17.0
github.com/go-openapi/swag v0.17.0
github.com/stretchr/testify v1.2.2
gopkg.in/yaml.v2 v2.2.1
)

41
vendor/github.com/go-openapi/validate/go.sum generated vendored Normal file
View File

@@ -0,0 +1,41 @@
github.com/PuerkitoBio/purell v1.1.0 h1:rmGxhojJlM0tuKtfdvliR84CFHljx9ag64t2xmVkjK4=
github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf h1:eg0MeVzsP1G42dRafH3vf+al2vQIJU0YHX+1Tw87oco=
github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/globalsign/mgo v0.0.0-20180905125535-1ca0a4f7cbcb h1:D4uzjWwKYQ5XnAvUbuvHW93esHg7F8N/OYeBBcJoTr0=
github.com/globalsign/mgo v0.0.0-20180905125535-1ca0a4f7cbcb/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q=
github.com/go-openapi/analysis v0.0.0-20180825180245-b006789cd277/go.mod h1:k70tL6pCuVxPJOHXQ+wIac1FUrvNkHolPie/cLEU6hI=
github.com/go-openapi/analysis v0.17.0 h1:8JV+dzJJiK46XqGLqqLav8ZfEiJECp8jlOFhpiCdZ+0=
github.com/go-openapi/analysis v0.17.0/go.mod h1:IowGgpVeD0vNm45So8nr+IcQ3pxVtpRoBWb8PVZO0ik=
github.com/go-openapi/errors v0.17.0 h1:47T+LqPrQUxFXQnB22aLBfsTRFSqWp5y4OiFgQm+/Lw=
github.com/go-openapi/errors v0.17.0/go.mod h1:La0D2x9HoXenv7MDEiAv6vWoe84CXFo0PQRk/jdQlww=
github.com/go-openapi/jsonpointer v0.17.0 h1:Bpl2DtZ6k7wKqfFs7e+4P08+M9I3FQgn09a1UsRUQbk=
github.com/go-openapi/jsonpointer v0.17.0/go.mod h1:+35s3my2LFTysnkMfxsJBAMHj/DoqoB9knIWoYG/Vk0=
github.com/go-openapi/jsonreference v0.17.0 h1:d/o7/fsLWWQZACbihvZxcyLQ59jfUVs7WOJv/ak7T7A=
github.com/go-openapi/jsonreference v0.17.0/go.mod h1:W3Z9FmVs9qj+KR4zFKmDPGiLdk1D9Rlm7cyMvf57TTg=
github.com/go-openapi/loads v0.17.0 h1:H22nMs3GDQk4SwAaFQ+jLNw+0xoFeCueawhZlv8MBYs=
github.com/go-openapi/loads v0.17.0/go.mod h1:72tmFy5wsWx89uEVddd0RjRWPZm92WRLhf7AC+0+OOU=
github.com/go-openapi/runtime v0.0.0-20180920151709-4f900dc2ade9 h1:zXd+rkzHwMIYVTJ/j/v8zUQ9j3Ir32gC5Dn9DzZVvCk=
github.com/go-openapi/runtime v0.0.0-20180920151709-4f900dc2ade9/go.mod h1:6v9a6LTXWQCdL8k1AO3cvqx5OtZY/Y9wKTgaoP6YRfA=
github.com/go-openapi/spec v0.17.0 h1:MM5YaXBdBOEcjGHW5WayrAY5Ze2ydNyy71JHeTi7xUc=
github.com/go-openapi/spec v0.17.0/go.mod h1:J8+jY1nAiCcj+friV/PDoE1/3eeccG9LYBs0tYvLOWc=
github.com/go-openapi/strfmt v0.17.0 h1:79+bCyGHowS3rkr6z8RcG5jVzdKpeKXlDuW6yqE50TM=
github.com/go-openapi/strfmt v0.17.0/go.mod h1:/bCWipNKhC9QMhD8HRe2EGbU8G0D4Yvh0G6X4k1Xwvg=
github.com/go-openapi/swag v0.17.0 h1:7wu+dZ5k83kvUWeAb+WUkFiUhDzwGqzTR/NhWzeo1JU=
github.com/go-openapi/swag v0.17.0/go.mod h1:DXUve3Dpr1UfpPtxFw+EFuQ41HhCWZfha5jSVRG7C7I=
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329 h1:2gxZ0XQIU/5z3Z3bUBu+FXuk2pFbkN6tcwi/pjyaDic=
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/net v0.0.0-20181005035420-146acd28ed58 h1:otZG8yDCO4LVps5+9bxOeNiCvgmOyt96J3roHTYs7oE=
golang.org/x/net v0.0.0-20181005035420-146acd28ed58/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

265
vendor/github.com/go-openapi/validate/helpers.go generated vendored Normal file
View File

@@ -0,0 +1,265 @@
// Copyright 2015 go-swagger maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package validate
// TODO: define this as package validate/internal
// This must be done while keeping CI intact with all tests and test coverage
import (
"reflect"
"strconv"
"strings"
"github.com/go-openapi/errors"
"github.com/go-openapi/spec"
)
const swaggerBody = "body"
const objectType = "object"
// Helpers available at the package level
var (
pathHelp *pathHelper
valueHelp *valueHelper
errorHelp *errorHelper
paramHelp *paramHelper
responseHelp *responseHelper
)
type errorHelper struct {
// A collection of unexported helpers for error construction
}
func (h *errorHelper) sErr(err errors.Error) *Result {
// Builds a Result from standard errors.Error
return &Result{Errors: []error{err}}
}
func (h *errorHelper) addPointerError(res *Result, err error, ref string, fromPath string) *Result {
// Provides more context on error messages
// reported by the jsoinpointer package by altering the passed Result
if err != nil {
res.AddErrors(cannotResolveRefMsg(fromPath, ref, err))
}
return res
}
type pathHelper struct {
// A collection of unexported helpers for path validation
}
func (h *pathHelper) stripParametersInPath(path string) string {
// Returns a path stripped from all path parameters, with multiple or trailing slashes removed.
//
// Stripping is performed on a slash-separated basis, e.g '/a{/b}' remains a{/b} and not /a.
// - Trailing "/" make a difference, e.g. /a/ !~ /a (ex: canary/bitbucket.org/swagger.json)
// - presence or absence of a parameter makes a difference, e.g. /a/{log} !~ /a/ (ex: canary/kubernetes/swagger.json)
// Regexp to extract parameters from path, with surrounding {}.
// NOTE: important non-greedy modifier
rexParsePathParam := mustCompileRegexp(`{[^{}]+?}`)
strippedSegments := []string{}
for _, segment := range strings.Split(path, "/") {
strippedSegments = append(strippedSegments, rexParsePathParam.ReplaceAllString(segment, "X"))
}
return strings.Join(strippedSegments, "/")
}
func (h *pathHelper) extractPathParams(path string) (params []string) {
// Extracts all params from a path, with surrounding "{}"
rexParsePathParam := mustCompileRegexp(`{[^{}]+?}`)
for _, segment := range strings.Split(path, "/") {
for _, v := range rexParsePathParam.FindAllStringSubmatch(segment, -1) {
params = append(params, v...)
}
}
return
}
type valueHelper struct {
// A collection of unexported helpers for value validation
}
func (h *valueHelper) asInt64(val interface{}) int64 {
// Number conversion function for int64, without error checking
// (implements an implicit type upgrade).
v := reflect.ValueOf(val)
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return int64(v.Uint())
case reflect.Float32, reflect.Float64:
return int64(v.Float())
default:
//panic("Non numeric value in asInt64()")
return 0
}
}
func (h *valueHelper) asUint64(val interface{}) uint64 {
// Number conversion function for uint64, without error checking
// (implements an implicit type upgrade).
v := reflect.ValueOf(val)
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return uint64(v.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return v.Uint()
case reflect.Float32, reflect.Float64:
return uint64(v.Float())
default:
//panic("Non numeric value in asUint64()")
return 0
}
}
// Same for unsigned floats
func (h *valueHelper) asFloat64(val interface{}) float64 {
// Number conversion function for float64, without error checking
// (implements an implicit type upgrade).
v := reflect.ValueOf(val)
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return float64(v.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return float64(v.Uint())
case reflect.Float32, reflect.Float64:
return v.Float()
default:
//panic("Non numeric value in asFloat64()")
return 0
}
}
type paramHelper struct {
// A collection of unexported helpers for parameters resolution
}
func (h *paramHelper) safeExpandedParamsFor(path, method, operationID string, res *Result, s *SpecValidator) (params []spec.Parameter) {
operation, ok := s.analyzer.OperationFor(method, path)
if ok {
// expand parameters first if necessary
resolvedParams := []spec.Parameter{}
for _, ppr := range operation.Parameters {
resolvedParam, red := h.resolveParam(path, method, operationID, &ppr, s)
res.Merge(red)
if resolvedParam != nil {
resolvedParams = append(resolvedParams, *resolvedParam)
}
}
// remove params with invalid expansion from Slice
operation.Parameters = resolvedParams
for _, ppr := range s.analyzer.SafeParamsFor(method, path,
func(p spec.Parameter, err error) bool {
// since params have already been expanded, there are few causes for error
res.AddErrors(someParametersBrokenMsg(path, method, operationID))
// original error from analyzer
res.AddErrors(err)
return true
}) {
params = append(params, ppr)
}
}
return
}
func (h *paramHelper) resolveParam(path, method, operationID string, param *spec.Parameter, s *SpecValidator) (*spec.Parameter, *Result) {
// Ensure parameter is expanded
var err error
res := new(Result)
isRef := param.Ref.String() != ""
if s.spec.SpecFilePath() == "" {
err = spec.ExpandParameterWithRoot(param, s.spec.Spec(), nil)
} else {
err = spec.ExpandParameter(param, s.spec.SpecFilePath())
}
if err != nil { // Safeguard
// NOTE: we may enter enter here when the whole parameter is an unresolved $ref
refPath := strings.Join([]string{"\"" + path + "\"", method}, ".")
errorHelp.addPointerError(res, err, param.Ref.String(), refPath)
return nil, res
}
res.Merge(h.checkExpandedParam(param, param.Name, param.In, operationID, isRef))
return param, res
}
func (h *paramHelper) checkExpandedParam(pr *spec.Parameter, path, in, operation string, isRef bool) *Result {
// Secure parameter structure after $ref resolution
res := new(Result)
simpleZero := spec.SimpleSchema{}
// Try to explain why... best guess
if pr.In == swaggerBody && (pr.SimpleSchema != simpleZero && pr.SimpleSchema.Type != objectType) {
if isRef {
// Most likely, a $ref with a sibling is an unwanted situation: in itself this is a warning...
// but we detect it because of the following error:
// schema took over Parameter for an unexplained reason
res.AddWarnings(refShouldNotHaveSiblingsMsg(path, operation))
}
res.AddErrors(invalidParameterDefinitionMsg(path, in, operation))
} else if pr.In != swaggerBody && pr.Schema != nil {
if isRef {
res.AddWarnings(refShouldNotHaveSiblingsMsg(path, operation))
}
res.AddErrors(invalidParameterDefinitionAsSchemaMsg(path, in, operation))
} else if (pr.In == swaggerBody && pr.Schema == nil) ||
(pr.In != swaggerBody && pr.SimpleSchema == simpleZero) { // Safeguard
// Other unexpected mishaps
res.AddErrors(invalidParameterDefinitionMsg(path, in, operation))
}
return res
}
type responseHelper struct {
// A collection of unexported helpers for response resolution
}
func (r *responseHelper) expandResponseRef(
response *spec.Response,
path string, s *SpecValidator) (*spec.Response, *Result) {
// Ensure response is expanded
var err error
res := new(Result)
if s.spec.SpecFilePath() == "" {
// there is no physical document to resolve $ref in response
err = spec.ExpandResponseWithRoot(response, s.spec.Spec(), nil)
} else {
err = spec.ExpandResponse(response, s.spec.SpecFilePath())
}
if err != nil { // Safeguard
// NOTE: we may enter here when the whole response is an unresolved $ref.
errorHelp.addPointerError(res, err, response.Ref.String(), path)
return nil, res
}
return response, res
}
func (r *responseHelper) responseMsgVariants(
responseType string,
responseCode int) (responseName, responseCodeAsStr string) {
// Path variants for messages
if responseType == "default" {
responseCodeAsStr = "default"
responseName = "default response"
} else {
responseCodeAsStr = strconv.Itoa(responseCode)
responseName = "response " + responseCodeAsStr
}
return
}

View File

@@ -15,7 +15,6 @@
package validate
import (
"log"
"reflect"
"regexp"
"strings"
@@ -47,9 +46,7 @@ func (o *objectValidator) Applies(source interface{}, kind reflect.Kind) bool {
// there is a problem in the type validator where it will be unhappy about null values
// so that requires more testing
r := reflect.TypeOf(source) == specSchemaType && (kind == reflect.Map || kind == reflect.Struct)
if Debug {
log.Printf("object validator for %q applies %t for %T (kind: %v)\n", o.Path, r, source, kind)
}
debugLog("object validator for %q applies %t for %T (kind: %v)\n", o.Path, r, source, kind)
return r
}
@@ -57,6 +54,7 @@ func (o *objectValidator) isPropertyName() bool {
p := strings.Split(o.Path, ".")
return p[len(p)-1] == "properties" && p[len(p)-2] != "properties"
}
func (o *objectValidator) checkArrayMustHaveItems(res *Result, val map[string]interface{}) {
if t, typeFound := val["type"]; typeFound {
if tpe, ok := t.(string); ok && tpe == "array" {
@@ -82,26 +80,31 @@ func (o *objectValidator) checkItemsMustBeTypeArray(res *Result, val map[string]
}
}
}
func (o *objectValidator) precheck(res *Result, val map[string]interface{}) {
o.checkArrayMustHaveItems(res, val)
o.checkItemsMustBeTypeArray(res, val)
}
func (o *objectValidator) Validate(data interface{}) *Result {
val := data.(map[string]interface{})
// TODO: guard against nil data
numKeys := int64(len(val))
if o.MinProperties != nil && numKeys < *o.MinProperties {
return sErr(errors.TooFewProperties(o.Path, o.In, *o.MinProperties))
return errorHelp.sErr(errors.TooFewProperties(o.Path, o.In, *o.MinProperties))
}
if o.MaxProperties != nil && numKeys > *o.MaxProperties {
return sErr(errors.TooManyProperties(o.Path, o.In, *o.MaxProperties))
return errorHelp.sErr(errors.TooManyProperties(o.Path, o.In, *o.MaxProperties))
}
res := new(Result)
o.precheck(res, val)
// check validity of field names
if o.AdditionalProperties != nil && !o.AdditionalProperties.Allows {
// Case: additionalProperties: false
for k := range val {
_, regularProperty := o.Properties[k]
matched := false
@@ -112,45 +115,102 @@ func (o *objectValidator) Validate(data interface{}) *Result {
break
}
}
if !regularProperty && k != "$schema" && k != "id" && !matched {
// Special properties "$schema" and "id" are ignored
res.AddErrors(errors.PropertyNotAllowed(o.Path, o.In, k))
// BUG(fredbi): This section should move to a part dedicated to spec validation as
// it will conflict with regular schemas where a property "headers" is defined.
//
// Croaks a more explicit message on top of the standard one
// on some recognized cases.
//
// NOTE: edge cases with invalid type assertion are simply ignored here.
// NOTE: prefix your messages here by "IMPORTANT!" so there are not filtered
// by higher level callers (the IMPORTANT! tag will be eventually
// removed).
switch k {
// $ref is forbidden in header
case "headers":
if val[k] != nil {
if headers, mapOk := val[k].(map[string]interface{}); mapOk {
for headerKey, headerBody := range headers {
if headerBody != nil {
if headerSchema, mapOfMapOk := headerBody.(map[string]interface{}); mapOfMapOk {
if _, found := headerSchema["$ref"]; found {
var msg string
if refString, stringOk := headerSchema["$ref"].(string); stringOk {
msg = strings.Join([]string{", one may not use $ref=\":", refString, "\""}, "")
}
res.AddErrors(refNotAllowedInHeaderMsg(o.Path, headerKey, msg))
}
}
}
}
}
}
/*
case "$ref":
if val[k] != nil {
// TODO: check context of that ref: warn about siblings, check against invalid context
}
*/
}
}
}
} else {
// Cases: no additionalProperties (implying: true), or additionalProperties: true, or additionalProperties: { <<schema>> }
for key, value := range val {
_, regularProperty := o.Properties[key]
// Validates property against "patternProperties" if applicable
// BUG(fredbi): succeededOnce is always false
// NOTE: how about regular properties which do not match patternProperties?
matched, succeededOnce, _ := o.validatePatternProperty(key, value, res)
if !(regularProperty || matched || succeededOnce) {
// Cases: properties which are not regular properties and have not been matched by the PatternProperties validator
if o.AdditionalProperties != nil && o.AdditionalProperties.Schema != nil {
res.Merge(NewSchemaValidator(o.AdditionalProperties.Schema, o.Root, o.Path+"."+key, o.KnownFormats).Validate(value))
// AdditionalProperties as Schema
r := NewSchemaValidator(o.AdditionalProperties.Schema, o.Root, o.Path+"."+key, o.KnownFormats).Validate(value)
res.mergeForField(data.(map[string]interface{}), key, r)
} else if regularProperty && !(matched || succeededOnce) {
// TODO: this is dead code since regularProperty=false here
res.AddErrors(errors.FailedAllPatternProperties(o.Path, o.In, key))
}
}
}
// Valid cases: additionalProperties: true or undefined
}
createdFromDefaults := map[string]bool{}
for pName, pSchema := range o.Properties {
// Property types:
// - regular Property
for pName := range o.Properties {
pSchema := o.Properties[pName] // one instance per iteration
rName := pName
if o.Path != "" {
rName = o.Path + "." + pName
}
// Recursively validates each property against its schema
if v, ok := val[pName]; ok {
r := NewSchemaValidator(&pSchema, o.Root, rName, o.KnownFormats).Validate(v)
res.Merge(r)
res.mergeForField(data.(map[string]interface{}), pName, r)
} else if pSchema.Default != nil {
// If a default value is defined, creates the property from defaults
// NOTE: JSON schema does not enforce default values to be valid against schema. Swagger does.
createdFromDefaults[pName] = true
pName := pName // shaddow
def := pSchema.Default
res.Defaulters = append(res.Defaulters, DefaulterFunc(func() {
val[pName] = def
}))
res.addPropertySchemata(data.(map[string]interface{}), pName, &pSchema)
}
}
// Check required properties
if len(o.Required) > 0 {
for _, k := range o.Required {
if _, ok := val[k]; !ok && !createdFromDefaults[k] {
@@ -160,13 +220,16 @@ func (o *objectValidator) Validate(data interface{}) *Result {
}
}
// Check patternProperties
// TODO: it looks like we have done that twice in many cases
for key, value := range val {
_, regularProperty := o.Properties[key]
matched, succeededOnce, patterns := o.validatePatternProperty(key, value, res)
if !regularProperty && (matched || succeededOnce) {
matched, _ /*succeededOnce*/, patterns := o.validatePatternProperty(key, value, res)
if !regularProperty && (matched /*|| succeededOnce*/) {
for _, pName := range patterns {
if v, ok := o.PatternProperties[pName]; ok {
res.Merge(NewSchemaValidator(&v, o.Root, o.Path+"."+key, o.KnownFormats).Validate(value))
r := NewSchemaValidator(&v, o.Root, o.Path+"."+key, o.KnownFormats).Validate(value)
res.mergeForField(data.(map[string]interface{}), key, r)
}
}
}
@@ -174,6 +237,7 @@ func (o *objectValidator) Validate(data interface{}) *Result {
return res
}
// TODO: succeededOnce is not used anywhere
func (o *objectValidator) validatePatternProperty(key string, value interface{}, result *Result) (bool, bool, []string) {
matched := false
succeededOnce := false
@@ -190,9 +254,11 @@ func (o *objectValidator) validatePatternProperty(key string, value interface{},
}
}
if succeededOnce {
result.Inc()
}
// BUG(fredbi): can't get to here. Should remove dead code (commented out).
//if succeededOnce {
// result.Inc()
//}
return matched, succeededOnce, patterns
}

43
vendor/github.com/go-openapi/validate/options.go generated vendored Normal file
View File

@@ -0,0 +1,43 @@
// Copyright 2015 go-swagger maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package validate
import "sync"
// Opts specifies validation options for a SpecValidator.
//
// NOTE: other options might be needed, for example a go-swagger specific mode.
type Opts struct {
ContinueOnErrors bool // true: continue reporting errors, even if spec is invalid
}
var (
defaultOpts = Opts{ContinueOnErrors: false} // default is to stop validation on errors
defaultOptsMutex = &sync.Mutex{}
)
// SetContinueOnErrors sets global default behavior regarding spec validation errors reporting.
//
// For extended error reporting, you most likely want to set it to true.
// For faster validation, it's better to give up early when a spec is detected as invalid: set it to false (this is the default).
//
// Setting this mode does NOT affect the validation status.
//
// NOTE: this method affects global defaults. It is not suitable for a concurrent usage.
func SetContinueOnErrors(c bool) {
defer defaultOptsMutex.Unlock()
defaultOptsMutex.Lock()
defaultOpts.ContinueOnErrors = c
}

View File

@@ -15,66 +15,409 @@
package validate
import (
"os"
"fmt"
"reflect"
"strings"
"github.com/go-openapi/errors"
"github.com/go-openapi/spec"
)
var (
// Debug is true when the SWAGGER_DEBUG env var is not empty
Debug = os.Getenv("SWAGGER_DEBUG") != ""
)
type Defaulter interface {
Apply()
}
type DefaulterFunc func()
func (f DefaulterFunc) Apply() {
f()
}
// Result represents a validation result
// Result represents a validation result set, composed of
// errors and warnings.
//
// It is used to keep track of all detected errors and warnings during
// the validation of a specification.
//
// Matchcount is used to determine
// which errors are relevant in the case of AnyOf, OneOf
// schema validation. Results from the validation branch
// with most matches get eventually selected.
//
// TODO: keep path of key originating the error
type Result struct {
Errors []error
Warnings []error
MatchCount int
Defaulters []Defaulter
// the object data
data interface{}
// Schemata for the root object
rootObjectSchemata schemata
// Schemata for object fields
fieldSchemata []fieldSchemata
// Schemata for slice items
itemSchemata []itemSchemata
cachedFieldSchemta map[FieldKey][]*spec.Schema
cachedItemSchemata map[ItemKey][]*spec.Schema
}
// Merge merges this result with the other one, preserving match counts etc
func (r *Result) Merge(other *Result) *Result {
if other == nil {
return r
// FieldKey is a pair of an object and a field, usable as a key for a map.
type FieldKey struct {
object reflect.Value // actually a map[string]interface{}, but the latter cannot be a key
field string
}
// ItemKey is a pair of a slice and an index, usable as a key for a map.
type ItemKey struct {
slice reflect.Value // actually a []interface{}, but the latter cannot be a key
index int
}
// NewFieldKey returns a pair of an object and field usable as a key of a map.
func NewFieldKey(obj map[string]interface{}, field string) FieldKey {
return FieldKey{object: reflect.ValueOf(obj), field: field}
}
// Object returns the underlying object of this key.
func (fk *FieldKey) Object() map[string]interface{} {
return fk.object.Interface().(map[string]interface{})
}
// Field returns the underlying field of this key.
func (fk *FieldKey) Field() string {
return fk.field
}
// NewItemKey returns a pair of a slice and index usable as a key of a map.
func NewItemKey(slice interface{}, i int) ItemKey {
return ItemKey{slice: reflect.ValueOf(slice), index: i}
}
// Slice returns the underlying slice of this key.
func (ik *ItemKey) Slice() []interface{} {
return ik.slice.Interface().([]interface{})
}
// Index returns the underlying index of this key.
func (ik *ItemKey) Index() int {
return ik.index
}
type fieldSchemata struct {
obj map[string]interface{}
field string
schemata schemata
}
type itemSchemata struct {
slice reflect.Value
index int
schemata schemata
}
// Merge merges this result with the other one(s), preserving match counts etc.
func (r *Result) Merge(others ...*Result) *Result {
for _, other := range others {
if other == nil {
continue
}
r.mergeWithoutRootSchemata(other)
r.rootObjectSchemata.Append(other.rootObjectSchemata)
}
r.AddErrors(other.Errors...)
r.MatchCount += other.MatchCount
r.Defaulters = append(r.Defaulters, other.Defaulters...)
return r
}
// AddErrors adds errors to this validation result
func (r *Result) AddErrors(errors ...error) {
// TODO: filter already existing errors
r.Errors = append(r.Errors, errors...)
// Data returns the original data object used for validation. Mutating this renders
// the result invalid.
func (r *Result) Data() interface{} {
return r.data
}
// IsValid returns true when this result is valid
// RootObjectSchemata returns the schemata which apply to the root object.
func (r *Result) RootObjectSchemata() []*spec.Schema {
return r.rootObjectSchemata.Slice()
}
// FieldSchemata returns the schemata which apply to fields in objects.
func (r *Result) FieldSchemata() map[FieldKey][]*spec.Schema {
if r.cachedFieldSchemta != nil {
return r.cachedFieldSchemta
}
ret := make(map[FieldKey][]*spec.Schema, len(r.fieldSchemata))
for _, fs := range r.fieldSchemata {
key := NewFieldKey(fs.obj, fs.field)
if fs.schemata.one != nil {
ret[key] = append(ret[key], fs.schemata.one)
} else if len(fs.schemata.multiple) > 0 {
ret[key] = append(ret[key], fs.schemata.multiple...)
}
}
r.cachedFieldSchemta = ret
return ret
}
// ItemSchemata returns the schemata which apply to items in slices.
func (r *Result) ItemSchemata() map[ItemKey][]*spec.Schema {
if r.cachedItemSchemata != nil {
return r.cachedItemSchemata
}
ret := make(map[ItemKey][]*spec.Schema, len(r.itemSchemata))
for _, ss := range r.itemSchemata {
key := NewItemKey(ss.slice, ss.index)
if ss.schemata.one != nil {
ret[key] = append(ret[key], ss.schemata.one)
} else if len(ss.schemata.multiple) > 0 {
ret[key] = append(ret[key], ss.schemata.multiple...)
}
}
r.cachedItemSchemata = ret
return ret
}
func (r *Result) resetCaches() {
r.cachedFieldSchemta = nil
r.cachedItemSchemata = nil
}
// mergeForField merges other into r, assigning other's root schemata to the given Object and field name.
func (r *Result) mergeForField(obj map[string]interface{}, field string, other *Result) *Result {
if other == nil {
return r
}
r.mergeWithoutRootSchemata(other)
if other.rootObjectSchemata.Len() > 0 {
if r.fieldSchemata == nil {
r.fieldSchemata = make([]fieldSchemata, len(obj))
}
r.fieldSchemata = append(r.fieldSchemata, fieldSchemata{
obj: obj,
field: field,
schemata: other.rootObjectSchemata,
})
}
return r
}
// mergeForSlice merges other into r, assigning other's root schemata to the given slice and index.
func (r *Result) mergeForSlice(slice reflect.Value, i int, other *Result) *Result {
if other == nil {
return r
}
r.mergeWithoutRootSchemata(other)
if other.rootObjectSchemata.Len() > 0 {
if r.itemSchemata == nil {
r.itemSchemata = make([]itemSchemata, slice.Len())
}
r.itemSchemata = append(r.itemSchemata, itemSchemata{
slice: slice,
index: i,
schemata: other.rootObjectSchemata,
})
}
return r
}
// addRootObjectSchemata adds the given schemata for the root object of the result.
// The slice schemata might be reused. I.e. do not modify it after being added to a result.
func (r *Result) addRootObjectSchemata(s *spec.Schema) {
r.rootObjectSchemata.Append(schemata{one: s})
}
// addPropertySchemata adds the given schemata for the object and field.
// The slice schemata might be reused. I.e. do not modify it after being added to a result.
func (r *Result) addPropertySchemata(obj map[string]interface{}, fld string, schema *spec.Schema) {
if r.fieldSchemata == nil {
r.fieldSchemata = make([]fieldSchemata, 0, len(obj))
}
r.fieldSchemata = append(r.fieldSchemata, fieldSchemata{obj: obj, field: fld, schemata: schemata{one: schema}})
}
// addSliceSchemata adds the given schemata for the slice and index.
// The slice schemata might be reused. I.e. do not modify it after being added to a result.
func (r *Result) addSliceSchemata(slice reflect.Value, i int, schema *spec.Schema) {
if r.itemSchemata == nil {
r.itemSchemata = make([]itemSchemata, 0, slice.Len())
}
r.itemSchemata = append(r.itemSchemata, itemSchemata{slice: slice, index: i, schemata: schemata{one: schema}})
}
// mergeWithoutRootSchemata merges other into r, ignoring the rootObject schemata.
func (r *Result) mergeWithoutRootSchemata(other *Result) {
r.resetCaches()
r.AddErrors(other.Errors...)
r.AddWarnings(other.Warnings...)
r.MatchCount += other.MatchCount
if other.fieldSchemata != nil {
if r.fieldSchemata == nil {
r.fieldSchemata = other.fieldSchemata
} else {
for _, x := range other.fieldSchemata {
r.fieldSchemata = append(r.fieldSchemata, x)
}
}
}
if other.itemSchemata != nil {
if r.itemSchemata == nil {
r.itemSchemata = other.itemSchemata
} else {
for _, x := range other.itemSchemata {
r.itemSchemata = append(r.itemSchemata, x)
}
}
}
}
// MergeAsErrors merges this result with the other one(s), preserving match counts etc.
//
// Warnings from input are merged as Errors in the returned merged Result.
func (r *Result) MergeAsErrors(others ...*Result) *Result {
for _, other := range others {
if other != nil {
r.resetCaches()
r.AddErrors(other.Errors...)
r.AddErrors(other.Warnings...)
r.MatchCount += other.MatchCount
}
}
return r
}
// MergeAsWarnings merges this result with the other one(s), preserving match counts etc.
//
// Errors from input are merged as Warnings in the returned merged Result.
func (r *Result) MergeAsWarnings(others ...*Result) *Result {
for _, other := range others {
if other != nil {
r.resetCaches()
r.AddWarnings(other.Errors...)
r.AddWarnings(other.Warnings...)
r.MatchCount += other.MatchCount
}
}
return r
}
// AddErrors adds errors to this validation result (if not already reported).
//
// Since the same check may be passed several times while exploring the
// spec structure (via $ref, ...) reported messages are kept
// unique.
func (r *Result) AddErrors(errors ...error) {
for _, e := range errors {
found := false
if e != nil {
for _, isReported := range r.Errors {
if e.Error() == isReported.Error() {
found = true
break
}
}
if !found {
r.Errors = append(r.Errors, e)
}
}
}
}
// AddWarnings adds warnings to this validation result (if not already reported).
func (r *Result) AddWarnings(warnings ...error) {
for _, e := range warnings {
found := false
if e != nil {
for _, isReported := range r.Warnings {
if e.Error() == isReported.Error() {
found = true
break
}
}
if !found {
r.Warnings = append(r.Warnings, e)
}
}
}
}
func (r *Result) keepRelevantErrors() *Result {
// TODO: this one is going to disapear...
// keepRelevantErrors strips a result from standard errors and keeps
// the ones which are supposedly more accurate.
//
// The original result remains unaffected (creates a new instance of Result).
// This method is used to work around the "matchCount" filter which would otherwise
// strip our result from some accurate error reporting from lower level validators.
//
// NOTE: this implementation with a placeholder (IMPORTANT!) is neither clean nor
// very efficient. On the other hand, relying on go-openapi/errors to manipulate
// codes would require to change a lot here. So, for the moment, let's go with
// placeholders.
strippedErrors := []error{}
for _, e := range r.Errors {
if strings.HasPrefix(e.Error(), "IMPORTANT!") {
strippedErrors = append(strippedErrors, fmt.Errorf(strings.TrimPrefix(e.Error(), "IMPORTANT!")))
}
}
strippedWarnings := []error{}
for _, e := range r.Warnings {
if strings.HasPrefix(e.Error(), "IMPORTANT!") {
strippedWarnings = append(strippedWarnings, fmt.Errorf(strings.TrimPrefix(e.Error(), "IMPORTANT!")))
}
}
strippedResult := new(Result)
strippedResult.Errors = strippedErrors
strippedResult.Warnings = strippedWarnings
return strippedResult
}
// IsValid returns true when this result is valid.
//
// Returns true on a nil *Result.
func (r *Result) IsValid() bool {
if r == nil {
return true
}
return len(r.Errors) == 0
}
// HasErrors returns true when this result is invalid
// HasErrors returns true when this result is invalid.
//
// Returns false on a nil *Result.
func (r *Result) HasErrors() bool {
if r == nil {
return false
}
return !r.IsValid()
}
// HasWarnings returns true when this result contains warnings.
//
// Returns false on a nil *Result.
func (r *Result) HasWarnings() bool {
if r == nil {
return false
}
return len(r.Warnings) > 0
}
// HasErrorsOrWarnings returns true when this result contains
// either errors or warnings.
//
// Returns false on a nil *Result.
func (r *Result) HasErrorsOrWarnings() bool {
if r == nil {
return false
}
return len(r.Errors) > 0 || len(r.Warnings) > 0
}
// Inc increments the match count
func (r *Result) Inc() {
r.MatchCount++
}
// AsError renders this result as an error interface
//
// TODO: reporting / pretty print with path ordered and indented
func (r *Result) AsError() error {
if r.IsValid() {
return nil
@@ -82,8 +425,60 @@ func (r *Result) AsError() error {
return errors.CompositeValidationError(r.Errors...)
}
func (r *Result) ApplyDefaults() {
for _, d := range r.Defaulters {
d.Apply()
// schemata is an arbitrary number of schemata. It does a distinction between zero,
// one and many schemata to avoid slice allocations.
type schemata struct {
// one is set if there is exactly one schema. In that case multiple must be nil.
one *spec.Schema
// multiple is an arbitrary number of schemas. If it is set, one must be nil.
multiple []*spec.Schema
}
func (s *schemata) Len() int {
if s.one != nil {
return 1
}
return len(s.multiple)
}
func (s *schemata) Slice() []*spec.Schema {
if s == nil {
return nil
}
if s.one != nil {
return []*spec.Schema{s.one}
}
return s.multiple
}
// appendSchemata appends the schemata in other to s. It mutated s in-place.
func (s *schemata) Append(other schemata) {
if other.one == nil && len(other.multiple) == 0 {
return
}
if s.one == nil && len(s.multiple) == 0 {
*s = other
return
}
if s.one != nil {
if other.one != nil {
s.multiple = []*spec.Schema{s.one, other.one}
} else {
t := make([]*spec.Schema, 0, 1+len(other.multiple))
s.multiple = append(append(t, s.one), other.multiple...)
}
s.one = nil
} else {
if other.one != nil {
s.multiple = append(s.multiple, other.one)
} else {
if cap(s.multiple) >= len(s.multiple)+len(other.multiple) {
s.multiple = append(s.multiple, other.multiple...)
} else {
t := make([]*spec.Schema, 0, len(s.multiple)+len(other.multiple))
s.multiple = append(append(t, s.multiple...), other.multiple...)
}
}
}
}

71
vendor/github.com/go-openapi/validate/rexp.go generated vendored Normal file
View File

@@ -0,0 +1,71 @@
// Copyright 2015 go-swagger maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package validate
import (
re "regexp"
"sync"
"sync/atomic"
)
// Cache for compiled regular expressions
var (
cacheMutex = &sync.Mutex{}
reDict = atomic.Value{} //map[string]*re.Regexp
)
func compileRegexp(pattern string) (*re.Regexp, error) {
if cache, ok := reDict.Load().(map[string]*re.Regexp); ok {
if r := cache[pattern]; r != nil {
return r, nil
}
}
r, err := re.Compile(pattern)
if err != nil {
return nil, err
}
cacheRegexp(r)
return r, nil
}
func mustCompileRegexp(pattern string) *re.Regexp {
if cache, ok := reDict.Load().(map[string]*re.Regexp); ok {
if r := cache[pattern]; r != nil {
return r
}
}
r := re.MustCompile(pattern)
cacheRegexp(r)
return r
}
func cacheRegexp(r *re.Regexp) {
cacheMutex.Lock()
defer cacheMutex.Unlock()
if cache, ok := reDict.Load().(map[string]*re.Regexp); !ok || cache[r.String()] == nil {
newCache := map[string]*re.Regexp{
r.String(): r,
}
for k, v := range cache {
newCache[k] = v
}
reDict.Store(newCache)
}
}

View File

@@ -16,20 +16,22 @@ package validate
import (
"encoding/json"
"log"
"reflect"
"github.com/go-openapi/errors"
"github.com/go-openapi/spec"
"github.com/go-openapi/strfmt"
"github.com/go-openapi/swag"
)
var specSchemaType = reflect.TypeOf(&spec.Schema{})
var specParameterType = reflect.TypeOf(&spec.Parameter{})
var specItemsType = reflect.TypeOf(&spec.Items{})
var specHeaderType = reflect.TypeOf(&spec.Header{})
var (
specSchemaType = reflect.TypeOf(&spec.Schema{})
specParameterType = reflect.TypeOf(&spec.Parameter{})
specItemsType = reflect.TypeOf(&spec.Items{})
specHeaderType = reflect.TypeOf(&spec.Header{})
)
// SchemaValidator like param validator but for a full json schema
// SchemaValidator validates data against a JSON schema
type SchemaValidator struct {
Path string
in string
@@ -39,7 +41,20 @@ type SchemaValidator struct {
KnownFormats strfmt.Registry
}
// NewSchemaValidator creates a new schema validator
// AgainstSchema validates the specified data against the provided schema, using a registry of supported formats.
//
// When no pre-parsed *spec.Schema structure is provided, it uses a JSON schema as default. See example.
func AgainstSchema(schema *spec.Schema, data interface{}, formats strfmt.Registry) error {
res := NewSchemaValidator(schema, nil, "", formats).Validate(data)
if res.HasErrors() {
return errors.CompositeValidationError(res.Errors...)
}
return nil
}
// NewSchemaValidator creates a new schema validator.
//
// Panics if the provided schema is invalid.
func NewSchemaValidator(schema *spec.Schema, rootSchema interface{}, root string, formats strfmt.Registry) *SchemaValidator {
if schema == nil {
return nil
@@ -52,7 +67,8 @@ func NewSchemaValidator(schema *spec.Schema, rootSchema interface{}, root string
if schema.ID != "" || schema.Ref.String() != "" || schema.Ref.IsRoot() {
err := spec.ExpandSchema(schema, rootSchema, nil)
if err != nil {
panic(err)
msg := invalidSchemaProvidedMsg(err).Error()
panic(msg)
}
}
s := SchemaValidator{Path: root, in: "body", Schema: schema, Root: rootSchema, KnownFormats: formats}
@@ -82,15 +98,18 @@ func (s *SchemaValidator) Applies(source interface{}, kind reflect.Kind) bool {
// Validate validates the data against the schema
func (s *SchemaValidator) Validate(data interface{}) *Result {
result := new(Result)
result := &Result{data: data}
if s == nil {
return result
}
if s.Schema != nil {
result.addRootObjectSchemata(s.Schema)
}
if data == nil {
v := s.validators[0].Validate(data)
v.Merge(s.validators[6].Validate(data))
return v
result.Merge(s.validators[0].Validate(data)) // type validator
result.Merge(s.validators[6].Validate(data)) // common validator
return result
}
tpe := reflect.TypeOf(data)
@@ -100,16 +119,23 @@ func (s *SchemaValidator) Validate(data interface{}) *Result {
kind = tpe.Kind()
}
d := data
if kind == reflect.Struct {
// NOTE: since reflect retrieves the true nature of types
// this means that all strfmt types passed here (e.g. strfmt.Datetime, etc..)
// are converted here to strings, and structs are systematically converted
// to map[string]interface{}.
d = swag.ToDynamicJSON(data)
}
// TODO: this part should be handed over to type validator
// Handle special case of json.Number data (number marshalled as string)
isnumber := s.Schema.Type.Contains("number") || s.Schema.Type.Contains("integer")
if num, ok := data.(json.Number); ok && isnumber {
if s.Schema.Type.Contains("integer") { // avoid lossy conversion
in, erri := num.Int64()
if erri != nil {
result.AddErrors(erri)
result.AddErrors(invalidTypeConversionMsg(s.Path, erri))
result.Inc()
return result
}
@@ -117,7 +143,7 @@ func (s *SchemaValidator) Validate(data interface{}) *Result {
} else {
nf, errf := num.Float64()
if errf != nil {
result.AddErrors(errf)
result.AddErrors(invalidTypeConversionMsg(s.Path, errf))
result.Inc()
return result
}
@@ -130,9 +156,7 @@ func (s *SchemaValidator) Validate(data interface{}) *Result {
for _, v := range s.validators {
if !v.Applies(s.Schema, kind) {
if Debug {
log.Printf("%T does not apply for %v", v, kind)
}
debugLog("%T does not apply for %v", v, kind)
continue
}
@@ -141,6 +165,7 @@ func (s *SchemaValidator) Validate(data interface{}) *Result {
result.Inc()
}
result.Inc()
return result
}

View File

@@ -0,0 +1,78 @@
// Copyright 2015 go-swagger maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package validate
import (
"github.com/go-openapi/errors"
)
// Error messages related to schema validation and returned as results.
const (
// ArrayDoesNotAllowAdditionalItemsError when an additionalItems construct is not verified by the array values provided.
//
// TODO: should move to package go-openapi/errors
ArrayDoesNotAllowAdditionalItemsError = "array doesn't allow for additional items"
// HasDependencyError indicates that a dependencies construct was not verified
HasDependencyError = "%q has a dependency on %s"
// InvalidSchemaProvidedError indicates that the schema provided to validate a value cannot be properly compiled
InvalidSchemaProvidedError = "Invalid schema provided to SchemaValidator: %v"
// InvalidTypeConversionError indicates that a numerical conversion for the given type could not be carried on
InvalidTypeConversionError = "invalid type conversion in %s: %v "
// MustValidateAtLeastOneSchemaError indicates that in a AnyOf construct, none of the schema constraints specified were verified
MustValidateAtLeastOneSchemaError = "%q must validate at least one schema (anyOf)"
// MustValidateOnlyOneSchemaError indicates that in a OneOf construct, either none of the schema constraints specified were verified, or several were
MustValidateOnlyOneSchemaError = "%q must validate one and only one schema (oneOf). %s"
// MustValidateAllSchemasError indicates that in a AllOf construct, at least one of the schema constraints specified were not verified
//
// TODO: punctuation in message
MustValidateAllSchemasError = "%q must validate all the schemas (allOf)%s"
// MustNotValidateSchemaError indicates that in a Not construct, the schema constraint specified was verified
MustNotValidateSchemaError = "%q must not validate the schema (not)"
)
// Warning messages related to schema validation and returned as results
const ()
func invalidSchemaProvidedMsg(err error) errors.Error {
return errors.New(InternalErrorCode, InvalidSchemaProvidedError, err)
}
func invalidTypeConversionMsg(path string, err error) errors.Error {
return errors.New(errors.CompositeErrorCode, InvalidTypeConversionError, path, err)
}
func mustValidateOnlyOneSchemaMsg(path, additionalMsg string) errors.Error {
return errors.New(errors.CompositeErrorCode, MustValidateOnlyOneSchemaError, path, additionalMsg)
}
func mustValidateAtLeastOneSchemaMsg(path string) errors.Error {
return errors.New(errors.CompositeErrorCode, MustValidateAtLeastOneSchemaError, path)
}
func mustValidateAllSchemasMsg(path, additionalMsg string) errors.Error {
return errors.New(errors.CompositeErrorCode, MustValidateAllSchemasError, path, additionalMsg)
}
func mustNotValidatechemaMsg(path string) errors.Error {
return errors.New(errors.CompositeErrorCode, MustNotValidateSchemaError, path)
}
func hasADependencyMsg(path, depkey string) errors.Error {
return errors.New(errors.CompositeErrorCode, HasDependencyError, path, depkey)
}
func arrayDoesNotAllowAdditionalItemsMsg() errors.Error {
return errors.New(errors.CompositeErrorCode, ArrayDoesNotAllowAdditionalItemsError)
}

View File

@@ -15,10 +15,9 @@
package validate
import (
"log"
"fmt"
"reflect"
"github.com/go-openapi/errors"
"github.com/go-openapi/spec"
"github.com/go-openapi/strfmt"
)
@@ -44,15 +43,15 @@ func (s *schemaPropsValidator) SetPath(path string) {
}
func newSchemaPropsValidator(path string, in string, allOf, oneOf, anyOf []spec.Schema, not *spec.Schema, deps spec.Dependencies, root interface{}, formats strfmt.Registry) *schemaPropsValidator {
var anyValidators []SchemaValidator
anyValidators := make([]SchemaValidator, 0, len(anyOf))
for _, v := range anyOf {
anyValidators = append(anyValidators, *NewSchemaValidator(&v, root, path, formats))
}
var allValidators []SchemaValidator
allValidators := make([]SchemaValidator, 0, len(allOf))
for _, v := range allOf {
allValidators = append(allValidators, *NewSchemaValidator(&v, root, path, formats))
}
var oneValidators []SchemaValidator
oneValidators := make([]SchemaValidator, 0, len(oneOf))
for _, v := range oneOf {
oneValidators = append(oneValidators, *NewSchemaValidator(&v, root, path, formats))
}
@@ -81,35 +80,46 @@ func newSchemaPropsValidator(path string, in string, allOf, oneOf, anyOf []spec.
func (s *schemaPropsValidator) Applies(source interface{}, kind reflect.Kind) bool {
r := reflect.TypeOf(source) == specSchemaType
if Debug {
log.Printf("schema props validator for %q applies %t for %T (kind: %v)\n", s.Path, r, source, kind)
}
debugLog("schema props validator for %q applies %t for %T (kind: %v)\n", s.Path, r, source, kind)
return r
}
func (s *schemaPropsValidator) Validate(data interface{}) *Result {
mainResult := new(Result)
// Intermediary error results
// IMPORTANT! messages from underlying validators
keepResultAnyOf := new(Result)
keepResultOneOf := new(Result)
keepResultAllOf := new(Result)
// Validates at least one in anyOf schemas
var firstSuccess *Result
if len(s.anyOfValidators) > 0 {
var bestFailures *Result
succeededOnce := false
for _, anyOfSchema := range s.anyOfValidators {
result := anyOfSchema.Validate(data)
// We keep inner IMPORTANT! errors no matter what MatchCount tells us
keepResultAnyOf.Merge(result.keepRelevantErrors())
if result.IsValid() {
bestFailures = nil
succeededOnce = true
if firstSuccess == nil {
firstSuccess = result
}
keepResultAnyOf = new(Result)
break
}
// MatchCount is used to select errors from the schema with most positive checks
if bestFailures == nil || result.MatchCount > bestFailures.MatchCount {
bestFailures = result
}
}
if !succeededOnce {
mainResult.AddErrors(errors.New(422, "must validate at least one schema (anyOf)"))
mainResult.AddErrors(mustValidateAtLeastOneSchemaMsg(s.Path))
}
if bestFailures != nil {
mainResult.Merge(bestFailures)
@@ -118,6 +128,7 @@ func (s *schemaPropsValidator) Validate(data interface{}) *Result {
}
}
// Validates exactly one in oneOf schemas
if len(s.oneOfValidators) > 0 {
var bestFailures *Result
var firstSuccess *Result
@@ -125,21 +136,32 @@ func (s *schemaPropsValidator) Validate(data interface{}) *Result {
for _, oneOfSchema := range s.oneOfValidators {
result := oneOfSchema.Validate(data)
// We keep inner IMPORTANT! errors no matter what MatchCount tells us
keepResultOneOf.Merge(result.keepRelevantErrors())
if result.IsValid() {
validated++
bestFailures = nil
if firstSuccess == nil {
firstSuccess = result
}
keepResultOneOf = new(Result)
continue
}
// MatchCount is used to select errors from the schema with most positive checks
if validated == 0 && (bestFailures == nil || result.MatchCount > bestFailures.MatchCount) {
bestFailures = result
}
}
if validated != 1 {
mainResult.AddErrors(errors.New(422, "must validate one and only one schema (oneOf)"))
additionalMsg := ""
if validated == 0 {
additionalMsg = "Found none valid"
} else {
additionalMsg = fmt.Sprintf("Found %d valid alternatives", validated)
}
mainResult.AddErrors(mustValidateOnlyOneSchemaMsg(s.Path, additionalMsg))
if bestFailures != nil {
mainResult.Merge(bestFailures)
}
@@ -148,11 +170,15 @@ func (s *schemaPropsValidator) Validate(data interface{}) *Result {
}
}
// Validates all of allOf schemas
if len(s.allOfValidators) > 0 {
validated := 0
for _, allOfSchema := range s.allOfValidators {
result := allOfSchema.Validate(data)
// We keep inner IMPORTANT! errors no matter what MatchCount tells us
keepResultAllOf.Merge(result.keepRelevantErrors())
//keepResultAllOf.Merge(result)
if result.IsValid() {
validated++
}
@@ -160,14 +186,20 @@ func (s *schemaPropsValidator) Validate(data interface{}) *Result {
}
if validated != len(s.allOfValidators) {
mainResult.AddErrors(errors.New(422, "must validate all the schemas (allOf)"))
additionalMsg := ""
if validated == 0 {
additionalMsg = ". None validated"
}
mainResult.AddErrors(mustValidateAllSchemasMsg(s.Path, additionalMsg))
}
}
if s.notValidator != nil {
result := s.notValidator.Validate(data)
// We keep inner IMPORTANT! errors no matter what MatchCount tells us
if result.IsValid() {
mainResult.AddErrors(errors.New(422, "must not validate the schema (not)"))
mainResult.AddErrors(mustNotValidatechemaMsg(s.Path))
}
}
@@ -184,7 +216,7 @@ func (s *schemaPropsValidator) Validate(data interface{}) *Result {
if len(dep.Property) > 0 {
for _, depKey := range dep.Property {
if _, ok := val[depKey]; !ok {
mainResult.AddErrors(errors.New(422, "has a dependency on %s", depKey))
mainResult.AddErrors(hasADependencyMsg(s.Path, depKey))
}
}
}
@@ -193,5 +225,7 @@ func (s *schemaPropsValidator) Validate(data interface{}) *Result {
}
mainResult.Inc()
return mainResult
// In the end we retain best failures for schema validation
// plus, if any, composite errors which may explain special cases (tagged as IMPORTANT!).
return mainResult.Merge(keepResultAllOf, keepResultOneOf, keepResultAnyOf)
}

View File

@@ -18,7 +18,6 @@ import (
"fmt"
"reflect"
"github.com/go-openapi/errors"
"github.com/go-openapi/spec"
"github.com/go-openapi/strfmt"
)
@@ -58,30 +57,29 @@ func (s *schemaSliceValidator) Validate(data interface{}) *Result {
for i := 0; i < size; i++ {
validator.SetPath(fmt.Sprintf("%s.%d", s.Path, i))
value := val.Index(i)
result.Merge(validator.Validate(value.Interface()))
result.mergeForSlice(val, i, validator.Validate(value.Interface()))
}
}
itemsSize := int64(0)
itemsSize := 0
if s.Items != nil && len(s.Items.Schemas) > 0 {
itemsSize = int64(len(s.Items.Schemas))
for i := int64(0); i < itemsSize; i++ {
itemsSize = len(s.Items.Schemas)
for i := 0; i < itemsSize; i++ {
validator := NewSchemaValidator(&s.Items.Schemas[i], s.Root, fmt.Sprintf("%s.%d", s.Path, i), s.KnownFormats)
if val.Len() <= int(i) {
if val.Len() <= i {
break
}
result.Merge(validator.Validate(val.Index(int(i)).Interface()))
result.mergeForSlice(val, int(i), validator.Validate(val.Index(i).Interface()))
}
}
if s.AdditionalItems != nil && itemsSize < int64(size) {
if s.AdditionalItems != nil && itemsSize < size {
if s.Items != nil && len(s.Items.Schemas) > 0 && !s.AdditionalItems.Allows {
result.AddErrors(errors.New(422, "array doesn't allow for additional items"))
result.AddErrors(arrayDoesNotAllowAdditionalItemsMsg())
}
if s.AdditionalItems.Schema != nil {
for i := itemsSize; i < (int64(size)-itemsSize)+1; i++ {
for i := itemsSize; i < size-itemsSize+1; i++ {
validator := NewSchemaValidator(s.AdditionalItems.Schema, s.Root, fmt.Sprintf("%s.%d", s.Path, i), s.KnownFormats)
result.Merge(validator.Validate(val.Index(int(i)).Interface()))
result.mergeForSlice(val, int(i), validator.Validate(val.Index(int(i)).Interface()))
}
}
}

File diff suppressed because it is too large Load Diff

354
vendor/github.com/go-openapi/validate/spec_messages.go generated vendored Normal file
View File

@@ -0,0 +1,354 @@
// Copyright 2015 go-swagger maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package validate
import (
"net/http"
"github.com/go-openapi/errors"
)
// Error messages related to spec validation and returned as results.
const (
// ArrayRequiresItemsError ...
ArrayRequiresItemsError = "%s for %q is a collection without an element type (array requires items definition)"
// ArrayInParamRequiresItemsError ...
ArrayInParamRequiresItemsError = "param %q for %q is a collection without an element type (array requires item definition)"
// ArrayInHeaderRequiresItemsError ...
ArrayInHeaderRequiresItemsError = "header %q for %q is a collection without an element type (array requires items definition)"
// BothFormDataAndBodyError indicates that an operation specifies both a body and a formData parameter, which is forbidden
BothFormDataAndBodyError = "operation %q has both formData and body parameters. Only one such In: type may be used for a given operation"
// CannotResolveRefError when a $ref could not be resolved
CannotResolveReferenceError = "could not resolve reference in %s to $ref %s: %v"
// CircularAncestryDefinitionError ...
CircularAncestryDefinitionError = "definition %q has circular ancestry: %v"
// DefaultValueDoesNotValidateError results from an invalid default value provided
DefaultValueDoesNotValidateError = "default value for %s in %s does not validate its schema"
// DefaultValueItemsDoesNotValidateError results from an invalid default value provided for Items
DefaultValueItemsDoesNotValidateError = "default value for %s.items in %s does not validate its schema"
// DefaultValueHeaderDoesNotValidateError results from an invalid default value provided in header
DefaultValueHeaderDoesNotValidateError = "in operation %q, default value in header %s for %s does not validate its schema"
// DefaultValueHeaderItemsDoesNotValidateError results from an invalid default value provided in header.items
DefaultValueHeaderItemsDoesNotValidateError = "in operation %q, default value in header.items %s for %s does not validate its schema"
// DefaultValueInDoesNotValidateError ...
DefaultValueInDoesNotValidateError = "in operation %q, default value in %s does not validate its schema"
// DuplicateParamNameError ...
DuplicateParamNameError = "duplicate parameter name %q for %q in operation %q"
// DuplicatePropertiesError ...
DuplicatePropertiesError = "definition %q contains duplicate properties: %v"
// ExampleValueDoesNotValidateError results from an invalid example value provided
ExampleValueDoesNotValidateError = "example value for %s in %s does not validate its schema"
// ExampleValueItemsDoesNotValidateError results from an invalid example value provided for Items
ExampleValueItemsDoesNotValidateError = "example value for %s.items in %s does not validate its schema"
// ExampleValueHeaderDoesNotValidateError results from an invalid example value provided in header
ExampleValueHeaderDoesNotValidateError = "in operation %q, example value in header %s for %s does not validate its schema"
// ExampleValueHeaderItemsDoesNotValidateError results from an invalid example value provided in header.items
ExampleValueHeaderItemsDoesNotValidateError = "in operation %q, example value in header.items %s for %s does not validate its schema"
// ExampleValueInDoesNotValidateError ...
ExampleValueInDoesNotValidateError = "in operation %q, example value in %s does not validate its schema"
// EmptyPathParameterError means that a path parameter was found empty (e.g. "{}")
EmptyPathParameterError = "%q contains an empty path parameter"
// InvalidDocumentError states that spec validation only processes spec.Document objects
InvalidDocumentError = "spec validator can only validate spec.Document objects"
// InvalidItemsPatternError indicates an Items definition with invalid pattern
InvalidItemsPatternError = "%s for %q has invalid items pattern: %q"
// InvalidParameterDefinitionError indicates an error detected on a parameter definition
InvalidParameterDefinitionError = "invalid definition for parameter %s in %s in operation %q"
// InvalidParameterDefinitionAsSchemaError indicates an error detected on a parameter definition, which was mistaken with a schema definition.
// Most likely, this situation is encountered whenever a $ref has been added as a sibling of the parameter definition.
InvalidParameterDefinitionAsSchemaError = "invalid definition as Schema for parameter %s in %s in operation %q"
// InvalidPatternError ...
InvalidPatternError = "pattern %q is invalid in %s"
// InvalidPatternInError indicates an invalid pattern in a schema or items definition
InvalidPatternInError = "%s in %s has invalid pattern: %q"
// InvalidPatternInHeaderError indicates a header definition with an invalid pattern
InvalidPatternInHeaderError = "in operation %q, header %s for %s has invalid pattern %q: %v"
// InvalidPatternInParamError ...
InvalidPatternInParamError = "operation %q has invalid pattern in param %q: %q"
// InvalidReferenceError indicates that a $ref property could not be resolved
InvalidReferenceError = "invalid ref %q"
// InvalidResponseDefinitionAsSchemaError indicates an error detected on a response definition, which was mistaken with a schema definition.
// Most likely, this situation is encountered whenever a $ref has been added as a sibling of the response definition.
InvalidResponseDefinitionAsSchemaError = "invalid definition as Schema for response %s in %s"
// MultipleBodyParamError indicates that an operation specifies multiple parameter with in: body
MultipleBodyParamError = "operation %q has more than 1 body param: %v"
// NonUniqueOperationIDError indicates that the same operationId has been specified several times
NonUniqueOperationIDError = "%q is defined %d times"
// NoParameterInPathError indicates that a path was found without any parameter
NoParameterInPathError = "path param %q has no parameter definition"
// NoValidPathErrorOrWarning indicates that no single path could be validated. If Paths is empty, this message is only a warning.
NoValidPathErrorOrWarning = "spec has no valid path defined"
// NoValidResponseError indicates that no valid response description could be found for an operation
NoValidResponseError = "operation %q has no valid response"
// PathOverlapError ...
PathOverlapError = "path %s overlaps with %s"
// PathParamNotInPathError indicates that a parameter specified with in: path was not found in the path specification
PathParamNotInPathError = "path param %q is not present in path %q"
// PathParamNotUniqueError ...
PathParamNotUniqueError = "params in path %q must be unique: %q conflicts with %q"
// PathParamNotRequiredError ...
PathParamRequiredError = "in operation %q,path param %q must be declared as required"
// RefNotAllowedInHeaderError indicates a $ref was found in a header definition, which is not allowed by Swagger
RefNotAllowedInHeaderError = "IMPORTANT!in %q: $ref are not allowed in headers. In context for header %q%s"
// RequiredButNotDefinedError ...
RequiredButNotDefinedError = "%q is present in required but not defined as property in definition %q"
// SomeParametersBrokenError indicates that some parameters could not be resolved, which might result in partial checks to be carried on
SomeParametersBrokenError = "some parameters definitions are broken in %q.%s. Cannot carry on full checks on parameters for operation %s"
// UnresolvedReferencesError indicates that at least one $ref could not be resolved
UnresolvedReferencesError = "some references could not be resolved in spec. First found: %v"
)
// Warning messages related to spec validation and returned as results
const (
// ExamplesWithoutSchemaWarning indicates that examples are provided for a response,but not schema to validate the example against
ExamplesWithoutSchemaWarning = "Examples provided without schema in operation %q, %s"
// ExamplesMimeNotSupportedWarning indicates that examples are provided with a mime type different than application/json, which
// the validator dos not support yetl
ExamplesMimeNotSupportedWarning = "No validation attempt for examples for media types other than application/json, in operation %q, %s"
// PathParamGarbledWarning ...
PathParamGarbledWarning = "in path %q, param %q contains {,} or white space. Albeit not stricly illegal, this is probably no what you want"
// PathStrippedParamGarbledWarning ...
PathStrippedParamGarbledWarning = "path stripped from path parameters %s contains {,} or white space. This is probably no what you want."
// ReadOnlyAndRequiredWarning ...
ReadOnlyAndRequiredWarning = "Required property %s in %q should not be marked as both required and readOnly"
// RefShouldNotHaveSiblingsWarning indicates that a $ref was found with a sibling definition. This results in the $ref taking over its siblings,
// which is most likely not wanted.
RefShouldNotHaveSiblingsWarning = "$ref property should have no sibling in %q.%s"
// RequiredHasDefaultWarning indicates that a required parameter property should not have a default
RequiredHasDefaultWarning = "%s in %s has a default value and is required as parameter"
// UnusedDefinitionWarning ...
UnusedDefinitionWarning = "definition %q is not used anywhere"
// UnusedParamWarning ...
UnusedParamWarning = "parameter %q is not used anywhere"
// UnusedResponseWarning ...
UnusedResponseWarning = "response %q is not used anywhere"
)
// Additional error codes
const (
// InternalErrorCode reports an internal technical error
InternalErrorCode = http.StatusInternalServerError
// NotFoundErrorCode indicates that a resource (e.g. a $ref) could not be found
NotFoundErrorCode = http.StatusNotFound
)
func invalidDocumentMsg() errors.Error {
return errors.New(InternalErrorCode, InvalidDocumentError)
}
func invalidRefMsg(path string) errors.Error {
return errors.New(NotFoundErrorCode, InvalidReferenceError, path)
}
func unresolvedReferencesMsg(err error) errors.Error {
return errors.New(errors.CompositeErrorCode, UnresolvedReferencesError, err)
}
func noValidPathMsg() errors.Error {
return errors.New(errors.CompositeErrorCode, NoValidPathErrorOrWarning)
}
func emptyPathParameterMsg(path string) errors.Error {
return errors.New(errors.CompositeErrorCode, EmptyPathParameterError, path)
}
func nonUniqueOperationIDMsg(path string, i int) errors.Error {
return errors.New(errors.CompositeErrorCode, NonUniqueOperationIDError, path, i)
}
func circularAncestryDefinitionMsg(path string, args interface{}) errors.Error {
return errors.New(errors.CompositeErrorCode, CircularAncestryDefinitionError, path, args)
}
func duplicatePropertiesMsg(path string, args interface{}) errors.Error {
return errors.New(errors.CompositeErrorCode, DuplicatePropertiesError, path, args)
}
func pathParamNotInPathMsg(path, param string) errors.Error {
return errors.New(errors.CompositeErrorCode, PathParamNotInPathError, param, path)
}
func arrayRequiresItemsMsg(path, operation string) errors.Error {
return errors.New(errors.CompositeErrorCode, ArrayRequiresItemsError, path, operation)
}
func arrayInParamRequiresItemsMsg(path, operation string) errors.Error {
return errors.New(errors.CompositeErrorCode, ArrayInParamRequiresItemsError, path, operation)
}
func arrayInHeaderRequiresItemsMsg(path, operation string) errors.Error {
return errors.New(errors.CompositeErrorCode, ArrayInHeaderRequiresItemsError, path, operation)
}
func invalidItemsPatternMsg(path, operation, pattern string) errors.Error {
return errors.New(errors.CompositeErrorCode, InvalidItemsPatternError, path, operation, pattern)
}
func invalidPatternMsg(pattern, path string) errors.Error {
return errors.New(errors.CompositeErrorCode, InvalidPatternError, pattern, path)
}
func requiredButNotDefinedMsg(path, definition string) errors.Error {
return errors.New(errors.CompositeErrorCode, RequiredButNotDefinedError, path, definition)
}
func pathParamGarbledMsg(path, param string) errors.Error {
return errors.New(errors.CompositeErrorCode, PathParamGarbledWarning, path, param)
}
func pathStrippedParamGarbledMsg(path string) errors.Error {
return errors.New(errors.CompositeErrorCode, PathStrippedParamGarbledWarning, path)
}
func pathOverlapMsg(path, arg string) errors.Error {
return errors.New(errors.CompositeErrorCode, PathOverlapError, path, arg)
}
func invalidPatternInParamMsg(operation, param, pattern string) errors.Error {
return errors.New(errors.CompositeErrorCode, InvalidPatternInParamError, operation, param, pattern)
}
func pathParamRequiredMsg(operation, param string) errors.Error {
return errors.New(errors.CompositeErrorCode, PathParamRequiredError, operation, param)
}
func bothFormDataAndBodyMsg(operation string) errors.Error {
return errors.New(errors.CompositeErrorCode, BothFormDataAndBodyError, operation)
}
func multipleBodyParamMsg(operation string, args interface{}) errors.Error {
return errors.New(errors.CompositeErrorCode, MultipleBodyParamError, operation, args)
}
func pathParamNotUniqueMsg(path, param, arg string) errors.Error {
return errors.New(errors.CompositeErrorCode, PathParamNotUniqueError, path, param, arg)
}
func duplicateParamNameMsg(path, param, operation string) errors.Error {
return errors.New(errors.CompositeErrorCode, DuplicateParamNameError, param, path, operation)
}
func unusedParamMsg(arg string) errors.Error {
return errors.New(errors.CompositeErrorCode, UnusedParamWarning, arg)
}
func unusedDefinitionMsg(arg string) errors.Error {
return errors.New(errors.CompositeErrorCode, UnusedDefinitionWarning, arg)
}
func unusedResponseMsg(arg string) errors.Error {
return errors.New(errors.CompositeErrorCode, UnusedResponseWarning, arg)
}
func readOnlyAndRequiredMsg(path, param string) errors.Error {
return errors.New(errors.CompositeErrorCode, ReadOnlyAndRequiredWarning, param, path)
}
func noParameterInPathMsg(param string) errors.Error {
return errors.New(errors.CompositeErrorCode, NoParameterInPathError, param)
}
func requiredHasDefaultMsg(param, path string) errors.Error {
return errors.New(errors.CompositeErrorCode, RequiredHasDefaultWarning, param, path)
}
func defaultValueDoesNotValidateMsg(param, path string) errors.Error {
return errors.New(errors.CompositeErrorCode, DefaultValueDoesNotValidateError, param, path)
}
func defaultValueItemsDoesNotValidateMsg(param, path string) errors.Error {
return errors.New(errors.CompositeErrorCode, DefaultValueItemsDoesNotValidateError, param, path)
}
func noValidResponseMsg(operation string) errors.Error {
return errors.New(errors.CompositeErrorCode, NoValidResponseError, operation)
}
func defaultValueHeaderDoesNotValidateMsg(operation, header, path string) errors.Error {
return errors.New(errors.CompositeErrorCode, DefaultValueHeaderDoesNotValidateError, operation, header, path)
}
func defaultValueHeaderItemsDoesNotValidateMsg(operation, header, path string) errors.Error {
return errors.New(errors.CompositeErrorCode, DefaultValueHeaderItemsDoesNotValidateError, operation, header, path)
}
func invalidPatternInHeaderMsg(operation, header, path, pattern string, args interface{}) errors.Error {
return errors.New(errors.CompositeErrorCode, InvalidPatternInHeaderError, operation, header, path, pattern, args)
}
func invalidPatternInMsg(path, in, pattern string) errors.Error {
return errors.New(errors.CompositeErrorCode, InvalidPatternInError, path, in, pattern)
}
func defaultValueInDoesNotValidateMsg(operation, path string) errors.Error {
return errors.New(errors.CompositeErrorCode, DefaultValueInDoesNotValidateError, operation, path)
}
func exampleValueDoesNotValidateMsg(param, path string) errors.Error {
return errors.New(errors.CompositeErrorCode, ExampleValueDoesNotValidateError, param, path)
}
func exampleValueItemsDoesNotValidateMsg(param, path string) errors.Error {
return errors.New(errors.CompositeErrorCode, ExampleValueItemsDoesNotValidateError, param, path)
}
func exampleValueHeaderDoesNotValidateMsg(operation, header, path string) errors.Error {
return errors.New(errors.CompositeErrorCode, ExampleValueHeaderDoesNotValidateError, operation, header, path)
}
func exampleValueHeaderItemsDoesNotValidateMsg(operation, header, path string) errors.Error {
return errors.New(errors.CompositeErrorCode, ExampleValueHeaderItemsDoesNotValidateError, operation, header, path)
}
func exampleValueInDoesNotValidateMsg(operation, path string) errors.Error {
return errors.New(errors.CompositeErrorCode, ExampleValueInDoesNotValidateError, operation, path)
}
func examplesWithoutSchemaMsg(operation, response string) errors.Error {
return errors.New(errors.CompositeErrorCode, ExamplesWithoutSchemaWarning, operation, response)
}
func examplesMimeNotSupportedMsg(operation, response string) errors.Error {
return errors.New(errors.CompositeErrorCode, ExamplesMimeNotSupportedWarning, operation, response)
}
func refNotAllowedInHeaderMsg(path, header, ref string) errors.Error {
return errors.New(errors.CompositeErrorCode, RefNotAllowedInHeaderError, path, header, ref)
}
func cannotResolveRefMsg(path, ref string, err error) errors.Error {
return errors.New(errors.CompositeErrorCode, CannotResolveReferenceError, path, ref, err)
}
func invalidParameterDefinitionMsg(path, method, operationID string) errors.Error {
return errors.New(errors.CompositeErrorCode, InvalidParameterDefinitionError, path, method, operationID)
}
func invalidParameterDefinitionAsSchemaMsg(path, method, operationID string) errors.Error {
return errors.New(errors.CompositeErrorCode, InvalidParameterDefinitionAsSchemaError, path, method, operationID)
}
// disabled
//func invalidResponseDefinitionAsSchemaMsg(path, method string) errors.Error {
// return errors.New(errors.CompositeErrorCode, InvalidResponseDefinitionAsSchemaError, path, method)
//}
func someParametersBrokenMsg(path, method, operationID string) errors.Error {
return errors.New(errors.CompositeErrorCode, SomeParametersBrokenError, path, method, operationID)
}
func refShouldNotHaveSiblingsMsg(path, operationID string) errors.Error {
return errors.New(errors.CompositeErrorCode, RefShouldNotHaveSiblingsWarning, operationID, path)
}

View File

@@ -15,7 +15,6 @@
package validate
import (
"log"
"reflect"
"strings"
@@ -34,25 +33,50 @@ type typeValidator struct {
}
func (t *typeValidator) schemaInfoForType(data interface{}) (string, string) {
// internal type to JSON type with swagger 2.0 format (with go-openapi/strfmt extensions),
// see https://github.com/go-openapi/strfmt/blob/master/README.md
// TODO: this switch really is some sort of reverse lookup for formats. It should be provided by strfmt.
switch data.(type) {
case []byte:
case []byte, strfmt.Base64, *strfmt.Base64:
return "string", "byte"
case strfmt.CreditCard, *strfmt.CreditCard:
return "string", "creditcard"
case strfmt.Date, *strfmt.Date:
return "string", "date"
case strfmt.DateTime, *strfmt.DateTime:
return "string", "datetime"
return "string", "date-time"
case strfmt.Duration, *strfmt.Duration:
return "string", "duration"
case runtime.File, *runtime.File:
return "file", ""
case strfmt.URI, *strfmt.URI:
return "string", "uri"
case strfmt.Email, *strfmt.Email:
return "string", "email"
case strfmt.HexColor, *strfmt.HexColor:
return "string", "hexcolor"
case strfmt.Hostname, *strfmt.Hostname:
return "string", "hostname"
case strfmt.IPv4, *strfmt.IPv4:
return "string", "ipv4"
case strfmt.IPv6, *strfmt.IPv6:
return "string", "ipv6"
case strfmt.ISBN, *strfmt.ISBN:
return "string", "isbn"
case strfmt.ISBN10, *strfmt.ISBN10:
return "string", "isbn10"
case strfmt.ISBN13, *strfmt.ISBN13:
return "string", "isbn13"
case strfmt.MAC, *strfmt.MAC:
return "string", "mac"
case strfmt.ObjectId, *strfmt.ObjectId:
return "string", "bsonobjectid"
case strfmt.Password, *strfmt.Password:
return "string", "password"
case strfmt.RGBColor, *strfmt.RGBColor:
return "string", "rgbcolor"
case strfmt.SSN, *strfmt.SSN:
return "string", "ssn"
case strfmt.URI, *strfmt.URI:
return "string", "uri"
case strfmt.UUID, *strfmt.UUID:
return "string", "uuid"
case strfmt.UUID3, *strfmt.UUID3:
@@ -61,20 +85,8 @@ func (t *typeValidator) schemaInfoForType(data interface{}) (string, string) {
return "string", "uuid4"
case strfmt.UUID5, *strfmt.UUID5:
return "string", "uuid5"
case strfmt.ISBN, *strfmt.ISBN:
return "string", "isbn"
case strfmt.ISBN10, *strfmt.ISBN10:
return "string", "isbn10"
case strfmt.ISBN13, *strfmt.ISBN13:
return "string", "isbn13"
case strfmt.CreditCard, *strfmt.CreditCard:
return "string", "creditcard"
case strfmt.SSN, *strfmt.SSN:
return "string", "ssn"
case strfmt.HexColor, *strfmt.HexColor:
return "string", "hexcolor"
case strfmt.RGBColor, *strfmt.RGBColor:
return "string", "rgbcolor"
// TODO: missing binary (io.ReadCloser)
// TODO: missing json.Number
default:
val := reflect.ValueOf(data)
tpe := val.Type()
@@ -84,13 +96,17 @@ func (t *typeValidator) schemaInfoForType(data interface{}) (string, string) {
case reflect.String:
return "string", ""
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint8, reflect.Uint16, reflect.Uint32:
// NOTE: that is the spec. With go-openapi, is that not uint32 for unsigned integers?
return "integer", "int32"
case reflect.Int, reflect.Int64, reflect.Uint, reflect.Uint64:
return "integer", "int64"
case reflect.Float32:
// NOTE: is that not "float"?
return "number", "float32"
case reflect.Float64:
// NOTE: is that not "double"?
return "number", "float64"
// NOTE: go arrays (reflect.Array) are not supported (fixed length)
case reflect.Slice:
return "array", ""
case reflect.Map, reflect.Struct:
@@ -110,11 +126,10 @@ func (t *typeValidator) SetPath(path string) {
}
func (t *typeValidator) Applies(source interface{}, kind reflect.Kind) bool {
// typeValidator applies to Schema, Parameter and Header objects
stpe := reflect.TypeOf(source)
r := (len(t.Type) > 0 || t.Format != "") && (stpe == specSchemaType || stpe == specParameterType || stpe == specHeaderType)
if Debug {
log.Printf("type validator for %q applies %t for %T (kind: %v)\n", t.Path, r, source, kind)
}
debugLog("type validator for %q applies %t for %T (kind: %v)\n", t.Path, r, source, kind)
return r
}
@@ -122,8 +137,9 @@ func (t *typeValidator) Validate(data interface{}) *Result {
result := new(Result)
result.Inc()
if data == nil || reflect.DeepEqual(reflect.Zero(reflect.TypeOf(data)), reflect.ValueOf(data)) {
// nil or zero value for the passed structure require Type: null
if len(t.Type) > 0 && !t.Type.Contains("null") { // TODO: if a property is not required it also passes this
return sErr(errors.InvalidType(t.Path, t.In, strings.Join(t.Type, ","), "null"))
return errorHelp.sErr(errors.InvalidType(t.Path, t.In, strings.Join(t.Type, ","), "null"))
}
return result
}
@@ -132,24 +148,30 @@ func (t *typeValidator) Validate(data interface{}) *Result {
val := reflect.Indirect(reflect.ValueOf(data))
kind := val.Kind()
// infer schema type (JSON) and format from passed data type
schType, format := t.schemaInfoForType(data)
if Debug {
log.Println("path:", t.Path, "schType:", schType, "format:", format, "expType:", t.Type, "expFmt:", t.Format, "kind:", val.Kind().String())
}
debugLog("path: %s, schType: %s, format: %s, expType: %s, expFmt: %s, kind: %s", t.Path, schType, format, t.Type, t.Format, val.Kind().String())
// check numerical types
// TODO: check unsigned ints
// TODO: check json.Number (see schema.go)
isLowerInt := t.Format == "int64" && format == "int32"
isLowerFloat := t.Format == "float64" && format == "float32"
isFloatInt := schType == "number" && swag.IsFloat64AJSONInteger(val.Float()) && t.Type.Contains("integer")
isIntFloat := schType == "integer" && t.Type.Contains("number")
if kind != reflect.String && kind != reflect.Slice && t.Format != "" && !(t.Type.Contains(schType) || format == t.Format || isFloatInt || isIntFloat || isLowerInt || isLowerFloat) {
return sErr(errors.InvalidType(t.Path, t.In, t.Format, format))
// TODO: test case
return errorHelp.sErr(errors.InvalidType(t.Path, t.In, t.Format, format))
}
if !(t.Type.Contains("number") || t.Type.Contains("integer")) && t.Format != "" && (kind == reflect.String || kind == reflect.Slice) {
return result
}
if !(t.Type.Contains(schType) || isFloatInt || isIntFloat) {
return sErr(errors.InvalidType(t.Path, t.In, strings.Join(t.Type, ","), schType))
return errorHelp.sErr(errors.InvalidType(t.Path, t.In, strings.Join(t.Type, ","), schType))
}
return result
}

View File

@@ -16,7 +16,6 @@ package validate
import (
"fmt"
"log"
"reflect"
"github.com/go-openapi/errors"
@@ -112,6 +111,8 @@ func (i *itemsValidator) numberValidator() valueValidator {
ExclusiveMaximum: i.items.ExclusiveMaximum,
Minimum: i.items.Minimum,
ExclusiveMinimum: i.items.ExclusiveMinimum,
Type: i.items.Type,
Format: i.items.Format,
}
}
@@ -158,17 +159,16 @@ func (b *basicCommonValidator) Validate(data interface{}) (res *Result) {
if len(b.Enum) > 0 {
for _, enumValue := range b.Enum {
actualType := reflect.TypeOf(enumValue)
if actualType == nil {
continue
}
expectedValue := reflect.ValueOf(data)
if expectedValue.IsValid() && expectedValue.Type().ConvertibleTo(actualType) {
if reflect.DeepEqual(expectedValue.Convert(actualType).Interface(), enumValue) {
return nil
if actualType != nil { // Safeguard
expectedValue := reflect.ValueOf(data)
if expectedValue.IsValid() && expectedValue.Type().ConvertibleTo(actualType) {
if reflect.DeepEqual(expectedValue.Convert(actualType).Interface(), enumValue) {
return nil
}
}
}
}
return sErr(errors.EnumFail(b.Path, b.In, data, b.Enum))
return errorHelp.sErr(errors.EnumFail(b.Path, b.In, data, b.Enum))
}
return nil
}
@@ -252,6 +252,8 @@ func (p *HeaderValidator) numberValidator() valueValidator {
ExclusiveMaximum: p.header.ExclusiveMaximum,
Minimum: p.header.Minimum,
ExclusiveMinimum: p.header.ExclusiveMinimum,
Type: p.header.Type,
Format: p.header.Format,
}
}
@@ -357,6 +359,8 @@ func (p *ParamValidator) numberValidator() valueValidator {
ExclusiveMaximum: p.param.ExclusiveMaximum,
Minimum: p.param.Minimum,
ExclusiveMinimum: p.param.ExclusiveMinimum,
Type: p.param.Type,
Format: p.param.Format,
}
}
@@ -408,29 +412,25 @@ func (s *basicSliceValidator) Applies(source interface{}, kind reflect.Kind) boo
return false
}
func sErr(err errors.Error) *Result {
return &Result{Errors: []error{err}}
}
func (s *basicSliceValidator) Validate(data interface{}) *Result {
val := reflect.ValueOf(data)
size := int64(val.Len())
if s.MinItems != nil {
if err := MinItems(s.Path, s.In, size, *s.MinItems); err != nil {
return sErr(err)
return errorHelp.sErr(err)
}
}
if s.MaxItems != nil {
if err := MaxItems(s.Path, s.In, size, *s.MaxItems); err != nil {
return sErr(err)
return errorHelp.sErr(err)
}
}
if s.UniqueItems {
if err := UniqueItems(s.Path, s.In, data); err != nil {
return sErr(err)
return errorHelp.sErr(err)
}
}
@@ -470,6 +470,9 @@ type numberValidator struct {
ExclusiveMaximum bool
Minimum *float64
ExclusiveMinimum bool
// Allows for more accurate behavior regarding integers
Type string
Format string
}
func (n *numberValidator) SetPath(path string) {
@@ -482,51 +485,98 @@ func (n *numberValidator) Applies(source interface{}, kind reflect.Kind) bool {
isInt := kind >= reflect.Int && kind <= reflect.Uint64
isFloat := kind == reflect.Float32 || kind == reflect.Float64
r := isInt || isFloat
if Debug {
log.Printf("schema props validator for %q applies %t for %T (kind: %v)\n", n.Path, r, source, kind)
}
debugLog("schema props validator for %q applies %t for %T (kind: %v) isInt=%t, isFloat=%t\n", n.Path, r, source, kind, isInt, isFloat)
return r
}
if Debug {
log.Printf("schema props validator for %q applies %t for %T (kind: %v)\n", n.Path, false, source, kind)
}
debugLog("schema props validator for %q applies %t for %T (kind: %v)\n", n.Path, false, source, kind)
return false
}
func (n *numberValidator) convertToFloat(val interface{}) float64 {
v := reflect.ValueOf(val)
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return float64(v.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return float64(v.Uint())
case reflect.Float32, reflect.Float64:
return v.Float()
}
return 0
}
// Validate provides a validator for generic JSON numbers,
//
// By default, numbers are internally represented as float64.
// Formats float, or float32 may alter this behavior by mapping to float32.
// A special validation process is followed for integers, with optional "format":
// this is an attempt to provide a validation with native types.
//
// NOTE: since the constraint specified (boundary, multipleOf) is unmarshalled
// as float64, loss of information remains possible (e.g. on very large integers).
//
// Since this value directly comes from the unmarshalling, it is not possible
// at this stage of processing to check further and guarantee the correctness of such values.
//
// Normally, the JSON Number.MAX_SAFE_INTEGER (resp. Number.MIN_SAFE_INTEGER)
// would check we do not get such a loss.
//
// If this is the case, replace AddErrors() by AddWarnings() and IsValid() by !HasWarnings().
//
// TODO: consider replacing boundary check errors by simple warnings.
//
// TODO: default boundaries with MAX_SAFE_INTEGER are not checked (specific to json.Number?)
func (n *numberValidator) Validate(val interface{}) *Result {
data := n.convertToFloat(val)
res := new(Result)
resMultiple := new(Result)
resMinimum := new(Result)
resMaximum := new(Result)
// Used only to attempt to validate constraint on value,
// even though value or constraint specified do not match type and format
data := valueHelp.asFloat64(val)
// Is the provided value within the range of the specified numeric type and format?
res.AddErrors(IsValueValidAgainstRange(val, n.Type, n.Format, "Checked", n.Path))
if n.MultipleOf != nil {
if err := MultipleOf(n.Path, n.In, data, *n.MultipleOf); err != nil {
return sErr(err)
// Is the constraint specifier within the range of the specific numeric type and format?
resMultiple.AddErrors(IsValueValidAgainstRange(*n.MultipleOf, n.Type, n.Format, "MultipleOf", n.Path))
if resMultiple.IsValid() {
// Constraint validated with compatible types
if err := MultipleOfNativeType(n.Path, n.In, val, *n.MultipleOf); err != nil {
resMultiple.Merge(errorHelp.sErr(err))
}
} else {
// Constraint nevertheless validated, converted as general number
if err := MultipleOf(n.Path, n.In, data, *n.MultipleOf); err != nil {
resMultiple.Merge(errorHelp.sErr(err))
}
}
}
if n.Maximum != nil {
if err := Maximum(n.Path, n.In, data, *n.Maximum, n.ExclusiveMaximum); err != nil {
return sErr(err)
// Is the constraint specifier within the range of the specific numeric type and format?
resMaximum.AddErrors(IsValueValidAgainstRange(*n.Maximum, n.Type, n.Format, "Maximum boundary", n.Path))
if resMaximum.IsValid() {
// Constraint validated with compatible types
if err := MaximumNativeType(n.Path, n.In, val, *n.Maximum, n.ExclusiveMaximum); err != nil {
resMaximum.Merge(errorHelp.sErr(err))
}
} else {
// Constraint nevertheless validated, converted as general number
if err := Maximum(n.Path, n.In, data, *n.Maximum, n.ExclusiveMaximum); err != nil {
resMaximum.Merge(errorHelp.sErr(err))
}
}
}
if n.Minimum != nil {
if err := Minimum(n.Path, n.In, data, *n.Minimum, n.ExclusiveMinimum); err != nil {
return sErr(err)
// Is the constraint specifier within the range of the specific numeric type and format?
resMinimum.AddErrors(IsValueValidAgainstRange(*n.Minimum, n.Type, n.Format, "Minimum boundary", n.Path))
if resMinimum.IsValid() {
// Constraint validated with compatible types
if err := MinimumNativeType(n.Path, n.In, val, *n.Minimum, n.ExclusiveMinimum); err != nil {
resMinimum.Merge(errorHelp.sErr(err))
}
} else {
// Constraint nevertheless validated, converted as general number
if err := Minimum(n.Path, n.In, data, *n.Minimum, n.ExclusiveMinimum); err != nil {
resMinimum.Merge(errorHelp.sErr(err))
}
}
}
result := new(Result)
result.Inc()
return result
res.Merge(resMultiple, resMinimum, resMaximum)
res.Inc()
return res
}
type stringValidator struct {
@@ -548,44 +598,40 @@ func (s *stringValidator) Applies(source interface{}, kind reflect.Kind) bool {
switch source.(type) {
case *spec.Parameter, *spec.Schema, *spec.Items, *spec.Header:
r := kind == reflect.String
if Debug {
log.Printf("string validator for %q applies %t for %T (kind: %v)\n", s.Path, r, source, kind)
}
debugLog("string validator for %q applies %t for %T (kind: %v)\n", s.Path, r, source, kind)
return r
}
if Debug {
log.Printf("string validator for %q applies %t for %T (kind: %v)\n", s.Path, false, source, kind)
}
debugLog("string validator for %q applies %t for %T (kind: %v)\n", s.Path, false, source, kind)
return false
}
func (s *stringValidator) Validate(val interface{}) *Result {
data, ok := val.(string)
if !ok {
return sErr(errors.InvalidType(s.Path, s.In, "string", val))
return errorHelp.sErr(errors.InvalidType(s.Path, s.In, "string", val))
}
if s.Required && !s.AllowEmptyValue && (s.Default == nil || s.Default == "") {
if err := RequiredString(s.Path, s.In, data); err != nil {
return sErr(err)
return errorHelp.sErr(err)
}
}
if s.MaxLength != nil {
if err := MaxLength(s.Path, s.In, data, *s.MaxLength); err != nil {
return sErr(err)
return errorHelp.sErr(err)
}
}
if s.MinLength != nil {
if err := MinLength(s.Path, s.In, data, *s.MinLength); err != nil {
return sErr(err)
return errorHelp.sErr(err)
}
}
if s.Pattern != "" {
if err := Pattern(s.Path, s.In, data, s.Pattern); err != nil {
return sErr(err)
return errorHelp.sErr(err)
}
}
return nil

View File

@@ -15,8 +15,8 @@
package validate
import (
"fmt"
"reflect"
"regexp"
"unicode/utf8"
"github.com/go-openapi/errors"
@@ -40,7 +40,7 @@ func Enum(path, in string, data interface{}, enum interface{}) *errors.Validatio
return nil
}
actualType := reflect.TypeOf(enumValue)
if actualType == nil {
if actualType == nil { // Safeguard. Frankly, I don't know how we may get a nil
continue
}
expectedValue := reflect.ValueOf(data)
@@ -139,7 +139,10 @@ func RequiredNumber(path, in string, data float64) *errors.Validation {
// Pattern validates a string against a regular expression
func Pattern(path, in, data, pattern string) *errors.Validation {
re := regexp.MustCompile(pattern)
re, err := compileRegexp(pattern)
if err != nil {
return errors.FailedPattern(path, in, fmt.Sprintf("%s, but pattern is invalid: %s", pattern, err.Error()))
}
if !re.MatchString(data) {
return errors.FailedPattern(path, in, pattern)
}
@@ -196,9 +199,15 @@ func MinimumUint(path, in string, data, min uint64, exclusive bool) *errors.Vali
// MultipleOf validates if the provided number is a multiple of the factor
func MultipleOf(path, in string, data, factor float64) *errors.Validation {
mult := data / factor
// multipleOf factor must be positive
if factor < 0 {
return errors.MultipleOfMustBePositive(path, in, factor)
}
var mult float64
if factor < 1 {
mult = 1 / factor * data
} else {
mult = data / factor
}
if !swag.IsFloat64AJSONInteger(mult) {
return errors.NotMultipleOf(path, in, factor)
@@ -206,6 +215,28 @@ func MultipleOf(path, in string, data, factor float64) *errors.Validation {
return nil
}
// MultipleOfInt validates if the provided integer is a multiple of the factor
func MultipleOfInt(path, in string, data int64, factor int64) *errors.Validation {
// multipleOf factor must be positive
if factor < 0 {
return errors.MultipleOfMustBePositive(path, in, factor)
}
mult := data / factor
if mult*factor != data {
return errors.NotMultipleOf(path, in, factor)
}
return nil
}
// MultipleOfUint validates if the provided unsigned integer is a multiple of the factor
func MultipleOfUint(path, in string, data, factor uint64) *errors.Validation {
mult := data / factor
if mult*factor != data {
return errors.NotMultipleOf(path, in, factor)
}
return nil
}
// FormatOf validates if a string matches a format in the format registry
func FormatOf(path, in, format, data string, registry strfmt.Registry) *errors.Validation {
if registry == nil {
@@ -217,6 +248,151 @@ func FormatOf(path, in, format, data string, registry strfmt.Registry) *errors.V
if ok := registry.Validates(format, data); !ok {
return errors.InvalidType(path, in, format, data)
}
return nil
}
// MaximumNativeType provides native type constraint validation as a facade
// to various numeric types versions of Maximum constraint check.
//
// Assumes that any possible loss conversion during conversion has been
// checked beforehand.
//
// NOTE: currently, the max value is marshalled as a float64, no matter what,
// which means there may be a loss during conversions (e.g. for very large integers)
//
// TODO: Normally, a JSON MAX_SAFE_INTEGER check would ensure conversion remains loss-free
func MaximumNativeType(path, in string, val interface{}, max float64, exclusive bool) *errors.Validation {
kind := reflect.ValueOf(val).Type().Kind()
switch kind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
value := valueHelp.asInt64(val)
return MaximumInt(path, in, value, int64(max), exclusive)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
value := valueHelp.asUint64(val)
if max < 0 {
return errors.ExceedsMaximum(path, in, max, exclusive)
}
return MaximumUint(path, in, value, uint64(max), exclusive)
case reflect.Float32, reflect.Float64:
fallthrough
default:
value := valueHelp.asFloat64(val)
return Maximum(path, in, value, max, exclusive)
}
}
// MinimumNativeType provides native type constraint validation as a facade
// to various numeric types versions of Minimum constraint check.
//
// Assumes that any possible loss conversion during conversion has been
// checked beforehand.
//
// NOTE: currently, the min value is marshalled as a float64, no matter what,
// which means there may be a loss during conversions (e.g. for very large integers)
//
// TODO: Normally, a JSON MAX_SAFE_INTEGER check would ensure conversion remains loss-free
func MinimumNativeType(path, in string, val interface{}, min float64, exclusive bool) *errors.Validation {
kind := reflect.ValueOf(val).Type().Kind()
switch kind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
value := valueHelp.asInt64(val)
return MinimumInt(path, in, value, int64(min), exclusive)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
value := valueHelp.asUint64(val)
if min < 0 {
return nil
}
return MinimumUint(path, in, value, uint64(min), exclusive)
case reflect.Float32, reflect.Float64:
fallthrough
default:
value := valueHelp.asFloat64(val)
return Minimum(path, in, value, min, exclusive)
}
}
// MultipleOfNativeType provides native type constraint validation as a facade
// to various numeric types version of MultipleOf constraint check.
//
// Assumes that any possible loss conversion during conversion has been
// checked beforehand.
//
// NOTE: currently, the multipleOf factor is marshalled as a float64, no matter what,
// which means there may be a loss during conversions (e.g. for very large integers)
//
// TODO: Normally, a JSON MAX_SAFE_INTEGER check would ensure conversion remains loss-free
func MultipleOfNativeType(path, in string, val interface{}, multipleOf float64) *errors.Validation {
kind := reflect.ValueOf(val).Type().Kind()
switch kind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
value := valueHelp.asInt64(val)
return MultipleOfInt(path, in, value, int64(multipleOf))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
value := valueHelp.asUint64(val)
return MultipleOfUint(path, in, value, uint64(multipleOf))
case reflect.Float32, reflect.Float64:
fallthrough
default:
value := valueHelp.asFloat64(val)
return MultipleOf(path, in, value, multipleOf)
}
}
// IsValueValidAgainstRange checks that a numeric value is compatible with
// the range defined by Type and Format, that is, may be converted without loss.
//
// NOTE: this check is about type capacity and not formal verification such as: 1.0 != 1L
func IsValueValidAgainstRange(val interface{}, typeName, format, prefix, path string) error {
kind := reflect.ValueOf(val).Type().Kind()
// What is the string representation of val
stringRep := ""
switch kind {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
stringRep = swag.FormatUint64(valueHelp.asUint64(val))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
stringRep = swag.FormatInt64(valueHelp.asInt64(val))
case reflect.Float32, reflect.Float64:
stringRep = swag.FormatFloat64(valueHelp.asFloat64(val))
default:
return fmt.Errorf("%s value number range checking called with invalid (non numeric) val type in %s", prefix, path)
}
var errVal error
switch typeName {
case "integer":
switch format {
case "int32":
_, errVal = swag.ConvertInt32(stringRep)
case "uint32":
_, errVal = swag.ConvertUint32(stringRep)
case "uint64":
_, errVal = swag.ConvertUint64(stringRep)
case "int64":
fallthrough
default:
_, errVal = swag.ConvertInt64(stringRep)
}
case "number":
fallthrough
default:
switch format {
case "float", "float32":
_, errVal = swag.ConvertFloat32(stringRep)
case "double", "float64":
fallthrough
default:
// No check can be performed here since
// no number beyond float64 is supported
}
}
if errVal != nil { // We don't report the actual errVal from strconv
if format != "" {
errVal = fmt.Errorf("%s value must be of type %s with format %s in %s", prefix, typeName, format, path)
} else {
errVal = fmt.Errorf("%s value must be of type %s (default format) in %s", prefix, typeName, path)
}
}
return errVal
}