Skip to content

Commit

Permalink
fix incorrect error returned (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
bailinhe authored Nov 3, 2023
1 parent e7663f4 commit 6ab4ca5
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1 deletion.
108 changes: 108 additions & 0 deletions pkg/jsonschema/compiler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package jsonschema

import (
"context"
"testing"

"github.com/stretchr/testify/suite"
)

type CompilerTestSuite struct {
suite.Suite
}

func (s *CompilerTestSuite) TestCompile() {
tests := []struct {
name string
expectedErr string
erdSlugPlural string
erdVersion string
schema string
}{
{
name: "ok",
schema: `{
"$id": "v1.canada-geese.test-ex-1",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Canada Goose",
"type": "object",
"unique": [
"firstName",
"lastName"
],
"required": [
"firstName",
"lastName"
],
"properties": {
"firstName": {
"type": "string",
"description": "The goose's first name.",
"ui": {
"hide": true
}
},
"lastName": {
"type": "string",
"description": "The goose's last name."
},
"age": {
"description": "Age in years which must be equal to or greater than zero.",
"type": "integer",
"minimum": 0
}
}
}`,
expectedErr: "",
erdSlugPlural: "canada-geese",
erdVersion: "v1alpha1",
},
{
name: "test schema with required missing",
schema: `
{
"$id": "v1.hello-world.extension-example.governor.equinixmetal.com",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"unique": [
"name"
],
"properties": {
"name": {
"default": "world",
"description": "hello, name",
"type": "string"
}
},
"title": "Greeting",
"type": "object"
}
`,
expectedErr: `cannot apply unique constraint when "required" is not provided`,
erdSlugPlural: "greetings",
erdVersion: "v1alpha1",
},
}

for _, tt := range tests {
compiler := NewCompiler(
"extension-validator", tt.erdSlugPlural, tt.erdVersion,
WithUniqueConstraint(
context.Background(),
nil, nil, nil,
),
)

_, err := compiler.Compile(tt.schema)

if tt.expectedErr != "" {
s.Require().Error(err)
s.Require().Contains(err.Error(), tt.expectedErr)
} else {
s.Require().NoError(err)
}
}
}

func TestCompilerSuite(t *testing.T) {
suite.Run(t, new(CompilerTestSuite))
}
10 changes: 9 additions & 1 deletion pkg/jsonschema/unique_constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,15 @@ func (uc *UniqueConstraintCompiler) Compile(
return nil, nil
}

requiredFields, err := assertStringSlice(m["required"])
required, ok := m["required"]
if !ok {
return nil, fmt.Errorf(
`%w: cannot apply unique constraint when "required" is not provided`,
ErrInvalidUniqueProperty,
)
}

requiredFields, err := assertStringSlice(required)
if err != nil {
return nil, err
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/jsonschema/unique_constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ func (s *UniqueConstrainTestSuite) TestCompile() {
},
expectedErr: "",
},
{
name: "unique exists but required missing",
inputMap: map[string]interface{}{
"unique": []interface{}{"a"},
},
expectedErr: `cannot apply unique constraint when "required" is not provided`,
},
{
name: "unique exists but required invalid",
inputMap: map[string]interface{}{
Expand Down

0 comments on commit 6ab4ca5

Please sign in to comment.