Skip to content

Commit b6d690e

Browse files
committed
wip
Signed-off-by: Pierre Fenoll <[email protected]>
1 parent 3690b66 commit b6d690e

16 files changed

+324
-989
lines changed

README.md

+10-35
Original file line numberDiff line numberDiff line change
@@ -153,43 +153,18 @@ func xmlBodyDecoder(body []byte) (interface{}, error) {
153153
}
154154
```
155155

156-
## Custom function to check uniqueness of array items
157-
158-
By defaut, the library check unique items by below predefined function
159-
160-
```go
161-
func isSliceOfUniqueItems(xs []interface{}) bool {
162-
s := len(xs)
163-
m := make(map[string]struct{}, s)
164-
for _, x := range xs {
165-
key, _ := json.Marshal(&x)
166-
m[string(key)] = struct{}{}
167-
}
168-
return s == len(m)
169-
}
170-
```
171-
172-
In the predefined function using `json.Marshal` to generate a string can
173-
be used as a map key which is to support check the uniqueness of an array
174-
when the array items are objects or arrays. You can register
175-
you own function according to your input data to get better performance:
176-
177-
```go
178-
func main() {
179-
// ...
180-
181-
// Register a customized function used to check uniqueness of array.
182-
openapi3.RegisterArrayUniqueItemsChecker(arrayUniqueItemsChecker)
183-
184-
// ... other validate codes
185-
}
186-
187-
func arrayUniqueItemsChecker(items []interface{}) bool {
188-
// Check the uniqueness of the input slice
189-
}
190-
```
191156

192157
## Sub-v0 breaking API changes
193158

159+
### v0.49.0
160+
OpenAPIv3 "in-house" schema validation was replaced with a correct JSON Schema implementation and conversion from OpenAPIv3 Schema to JSON Schema.
161+
* Dropped `openapi3.ErrOneOfConflict`: now when a value matches more than one `oneOf` schemas the error string contains `Must validate one and only one schema (oneOf)`
162+
* Dropped `openapi3.SchemaFormatValidationDisabled`: any `openapi3.Schema.Format` value is valid.
163+
* Dropped `openapi3.FailFast() openapi3.SchemaValidationOption` and `openapi3.SchemaErrorDetailsDisabled`: validating values against schemas is offloaded to a third-party library that does not provide such a mechanism.
164+
* Dropped `openapi3.RegisterArrayUniqueItemsChecker(openapi3.SliceUniqueItemsChecker)`: validating values against schemas is offloaded to a third-party library that does not provide such a mechanism.
165+
* Dropped `openapi3.SchemaStringFormats`, `openapi3.FormatCallback`, `openapi3.Format`, `openapi3.FormatOfStringForUUIDOfRFC4122`, `openapi3.DefineStringFormat(...)` and `openapi3.DefineStringFormatCallback(...)`. If your special format is not already under [`gojsonschema.FormatCheckers`](https://pkg.go.dev/github.com/xeipuuv/gojsonschema#pkg-variables), first define a [`gojsonschema.FormatChecker`](https://pkg.go.dev/github.com/xeipuuv/gojsonschema#FormatChecker) and register it with [`gojsonschema.FormatCheckers.Add("my-format", myImpl{})`](https://pkg.go.dev/github.com/xeipuuv/gojsonschema#FormatCheckerChain.Add) *before compiling your schemas*.
166+
* Dropped `openapi3.ErrSchemaInputNaN` and `openapi3.ErrSchemaInputInf`: OpenAPIv3 does not explicitly mention the related values.
167+
* Replaced `openapi3.SchemaError` with `openapi3.SchemaValidationError` which wraps `[]gojsonschema.ResultError` and thus provides similar functionality and more.
168+
194169
### v0.47.0
195170
Field `(*openapi3.SwaggerLoader).LoadSwaggerFromURIFunc` of type `func(*openapi3.SwaggerLoader, *url.URL) (*openapi3.Swagger, error)` was removed after the addition of the field `(*openapi3.SwaggerLoader).ReadFromURIFunc` of type `func(*openapi3.SwaggerLoader, *url.URL) ([]byte, error)`.

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ require (
77
github.com/go-openapi/jsonpointer v0.19.5
88
github.com/gorilla/mux v1.8.0
99
github.com/stretchr/testify v1.5.1
10+
github.com/xeipuuv/gojsonschema v1.2.0
1011
gopkg.in/yaml.v2 v2.3.0 // indirect
1112
)

go.sum

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
2323
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
2424
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
2525
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
26+
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
27+
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
28+
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
29+
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
30+
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
31+
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
2632
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2733
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
2834
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

openapi3/errors.go

+38
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,46 @@ package openapi3
33
import (
44
"bytes"
55
"errors"
6+
"strings"
7+
8+
"github.com/xeipuuv/gojsonschema"
69
)
710

11+
// SchemaValidationError is a collection of errors
12+
type SchemaValidationError []gojsonschema.ResultError
13+
14+
var _ error = (*SchemaValidationError)(nil)
15+
16+
func (e SchemaValidationError) Error() string {
17+
var buff strings.Builder
18+
for i, re := range []gojsonschema.ResultError(e) {
19+
if i != 0 {
20+
buff.WriteString("\n")
21+
}
22+
buff.WriteString(re.String())
23+
}
24+
return buff.String()
25+
}
26+
27+
// Errors unwraps into much detailed errors.
28+
// See https://pkg.go.dev/github.com/xeipuuv/gojsonschema#ResultError
29+
func (e SchemaValidationError) Errors() []gojsonschema.ResultError {
30+
return e
31+
}
32+
33+
// JSONPointer returns a dot (.) delimited "JSON path" to the context of the first error.
34+
func (e SchemaValidationError) JSONPointer() string {
35+
return []gojsonschema.ResultError(e)[0].Field()
36+
}
37+
38+
func (e SchemaValidationError) asMultiError() MultiError {
39+
errs := make([]error, 0, len(e))
40+
for _, re := range e {
41+
errs = append(errs, errors.New(re.String()))
42+
}
43+
return errs
44+
}
45+
846
// MultiError is a collection of errors, intended for when
947
// multiple issues need to be reported upstream
1048
type MultiError []error

0 commit comments

Comments
 (0)