From f31585dd423abab4adba45f50211a610009ba7ac Mon Sep 17 00:00:00 2001 From: Frederic BIDON Date: Wed, 6 Dec 2023 22:13:22 +0100 Subject: [PATCH] chore: updated & relinted * updated required go version to go1.19 * updated dependencies (test deps) * updated linting rules * relinted code (primarily tests & docstrings) * updated github action * updated badges & links in README Signed-off-by: Frederic BIDON --- .github/workflows/ci.yaml | 47 -- .github/workflows/go-test.yml | 50 +++ .golangci.yml | 52 ++- README.md | 5 +- api.go | 18 +- api_test.go | 14 +- go.mod | 6 +- go.sum | 7 - schema_test.go | 810 ++++++++++++++++++---------------- 9 files changed, 529 insertions(+), 480 deletions(-) delete mode 100644 .github/workflows/ci.yaml create mode 100644 .github/workflows/go-test.yml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml deleted file mode 100644 index 6905123..0000000 --- a/.github/workflows/ci.yaml +++ /dev/null @@ -1,47 +0,0 @@ -name: Go - -on: [push, pull_request] - -jobs: - build: - runs-on: ${{ matrix.os }} - - strategy: - matrix: - os: [ ubuntu-latest, macos-latest, windows-latest ] - go: [1.18, 1.19] - fail-fast: false - - steps: - - uses: actions/checkout@v2 - - - name: Set up Go - uses: actions/setup-go@v2 - with: - go-version: ${{ matrix.go }} - - - name: Setup gotestsum - uses: autero1/action-gotestsum@v1.0.0 - with: - gotestsum_version: 1.8.1 - - - name: Test - run: gotestsum --format short-verbose -- -race -timeout=20m -coverprofile=coverage_txt -covermode=atomic ./... - - - uses: codecov/codecov-action@v2 - with: - files: coverage_txt - - lint: - runs-on: ${{ matrix.os }} - - strategy: - matrix: - os: [ ubuntu-latest, macos-latest, windows-latest ] - fail-fast: false - - steps: - - uses: actions/checkout@v2 - - uses: golangci/golangci-lint-action@v2 - with: - args: --timeout=5m \ No newline at end of file diff --git a/.github/workflows/go-test.yml b/.github/workflows/go-test.yml new file mode 100644 index 0000000..e1a95c7 --- /dev/null +++ b/.github/workflows/go-test.yml @@ -0,0 +1,50 @@ +name: go test + +on: [push, pull_request] + +jobs: + lint: + name: Lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v4 + with: + go-version: stable + check-latest: true + cache: true + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: latest + only-new-issues: true + + test: + name: Unit tests + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: [ ubuntu-latest, macos-latest, windows-latest ] + go_version: ['oldstable', 'stable' ] + + steps: + - name: Run unit tests + uses: actions/setup-go@v4 + with: + go-version: '${{ matrix.go_version }}' + check-latest: true + cache: true + + - uses: actions/checkout@v3 + + - run: go test -v -race -coverprofile="coverage-${{ matrix.os }}.${{ matrix.go_version }}.out" -covermode=atomic ./... + + - name: Upload coverage to codecov + uses: codecov/codecov-action@v3 + with: + files: './coverage-${{ matrix.os }}.${{ matrix.go_version }}.out' + flags: '${{ matrix.go_version }}' + os: '${{ matrix.os }}' + fail_ci_if_error: false + verbose: true diff --git a/.golangci.yml b/.golangci.yml index 4e1fc0c..cf88ead 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -4,45 +4,59 @@ linters-settings: golint: min-confidence: 0 gocyclo: - min-complexity: 30 + min-complexity: 45 maligned: suggest-new: true dupl: - threshold: 100 + threshold: 200 goconst: min-len: 2 - min-occurrences: 4 + min-occurrences: 3 + linters: enable-all: true disable: + - errname # this repo doesn't follow the convention advised by this linter - maligned + - unparam - lll + - gochecknoinits - gochecknoglobals + - funlen - godox - gocognit - whitespace - wsl - - funlen - - gochecknoglobals - - gochecknoinits - - scopelint - wrapcheck - - exhaustivestruct - - exhaustive - - nlreturn - testpackage - - gci - - gofumpt - - goerr113 + - nlreturn - gomnd - - tparallel + - exhaustivestruct + - goerr113 + - errorlint - nestif - godot - - errorlint + - gofumpt - paralleltest - tparallel - - cyclop - - errname - - varnamelen + - thelper + - ifshort - exhaustruct - - maintidx + - varnamelen + - gci + - depguard + - errchkjson + - inamedparam + - nonamedreturns + - musttag + - ireturn + - forcetypeassert + - cyclop + # deprecated linters + - deadcode + - interfacer + - scopelint + - varcheck + - structcheck + - golint + - nosnakecase diff --git a/README.md b/README.md index 4aac049..6d57ea5 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,8 @@ -# OpenAPI errors +# OpenAPI errors [![Build Status](https://github.com/go-openapi/errors/actions/workflows/go-test.yml/badge.svg)](https://github.com/go-openapi/errors/actions?query=workflow%3A"go+test") [![codecov](https://codecov.io/gh/go-openapi/errors/branch/master/graph/badge.svg)](https://codecov.io/gh/go-openapi/errors) -[![Build Status](https://travis-ci.org/go-openapi/errors.svg?branch=master)](https://travis-ci.org/go-openapi/errors) -[![codecov](https://codecov.io/gh/go-openapi/errors/branch/master/graph/badge.svg)](https://codecov.io/gh/go-openapi/errors) [![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/errors/master/LICENSE) [![Go Reference](https://pkg.go.dev/badge/github.com/go-openapi/errors.svg)](https://pkg.go.dev/github.com/go-openapi/errors) -[![GolangCI](https://golangci.com/badges/github.com/go-openapi/errors.svg)](https://golangci.com) [![Go Report Card](https://goreportcard.com/badge/github.com/go-openapi/errors)](https://goreportcard.com/report/github.com/go-openapi/errors) Shared errors and error interface used throughout the various libraries found in the go-openapi toolkit. diff --git a/api.go b/api.go index c13f343..5320cb9 100644 --- a/api.go +++ b/api.go @@ -55,9 +55,15 @@ func (a apiError) MarshalJSON() ([]byte, error) { // New creates a new API error with a code and a message func New(code int32, message string, args ...interface{}) Error { if len(args) > 0 { - return &apiError{code, fmt.Sprintf(message, args...)} + return &apiError{ + code: code, + message: fmt.Sprintf(message, args...), + } + } + return &apiError{ + code: code, + message: message, } - return &apiError{code, message} } // NotFound creates a new not found error @@ -130,10 +136,14 @@ func flattenComposite(errs *CompositeError) *CompositeError { // MethodNotAllowed creates a new method not allowed error func MethodNotAllowed(requested string, allow []string) Error { msg := fmt.Sprintf("method %s is not allowed, but [%s] are", requested, strings.Join(allow, ",")) - return &MethodNotAllowedError{code: http.StatusMethodNotAllowed, Allowed: allow, message: msg} + return &MethodNotAllowedError{ + code: http.StatusMethodNotAllowed, + Allowed: allow, + message: msg, + } } -// ServeError the error handler interface implementation +// ServeError implements the http error handler interface func ServeError(rw http.ResponseWriter, r *http.Request, err error) { rw.Header().Set("Content-Type", "application/json") switch e := err.(type) { diff --git a/api_test.go b/api_test.go index 814524c..d0f0bf4 100644 --- a/api_test.go +++ b/api_test.go @@ -148,37 +148,37 @@ func TestServeError(t *testing.T) { func TestAPIErrors(t *testing.T) { err := New(402, "this failed %s", "yada") - assert.Error(t, err) + require.Error(t, err) assert.EqualValues(t, 402, err.Code()) assert.EqualValues(t, "this failed yada", err.Error()) err = NotFound("this failed %d", 1) - assert.Error(t, err) + require.Error(t, err) assert.EqualValues(t, http.StatusNotFound, err.Code()) assert.EqualValues(t, "this failed 1", err.Error()) err = NotFound("") - assert.Error(t, err) + require.Error(t, err) assert.EqualValues(t, http.StatusNotFound, err.Code()) assert.EqualValues(t, "Not found", err.Error()) err = NotImplemented("not implemented") - assert.Error(t, err) + require.Error(t, err) assert.EqualValues(t, http.StatusNotImplemented, err.Code()) assert.EqualValues(t, "not implemented", err.Error()) err = MethodNotAllowed("GET", []string{"POST", "PUT"}) - assert.Error(t, err) + require.Error(t, err) assert.EqualValues(t, http.StatusMethodNotAllowed, err.Code()) assert.EqualValues(t, "method GET is not allowed, but [POST,PUT] are", err.Error()) err = InvalidContentType("application/saml", []string{"application/json", "application/x-yaml"}) - assert.Error(t, err) + require.Error(t, err) assert.EqualValues(t, http.StatusUnsupportedMediaType, err.Code()) assert.EqualValues(t, "unsupported media type \"application/saml\", only [application/json application/x-yaml] are allowed", err.Error()) err = InvalidResponseFormat("application/saml", []string{"application/json", "application/x-yaml"}) - assert.Error(t, err) + require.Error(t, err) assert.EqualValues(t, http.StatusNotAcceptable, err.Code()) assert.EqualValues(t, "unsupported media type requested, only [application/json application/x-yaml] are available", err.Error()) } diff --git a/go.mod b/go.mod index ca6e5dc..970b0f1 100644 --- a/go.mod +++ b/go.mod @@ -1,12 +1,14 @@ module github.com/go-openapi/errors -go 1.14 +require github.com/stretchr/testify v1.6.1 require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/kr/text v0.2.0 // indirect github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect - github.com/stretchr/testify v1.6.1 + github.com/pmezard/go-difflib v1.0.0 // indirect gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) + +go 1.19 diff --git a/go.sum b/go.sum index 5dc27c4..cbf695e 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,8 @@ -github.com/creack/pty v1.1.9 h1:uDmaGzcdjhF4i/plgjmEsriH11Y0o7RKapEf/LDaM3w= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= @@ -16,13 +13,9 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c h1:grhR+C34yXImVGp7EzNk+DTIk+323eIUWOmEevy6bDo= -gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/schema_test.go b/schema_test.go index fe0535e..eabc69a 100644 --- a/schema_test.go +++ b/schema_test.go @@ -20,400 +20,430 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) +//nolint:maintidx func TestSchemaErrors(t *testing.T) { - err := InvalidType("confirmed", "query", "boolean", nil) - assert.Error(t, err) - assert.EqualValues(t, InvalidTypeCode, err.Code()) - assert.Equal(t, "confirmed in query must be of type boolean", err.Error()) - - err = InvalidType("confirmed", "", "boolean", nil) - assert.Error(t, err) - assert.EqualValues(t, InvalidTypeCode, err.Code()) - assert.Equal(t, "confirmed must be of type boolean", err.Error()) - - err = InvalidType("confirmed", "query", "boolean", "hello") - assert.Error(t, err) - assert.EqualValues(t, InvalidTypeCode, err.Code()) - assert.Equal(t, "confirmed in query must be of type boolean: \"hello\"", err.Error()) - - err = InvalidType("confirmed", "query", "boolean", errors.New("hello")) - assert.Error(t, err) - assert.EqualValues(t, InvalidTypeCode, err.Code()) - assert.Equal(t, "confirmed in query must be of type boolean, because: hello", err.Error()) - - err = InvalidType("confirmed", "", "boolean", "hello") - assert.Error(t, err) - assert.EqualValues(t, InvalidTypeCode, err.Code()) - assert.Equal(t, "confirmed must be of type boolean: \"hello\"", err.Error()) - - err = InvalidType("confirmed", "", "boolean", errors.New("hello")) - assert.Error(t, err) - assert.EqualValues(t, InvalidTypeCode, err.Code()) - assert.Equal(t, "confirmed must be of type boolean, because: hello", err.Error()) - - err = DuplicateItems("uniques", "query") - assert.Error(t, err) - assert.EqualValues(t, UniqueFailCode, err.Code()) - assert.Equal(t, "uniques in query shouldn't contain duplicates", err.Error()) - - err = DuplicateItems("uniques", "") - assert.Error(t, err) - assert.EqualValues(t, UniqueFailCode, err.Code()) - assert.Equal(t, "uniques shouldn't contain duplicates", err.Error()) - - err = TooManyItems("something", "query", 5, 6) - assert.Error(t, err) - assert.EqualValues(t, MaxItemsFailCode, err.Code()) - assert.Equal(t, "something in query should have at most 5 items", err.Error()) - assert.Equal(t, 6, err.Value) - - err = TooManyItems("something", "", 5, 6) - assert.Error(t, err) - assert.EqualValues(t, MaxItemsFailCode, err.Code()) - assert.Equal(t, "something should have at most 5 items", err.Error()) - assert.Equal(t, 6, err.Value) - - err = TooFewItems("something", "", 5, 4) - assert.Error(t, err) - assert.EqualValues(t, MinItemsFailCode, err.Code()) - assert.Equal(t, "something should have at least 5 items", err.Error()) - assert.Equal(t, 4, err.Value) - - err = ExceedsMaximumInt("something", "query", 5, false, 6) - assert.Error(t, err) - assert.EqualValues(t, MaxFailCode, err.Code()) - assert.Equal(t, "something in query should be less than or equal to 5", err.Error()) - assert.Equal(t, 6, err.Value) - - err = ExceedsMaximumInt("something", "", 5, false, 6) - assert.Error(t, err) - assert.EqualValues(t, MaxFailCode, err.Code()) - assert.Equal(t, "something should be less than or equal to 5", err.Error()) - assert.Equal(t, 6, err.Value) - - err = ExceedsMaximumInt("something", "query", 5, true, 6) - assert.Error(t, err) - assert.EqualValues(t, MaxFailCode, err.Code()) - assert.Equal(t, "something in query should be less than 5", err.Error()) - assert.Equal(t, 6, err.Value) - - err = ExceedsMaximumInt("something", "", 5, true, 6) - assert.Error(t, err) - assert.EqualValues(t, MaxFailCode, err.Code()) - assert.Equal(t, "something should be less than 5", err.Error()) - assert.Equal(t, 6, err.Value) - - err = ExceedsMaximumUint("something", "query", 5, false, 6) - assert.Error(t, err) - assert.EqualValues(t, MaxFailCode, err.Code()) - assert.Equal(t, "something in query should be less than or equal to 5", err.Error()) - assert.Equal(t, 6, err.Value) - - err = ExceedsMaximumUint("something", "", 5, false, 6) - assert.Error(t, err) - assert.EqualValues(t, MaxFailCode, err.Code()) - assert.Equal(t, "something should be less than or equal to 5", err.Error()) - assert.Equal(t, 6, err.Value) - - err = ExceedsMaximumUint("something", "query", 5, true, 6) - assert.Error(t, err) - assert.EqualValues(t, MaxFailCode, err.Code()) - assert.Equal(t, "something in query should be less than 5", err.Error()) - assert.Equal(t, 6, err.Value) - - err = ExceedsMaximumUint("something", "", 5, true, 6) - assert.Error(t, err) - assert.EqualValues(t, MaxFailCode, err.Code()) - assert.Equal(t, "something should be less than 5", err.Error()) - assert.Equal(t, 6, err.Value) - - err = ExceedsMaximum("something", "query", 5, false, 6) - assert.Error(t, err) - assert.EqualValues(t, MaxFailCode, err.Code()) - assert.Equal(t, "something in query should be less than or equal to 5", err.Error()) - assert.Equal(t, 6, err.Value) - - err = ExceedsMaximum("something", "", 5, false, 6) - assert.Error(t, err) - assert.EqualValues(t, MaxFailCode, err.Code()) - assert.Equal(t, "something should be less than or equal to 5", err.Error()) - assert.Equal(t, 6, err.Value) - - err = ExceedsMaximum("something", "query", 5, true, 6) - assert.Error(t, err) - assert.EqualValues(t, MaxFailCode, err.Code()) - assert.Equal(t, "something in query should be less than 5", err.Error()) - assert.Equal(t, 6, err.Value) - - err = ExceedsMaximum("something", "", 5, true, 6) - assert.Error(t, err) - assert.EqualValues(t, MaxFailCode, err.Code()) - assert.Equal(t, "something should be less than 5", err.Error()) - assert.Equal(t, 6, err.Value) - - err = ExceedsMinimumInt("something", "query", 5, false, 4) - assert.Error(t, err) - assert.EqualValues(t, MinFailCode, err.Code()) - assert.Equal(t, "something in query should be greater than or equal to 5", err.Error()) - assert.Equal(t, 4, err.Value) - - err = ExceedsMinimumInt("something", "", 5, false, 4) - assert.Error(t, err) - assert.EqualValues(t, MinFailCode, err.Code()) - assert.Equal(t, "something should be greater than or equal to 5", err.Error()) - assert.Equal(t, 4, err.Value) - - err = ExceedsMinimumInt("something", "query", 5, true, 4) - assert.Error(t, err) - assert.EqualValues(t, MinFailCode, err.Code()) - assert.Equal(t, "something in query should be greater than 5", err.Error()) - assert.Equal(t, 4, err.Value) - - err = ExceedsMinimumInt("something", "", 5, true, 4) - assert.Error(t, err) - assert.EqualValues(t, MinFailCode, err.Code()) - assert.Equal(t, "something should be greater than 5", err.Error()) - assert.Equal(t, 4, err.Value) - - err = ExceedsMinimumUint("something", "query", 5, false, 4) - assert.Error(t, err) - assert.EqualValues(t, MinFailCode, err.Code()) - assert.Equal(t, "something in query should be greater than or equal to 5", err.Error()) - assert.Equal(t, 4, err.Value) - - err = ExceedsMinimumUint("something", "", 5, false, 4) - assert.Error(t, err) - assert.EqualValues(t, MinFailCode, err.Code()) - assert.Equal(t, "something should be greater than or equal to 5", err.Error()) - assert.Equal(t, 4, err.Value) - - err = ExceedsMinimumUint("something", "query", 5, true, 4) - assert.Error(t, err) - assert.EqualValues(t, MinFailCode, err.Code()) - assert.Equal(t, "something in query should be greater than 5", err.Error()) - assert.Equal(t, 4, err.Value) - - err = ExceedsMinimumUint("something", "", 5, true, 4) - assert.Error(t, err) - assert.EqualValues(t, MinFailCode, err.Code()) - assert.Equal(t, "something should be greater than 5", err.Error()) - assert.Equal(t, 4, err.Value) - - err = ExceedsMinimum("something", "query", 5, false, 4) - assert.Error(t, err) - assert.EqualValues(t, MinFailCode, err.Code()) - assert.Equal(t, "something in query should be greater than or equal to 5", err.Error()) - assert.Equal(t, 4, err.Value) - - err = ExceedsMinimum("something", "", 5, false, 4) - assert.Error(t, err) - assert.EqualValues(t, MinFailCode, err.Code()) - assert.Equal(t, "something should be greater than or equal to 5", err.Error()) - assert.Equal(t, 4, err.Value) - - err = ExceedsMinimum("something", "query", 5, true, 4) - assert.Error(t, err) - assert.EqualValues(t, MinFailCode, err.Code()) - assert.Equal(t, "something in query should be greater than 5", err.Error()) - assert.Equal(t, 4, err.Value) - - err = ExceedsMinimum("something", "", 5, true, 4) - assert.Error(t, err) - assert.EqualValues(t, MinFailCode, err.Code()) - assert.Equal(t, "something should be greater than 5", err.Error()) - assert.Equal(t, 4, err.Value) - - err = NotMultipleOf("something", "query", 5, 1) - assert.Error(t, err) - assert.EqualValues(t, MultipleOfFailCode, err.Code()) - assert.Equal(t, "something in query should be a multiple of 5", err.Error()) - assert.Equal(t, 1, err.Value) - - err = NotMultipleOf("something", "query", float64(5), float64(1)) - assert.Error(t, err) - assert.EqualValues(t, MultipleOfFailCode, err.Code()) - assert.Equal(t, "something in query should be a multiple of 5", err.Error()) - assert.Equal(t, float64(1), err.Value) - - err = NotMultipleOf("something", "query", uint64(5), uint64(1)) - assert.Error(t, err) - assert.EqualValues(t, MultipleOfFailCode, err.Code()) - assert.Equal(t, "something in query should be a multiple of 5", err.Error()) - assert.Equal(t, uint64(1), err.Value) - - err = NotMultipleOf("something", "", 5, 1) - assert.Error(t, err) - assert.EqualValues(t, MultipleOfFailCode, err.Code()) - assert.Equal(t, "something should be a multiple of 5", err.Error()) - assert.Equal(t, 1, err.Value) - - err = EnumFail("something", "query", "yada", []interface{}{"hello", "world"}) - assert.Error(t, err) - assert.EqualValues(t, EnumFailCode, err.Code()) - assert.Equal(t, "something in query should be one of [hello world]", err.Error()) - assert.Equal(t, "yada", err.Value) - - err = EnumFail("something", "", "yada", []interface{}{"hello", "world"}) - assert.Error(t, err) - assert.EqualValues(t, EnumFailCode, err.Code()) - assert.Equal(t, "something should be one of [hello world]", err.Error()) - assert.Equal(t, "yada", err.Value) - - err = Required("something", "query", nil) - assert.Error(t, err) - assert.EqualValues(t, RequiredFailCode, err.Code()) - assert.Equal(t, "something in query is required", err.Error()) - assert.Equal(t, nil, err.Value) - - err = Required("something", "", nil) - assert.Error(t, err) - assert.EqualValues(t, RequiredFailCode, err.Code()) - assert.Equal(t, "something is required", err.Error()) - assert.Equal(t, nil, err.Value) - - err = ReadOnly("something", "query", nil) - assert.Error(t, err) - assert.EqualValues(t, ReadOnlyFailCode, err.Code()) - assert.Equal(t, "something in query is readOnly", err.Error()) - assert.Equal(t, nil, err.Value) - - err = ReadOnly("something", "", nil) - assert.Error(t, err) - assert.EqualValues(t, ReadOnlyFailCode, err.Code()) - assert.Equal(t, "something is readOnly", err.Error()) - assert.Equal(t, nil, err.Value) - - err = TooLong("something", "query", 5, "abcdef") - assert.Error(t, err) - assert.EqualValues(t, TooLongFailCode, err.Code()) - assert.Equal(t, "something in query should be at most 5 chars long", err.Error()) - assert.Equal(t, "abcdef", err.Value) - - err = TooLong("something", "", 5, "abcdef") - assert.Error(t, err) - assert.EqualValues(t, TooLongFailCode, err.Code()) - assert.Equal(t, "something should be at most 5 chars long", err.Error()) - assert.Equal(t, "abcdef", err.Value) - - err = TooShort("something", "query", 5, "a") - assert.Error(t, err) - assert.EqualValues(t, TooShortFailCode, err.Code()) - assert.Equal(t, "something in query should be at least 5 chars long", err.Error()) - assert.Equal(t, "a", err.Value) - - err = TooShort("something", "", 5, "a") - assert.Error(t, err) - assert.EqualValues(t, TooShortFailCode, err.Code()) - assert.Equal(t, "something should be at least 5 chars long", err.Error()) - assert.Equal(t, "a", err.Value) - - err = FailedPattern("something", "query", "\\d+", "a") - assert.Error(t, err) - assert.EqualValues(t, PatternFailCode, err.Code()) - assert.Equal(t, "something in query should match '\\d+'", err.Error()) - assert.Equal(t, "a", err.Value) - - err = FailedPattern("something", "", "\\d+", "a") - assert.Error(t, err) - assert.EqualValues(t, PatternFailCode, err.Code()) - assert.Equal(t, "something should match '\\d+'", err.Error()) - assert.Equal(t, "a", err.Value) - - err = InvalidTypeName("something") - assert.Error(t, err) - assert.EqualValues(t, InvalidTypeCode, err.Code()) - assert.Equal(t, "something is an invalid type name", err.Error()) - - err = AdditionalItemsNotAllowed("something", "query") - assert.Error(t, err) - assert.EqualValues(t, NoAdditionalItemsCode, err.Code()) - assert.Equal(t, "something in query can't have additional items", err.Error()) - - err = AdditionalItemsNotAllowed("something", "") - assert.Error(t, err) - assert.EqualValues(t, NoAdditionalItemsCode, err.Code()) - assert.Equal(t, "something can't have additional items", err.Error()) - - err = InvalidCollectionFormat("something", "query", "yada") - assert.Error(t, err) + t.Run("with InvalidType", func(t *testing.T) { + err := InvalidType("confirmed", "query", "boolean", nil) + require.Error(t, err) + assert.EqualValues(t, InvalidTypeCode, err.Code()) + assert.Equal(t, "confirmed in query must be of type boolean", err.Error()) + + err = InvalidType("confirmed", "", "boolean", nil) + require.Error(t, err) + assert.EqualValues(t, InvalidTypeCode, err.Code()) + assert.Equal(t, "confirmed must be of type boolean", err.Error()) + + err = InvalidType("confirmed", "query", "boolean", "hello") + require.Error(t, err) + assert.EqualValues(t, InvalidTypeCode, err.Code()) + assert.Equal(t, "confirmed in query must be of type boolean: \"hello\"", err.Error()) + + err = InvalidType("confirmed", "query", "boolean", errors.New("hello")) + require.Error(t, err) + assert.EqualValues(t, InvalidTypeCode, err.Code()) + assert.Equal(t, "confirmed in query must be of type boolean, because: hello", err.Error()) + + err = InvalidType("confirmed", "", "boolean", "hello") + require.Error(t, err) + assert.EqualValues(t, InvalidTypeCode, err.Code()) + assert.Equal(t, "confirmed must be of type boolean: \"hello\"", err.Error()) + + err = InvalidType("confirmed", "", "boolean", errors.New("hello")) + require.Error(t, err) + assert.EqualValues(t, InvalidTypeCode, err.Code()) + assert.Equal(t, "confirmed must be of type boolean, because: hello", err.Error()) + }) + + t.Run("with DuplicateItems", func(t *testing.T) { + err := DuplicateItems("uniques", "query") + require.Error(t, err) + assert.EqualValues(t, UniqueFailCode, err.Code()) + assert.Equal(t, "uniques in query shouldn't contain duplicates", err.Error()) + + err = DuplicateItems("uniques", "") + require.Error(t, err) + assert.EqualValues(t, UniqueFailCode, err.Code()) + assert.Equal(t, "uniques shouldn't contain duplicates", err.Error()) + }) + + t.Run("with TooMany/TooFew Items", func(t *testing.T) { + err := TooManyItems("something", "query", 5, 6) + require.Error(t, err) + assert.EqualValues(t, MaxItemsFailCode, err.Code()) + assert.Equal(t, "something in query should have at most 5 items", err.Error()) + assert.Equal(t, 6, err.Value) + + err = TooManyItems("something", "", 5, 6) + require.Error(t, err) + assert.EqualValues(t, MaxItemsFailCode, err.Code()) + assert.Equal(t, "something should have at most 5 items", err.Error()) + assert.Equal(t, 6, err.Value) + + err = TooFewItems("something", "", 5, 4) + require.Error(t, err) + assert.EqualValues(t, MinItemsFailCode, err.Code()) + assert.Equal(t, "something should have at least 5 items", err.Error()) + assert.Equal(t, 4, err.Value) + }) + + t.Run("with ExceedsMaximum", func(t *testing.T) { + err := ExceedsMaximumInt("something", "query", 5, false, 6) + require.Error(t, err) + assert.EqualValues(t, MaxFailCode, err.Code()) + assert.Equal(t, "something in query should be less than or equal to 5", err.Error()) + assert.Equal(t, 6, err.Value) + + err = ExceedsMaximumInt("something", "", 5, false, 6) + require.Error(t, err) + assert.EqualValues(t, MaxFailCode, err.Code()) + assert.Equal(t, "something should be less than or equal to 5", err.Error()) + assert.Equal(t, 6, err.Value) + + err = ExceedsMaximumInt("something", "query", 5, true, 6) + require.Error(t, err) + assert.EqualValues(t, MaxFailCode, err.Code()) + assert.Equal(t, "something in query should be less than 5", err.Error()) + assert.Equal(t, 6, err.Value) + + err = ExceedsMaximumInt("something", "", 5, true, 6) + require.Error(t, err) + assert.EqualValues(t, MaxFailCode, err.Code()) + assert.Equal(t, "something should be less than 5", err.Error()) + assert.Equal(t, 6, err.Value) + + err = ExceedsMaximumUint("something", "query", 5, false, 6) + require.Error(t, err) + assert.EqualValues(t, MaxFailCode, err.Code()) + assert.Equal(t, "something in query should be less than or equal to 5", err.Error()) + assert.Equal(t, 6, err.Value) + + err = ExceedsMaximumUint("something", "", 5, false, 6) + require.Error(t, err) + assert.EqualValues(t, MaxFailCode, err.Code()) + assert.Equal(t, "something should be less than or equal to 5", err.Error()) + assert.Equal(t, 6, err.Value) + + err = ExceedsMaximumUint("something", "query", 5, true, 6) + require.Error(t, err) + assert.EqualValues(t, MaxFailCode, err.Code()) + assert.Equal(t, "something in query should be less than 5", err.Error()) + assert.Equal(t, 6, err.Value) + + err = ExceedsMaximumUint("something", "", 5, true, 6) + require.Error(t, err) + assert.EqualValues(t, MaxFailCode, err.Code()) + assert.Equal(t, "something should be less than 5", err.Error()) + assert.Equal(t, 6, err.Value) + + err = ExceedsMaximum("something", "query", 5, false, 6) + require.Error(t, err) + assert.EqualValues(t, MaxFailCode, err.Code()) + assert.Equal(t, "something in query should be less than or equal to 5", err.Error()) + assert.Equal(t, 6, err.Value) + + err = ExceedsMaximum("something", "", 5, false, 6) + require.Error(t, err) + assert.EqualValues(t, MaxFailCode, err.Code()) + assert.Equal(t, "something should be less than or equal to 5", err.Error()) + assert.Equal(t, 6, err.Value) + + err = ExceedsMaximum("something", "query", 5, true, 6) + require.Error(t, err) + assert.EqualValues(t, MaxFailCode, err.Code()) + assert.Equal(t, "something in query should be less than 5", err.Error()) + assert.Equal(t, 6, err.Value) + + err = ExceedsMaximum("something", "", 5, true, 6) + require.Error(t, err) + assert.EqualValues(t, MaxFailCode, err.Code()) + assert.Equal(t, "something should be less than 5", err.Error()) + assert.Equal(t, 6, err.Value) + }) + + t.Run("with ExceedsMinimum", func(t *testing.T) { + err := ExceedsMinimumInt("something", "query", 5, false, 4) + require.Error(t, err) + assert.EqualValues(t, MinFailCode, err.Code()) + assert.Equal(t, "something in query should be greater than or equal to 5", err.Error()) + assert.Equal(t, 4, err.Value) + + err = ExceedsMinimumInt("something", "", 5, false, 4) + require.Error(t, err) + assert.EqualValues(t, MinFailCode, err.Code()) + assert.Equal(t, "something should be greater than or equal to 5", err.Error()) + assert.Equal(t, 4, err.Value) + + err = ExceedsMinimumInt("something", "query", 5, true, 4) + require.Error(t, err) + assert.EqualValues(t, MinFailCode, err.Code()) + assert.Equal(t, "something in query should be greater than 5", err.Error()) + assert.Equal(t, 4, err.Value) + + err = ExceedsMinimumInt("something", "", 5, true, 4) + require.Error(t, err) + assert.EqualValues(t, MinFailCode, err.Code()) + assert.Equal(t, "something should be greater than 5", err.Error()) + assert.Equal(t, 4, err.Value) + + err = ExceedsMinimumUint("something", "query", 5, false, 4) + require.Error(t, err) + assert.EqualValues(t, MinFailCode, err.Code()) + assert.Equal(t, "something in query should be greater than or equal to 5", err.Error()) + assert.Equal(t, 4, err.Value) + + err = ExceedsMinimumUint("something", "", 5, false, 4) + require.Error(t, err) + assert.EqualValues(t, MinFailCode, err.Code()) + assert.Equal(t, "something should be greater than or equal to 5", err.Error()) + assert.Equal(t, 4, err.Value) + + err = ExceedsMinimumUint("something", "query", 5, true, 4) + require.Error(t, err) + assert.EqualValues(t, MinFailCode, err.Code()) + assert.Equal(t, "something in query should be greater than 5", err.Error()) + assert.Equal(t, 4, err.Value) + + err = ExceedsMinimumUint("something", "", 5, true, 4) + require.Error(t, err) + assert.EqualValues(t, MinFailCode, err.Code()) + assert.Equal(t, "something should be greater than 5", err.Error()) + assert.Equal(t, 4, err.Value) + + err = ExceedsMinimum("something", "query", 5, false, 4) + require.Error(t, err) + assert.EqualValues(t, MinFailCode, err.Code()) + assert.Equal(t, "something in query should be greater than or equal to 5", err.Error()) + assert.Equal(t, 4, err.Value) + + err = ExceedsMinimum("something", "", 5, false, 4) + require.Error(t, err) + assert.EqualValues(t, MinFailCode, err.Code()) + assert.Equal(t, "something should be greater than or equal to 5", err.Error()) + assert.Equal(t, 4, err.Value) + + err = ExceedsMinimum("something", "query", 5, true, 4) + require.Error(t, err) + assert.EqualValues(t, MinFailCode, err.Code()) + assert.Equal(t, "something in query should be greater than 5", err.Error()) + assert.Equal(t, 4, err.Value) + + err = ExceedsMinimum("something", "", 5, true, 4) + require.Error(t, err) + assert.EqualValues(t, MinFailCode, err.Code()) + assert.Equal(t, "something should be greater than 5", err.Error()) + assert.Equal(t, 4, err.Value) + + err = NotMultipleOf("something", "query", 5, 1) + require.Error(t, err) + assert.EqualValues(t, MultipleOfFailCode, err.Code()) + assert.Equal(t, "something in query should be a multiple of 5", err.Error()) + assert.Equal(t, 1, err.Value) + }) + + t.Run("with MultipleOf", func(t *testing.T) { + err := NotMultipleOf("something", "query", float64(5), float64(1)) + require.Error(t, err) + assert.EqualValues(t, MultipleOfFailCode, err.Code()) + assert.Equal(t, "something in query should be a multiple of 5", err.Error()) + assert.Equal(t, float64(1), err.Value) + + err = NotMultipleOf("something", "query", uint64(5), uint64(1)) + require.Error(t, err) + assert.EqualValues(t, MultipleOfFailCode, err.Code()) + assert.Equal(t, "something in query should be a multiple of 5", err.Error()) + assert.Equal(t, uint64(1), err.Value) + + err = NotMultipleOf("something", "", 5, 1) + require.Error(t, err) + assert.EqualValues(t, MultipleOfFailCode, err.Code()) + assert.Equal(t, "something should be a multiple of 5", err.Error()) + assert.Equal(t, 1, err.Value) + + err = MultipleOfMustBePositive("path", "body", float64(-10)) + require.Error(t, err) + assert.EqualValues(t, MultipleOfMustBePositiveCode, err.Code()) + assert.Equal(t, `factor MultipleOf declared for path must be positive: -10`, err.Error()) + assert.Equal(t, float64(-10), err.Value) + + err = MultipleOfMustBePositive("path", "body", int64(-10)) + require.Error(t, err) + assert.EqualValues(t, MultipleOfMustBePositiveCode, err.Code()) + assert.Equal(t, `factor MultipleOf declared for path must be positive: -10`, err.Error()) + assert.Equal(t, int64(-10), err.Value) + }) + + t.Run("with EnumFail", func(t *testing.T) { + err := EnumFail("something", "query", "yada", []interface{}{"hello", "world"}) + require.Error(t, err) + assert.EqualValues(t, EnumFailCode, err.Code()) + assert.Equal(t, "something in query should be one of [hello world]", err.Error()) + assert.Equal(t, "yada", err.Value) + + err = EnumFail("something", "", "yada", []interface{}{"hello", "world"}) + require.Error(t, err) + assert.EqualValues(t, EnumFailCode, err.Code()) + assert.Equal(t, "something should be one of [hello world]", err.Error()) + assert.Equal(t, "yada", err.Value) + }) + + t.Run("with Required", func(t *testing.T) { + err := Required("something", "query", nil) + require.Error(t, err) + assert.EqualValues(t, RequiredFailCode, err.Code()) + assert.Equal(t, "something in query is required", err.Error()) + assert.Equal(t, nil, err.Value) + + err = Required("something", "", nil) + require.Error(t, err) + assert.EqualValues(t, RequiredFailCode, err.Code()) + assert.Equal(t, "something is required", err.Error()) + assert.Equal(t, nil, err.Value) + }) + + t.Run("with ReadOnly", func(t *testing.T) { + err := ReadOnly("something", "query", nil) + require.Error(t, err) + assert.EqualValues(t, ReadOnlyFailCode, err.Code()) + assert.Equal(t, "something in query is readOnly", err.Error()) + assert.Equal(t, nil, err.Value) + + err = ReadOnly("something", "", nil) + require.Error(t, err) + assert.EqualValues(t, ReadOnlyFailCode, err.Code()) + assert.Equal(t, "something is readOnly", err.Error()) + assert.Equal(t, nil, err.Value) + }) + + t.Run("with TooLong/TooShort", func(t *testing.T) { + err := TooLong("something", "query", 5, "abcdef") + require.Error(t, err) + assert.EqualValues(t, TooLongFailCode, err.Code()) + assert.Equal(t, "something in query should be at most 5 chars long", err.Error()) + assert.Equal(t, "abcdef", err.Value) + + err = TooLong("something", "", 5, "abcdef") + require.Error(t, err) + assert.EqualValues(t, TooLongFailCode, err.Code()) + assert.Equal(t, "something should be at most 5 chars long", err.Error()) + assert.Equal(t, "abcdef", err.Value) + + err = TooShort("something", "query", 5, "a") + require.Error(t, err) + assert.EqualValues(t, TooShortFailCode, err.Code()) + assert.Equal(t, "something in query should be at least 5 chars long", err.Error()) + assert.Equal(t, "a", err.Value) + + err = TooShort("something", "", 5, "a") + require.Error(t, err) + assert.EqualValues(t, TooShortFailCode, err.Code()) + assert.Equal(t, "something should be at least 5 chars long", err.Error()) + assert.Equal(t, "a", err.Value) + }) + + t.Run("with FailedPattern", func(t *testing.T) { + err := FailedPattern("something", "query", "\\d+", "a") + require.Error(t, err) + assert.EqualValues(t, PatternFailCode, err.Code()) + assert.Equal(t, "something in query should match '\\d+'", err.Error()) + assert.Equal(t, "a", err.Value) + + err = FailedPattern("something", "", "\\d+", "a") + require.Error(t, err) + assert.EqualValues(t, PatternFailCode, err.Code()) + assert.Equal(t, "something should match '\\d+'", err.Error()) + assert.Equal(t, "a", err.Value) + }) + + t.Run("with InvalidType", func(t *testing.T) { + err := InvalidTypeName("something") + require.Error(t, err) + assert.EqualValues(t, InvalidTypeCode, err.Code()) + assert.Equal(t, "something is an invalid type name", err.Error()) + }) + + t.Run("with AdditionalItemsNotAllowed", func(t *testing.T) { + err := AdditionalItemsNotAllowed("something", "query") + require.Error(t, err) + assert.EqualValues(t, NoAdditionalItemsCode, err.Code()) + assert.Equal(t, "something in query can't have additional items", err.Error()) + + err = AdditionalItemsNotAllowed("something", "") + require.Error(t, err) + assert.EqualValues(t, NoAdditionalItemsCode, err.Code()) + assert.Equal(t, "something can't have additional items", err.Error()) + }) + + err := InvalidCollectionFormat("something", "query", "yada") + require.Error(t, err) assert.EqualValues(t, InvalidTypeCode, err.Code()) assert.Equal(t, "the collection format \"yada\" is not supported for the query param \"something\"", err.Error()) - err2 := CompositeValidationError() - assert.Error(t, err2) - assert.EqualValues(t, CompositeErrorCode, err2.Code()) - assert.Equal(t, "validation failure list", err2.Error()) - - err2 = CompositeValidationError(fmt.Errorf("first error"), fmt.Errorf("second error")) - assert.Error(t, err2) - assert.EqualValues(t, CompositeErrorCode, err2.Code()) - assert.Equal(t, "validation failure list:\nfirst error\nsecond error", err2.Error()) - - // func MultipleOfMustBePositive(name, in string, factor interface{}) *Validation { - err = MultipleOfMustBePositive("path", "body", float64(-10)) - assert.Error(t, err) - assert.EqualValues(t, MultipleOfMustBePositiveCode, err.Code()) - assert.Equal(t, `factor MultipleOf declared for path must be positive: -10`, err.Error()) - assert.Equal(t, float64(-10), err.Value) - - err = MultipleOfMustBePositive("path", "body", int64(-10)) - assert.Error(t, err) - assert.EqualValues(t, MultipleOfMustBePositiveCode, err.Code()) - assert.Equal(t, `factor MultipleOf declared for path must be positive: -10`, err.Error()) - assert.Equal(t, int64(-10), err.Value) - - // func PropertyNotAllowed(name, in, key string) *Validation { - err = PropertyNotAllowed("path", "body", "key") - assert.Error(t, err) - assert.EqualValues(t, UnallowedPropertyCode, err.Code()) - // unallowedProperty = "%s.%s in %s is a forbidden property" - assert.Equal(t, "path.key in body is a forbidden property", err.Error()) - - err = PropertyNotAllowed("path", "", "key") - assert.Error(t, err) - assert.EqualValues(t, UnallowedPropertyCode, err.Code()) - // unallowedPropertyNoIn = "%s.%s is a forbidden property" - assert.Equal(t, "path.key is a forbidden property", err.Error()) - - // func TooManyProperties(name, in string, n int64) *Validation { - err = TooManyProperties("path", "body", 10) - assert.Error(t, err) - assert.EqualValues(t, TooManyPropertiesCode, err.Code()) - // tooManyProperties = "%s in %s should have at most %d properties" - assert.Equal(t, "path in body should have at most 10 properties", err.Error()) - - err = TooManyProperties("path", "", 10) - assert.Error(t, err) - assert.EqualValues(t, TooManyPropertiesCode, err.Code()) - // tooManyPropertiesNoIn = "%s should have at most %d properties" - assert.Equal(t, "path should have at most 10 properties", err.Error()) - - err = TooFewProperties("path", "body", 10) - // func TooFewProperties(name, in string, n int64) *Validation { - assert.Error(t, err) - assert.EqualValues(t, TooFewPropertiesCode, err.Code()) - // tooFewProperties = "%s in %s should have at least %d properties" - assert.Equal(t, "path in body should have at least 10 properties", err.Error()) - - err = TooFewProperties("path", "", 10) - // func TooFewProperties(name, in string, n int64) *Validation { - assert.Error(t, err) - assert.EqualValues(t, TooFewPropertiesCode, err.Code()) - // tooFewPropertiesNoIn = "%s should have at least %d properties" - assert.Equal(t, "path should have at least 10 properties", err.Error()) - - // func FailedAllPatternProperties(name, in, key string) *Validation { - err = FailedAllPatternProperties("path", "body", "key") - assert.Error(t, err) - assert.EqualValues(t, FailedAllPatternPropsCode, err.Code()) - // failedAllPatternProps = "%s.%s in %s failed all pattern properties" - assert.Equal(t, "path.key in body failed all pattern properties", err.Error()) - - err = FailedAllPatternProperties("path", "", "key") - assert.Error(t, err) - assert.EqualValues(t, FailedAllPatternPropsCode, err.Code()) - // failedAllPatternPropsNoIn = "%s.%s failed all pattern properties" - assert.Equal(t, "path.key failed all pattern properties", err.Error()) + t.Run("with CompositeValidationError", func(t *testing.T) { + err := CompositeValidationError() + require.Error(t, err) + assert.EqualValues(t, CompositeErrorCode, err.Code()) + assert.Equal(t, "validation failure list", err.Error()) + + err = CompositeValidationError(fmt.Errorf("first error"), fmt.Errorf("second error")) + require.Error(t, err) + assert.EqualValues(t, CompositeErrorCode, err.Code()) + assert.Equal(t, "validation failure list:\nfirst error\nsecond error", err.Error()) + }) + + t.Run("with PropertyNotAllowed", func(t *testing.T) { + err = PropertyNotAllowed("path", "body", "key") + require.Error(t, err) + assert.EqualValues(t, UnallowedPropertyCode, err.Code()) + // unallowedProperty = "%s.%s in %s is a forbidden property" + assert.Equal(t, "path.key in body is a forbidden property", err.Error()) + + err = PropertyNotAllowed("path", "", "key") + require.Error(t, err) + assert.EqualValues(t, UnallowedPropertyCode, err.Code()) + // unallowedPropertyNoIn = "%s.%s is a forbidden property" + assert.Equal(t, "path.key is a forbidden property", err.Error()) + }) + + t.Run("with TooMany/TooFew properties", func(t *testing.T) { + err := TooManyProperties("path", "body", 10) + require.Error(t, err) + assert.EqualValues(t, TooManyPropertiesCode, err.Code()) + // tooManyProperties = "%s in %s should have at most %d properties" + assert.Equal(t, "path in body should have at most 10 properties", err.Error()) + + err = TooManyProperties("path", "", 10) + require.Error(t, err) + assert.EqualValues(t, TooManyPropertiesCode, err.Code()) + // tooManyPropertiesNoIn = "%s should have at most %d properties" + assert.Equal(t, "path should have at most 10 properties", err.Error()) + + err = TooFewProperties("path", "body", 10) + require.Error(t, err) + assert.EqualValues(t, TooFewPropertiesCode, err.Code()) + // tooFewProperties = "%s in %s should have at least %d properties" + assert.Equal(t, "path in body should have at least 10 properties", err.Error()) + + err = TooFewProperties("path", "", 10) + require.Error(t, err) + assert.EqualValues(t, TooFewPropertiesCode, err.Code()) + // tooFewPropertiesNoIn = "%s should have at least %d properties" + assert.Equal(t, "path should have at least 10 properties", err.Error()) + }) + + t.Run("with PatternProperties", func(t *testing.T) { + err := FailedAllPatternProperties("path", "body", "key") + require.Error(t, err) + assert.EqualValues(t, FailedAllPatternPropsCode, err.Code()) + // failedAllPatternProps = "%s.%s in %s failed all pattern properties" + assert.Equal(t, "path.key in body failed all pattern properties", err.Error()) + + err = FailedAllPatternProperties("path", "", "key") + require.Error(t, err) + assert.EqualValues(t, FailedAllPatternPropsCode, err.Code()) + // failedAllPatternPropsNoIn = "%s.%s failed all pattern properties" + assert.Equal(t, "path.key failed all pattern properties", err.Error()) + }) }