Skip to content

Commit

Permalink
add SchemaService.Validate to validate arbitrary entity
Browse files Browse the repository at this point in the history
  • Loading branch information
randmonkey committed May 10, 2024
1 parent 2c2b379 commit 2b98025
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
- [0.2.0](#020)
- [0.1.0](#010)

## Unreleased

- Add `Validate` method in `SchemaService` to validate arbitrary type of Kong
entities.
[#443](https://github.com/Kong/go-kong/pull/443)

## [v0.54.0]

> Release date: 2024/04/03
Expand Down
28 changes: 28 additions & 0 deletions kong/schema_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package kong

import (
"context"
"errors"
"fmt"
"net/http"
)

// AbstractSchemaService handles schemas in Kong.
type AbstractSchemaService interface {
// Get fetches an entity schema from Kong.
Get(ctx context.Context, entity string) (Schema, error)
// Validate validates an arbitrary entity in Kong.
// Used for custom entities, or entities that does not have a Validate method in the corresponding service.
Validate(ctx context.Context, entityType string, entity interface{}) (bool, string, error)
}

// SchemaService handles schemas in Kong.
Expand All @@ -17,6 +22,8 @@ type SchemaService service
// Schema represents an entity schema in Kong.
type Schema map[string]interface{}

var _ AbstractSchemaService = &SchemaService{}

// Get retrieves the full schema of kong entities.
func (s *SchemaService) Get(ctx context.Context, entity string) (Schema, error) {
req, err := s.client.NewRequest("GET", fmt.Sprintf("/schemas/%s", entity), nil, nil)
Expand All @@ -30,3 +37,24 @@ func (s *SchemaService) Get(ctx context.Context, entity string) (Schema, error)
}
return schema, nil
}

// Validate validates an arbitrary entity in Kong.
func (s *SchemaService) Validate(ctx context.Context, entityType string, entity interface{}) (bool, string, error) {
endpoint := fmt.Sprintf("/schemas/%s/validate", entityType)
req, err := s.client.NewRequest("POST", endpoint, nil, entity)
if err != nil {
return false, "", err
}
resp, err := s.client.Do(ctx, req, nil)
if err != nil {
if resp == nil {
return false, "", err
}
var apiErr *APIError
if ok := errors.As(err, &apiErr); !ok || apiErr.Code() != http.StatusBadRequest {
return false, "", err
}
return false, apiErr.message, nil
}
return true, "", nil
}
49 changes: 49 additions & 0 deletions kong/schema_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,52 @@ func TestSchemaService(T *testing.T) {
assert.NoError(err)
}
}

func TestSchemaServiceValidate(t *testing.T) {
assert := assert.New(t)

client, err := NewTestClient(nil, nil)
assert.NoError(err)
assert.NotNil(client)

testCases := []struct {
name string
entityType string
entity any
valid bool
}{
{
name: "valid service should pass the validation",
entityType: "services",
entity: &Service{
Name: String("test.service"),
Host: String("foo.com"),
},
valid: true,
},
{
name: "invalid service (invalid protocol) should fail the validation",
entity: &Service{
Name: String("test.service"),
Host: String("foo.com"),
Protocol: String("not-exist-protocol"),
},
valid: false,
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {

Check warning on line 71 in kong/schema_service_test.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 't' seems to be unused, consider removing or renaming it as _ (revive)
valid, msg, err := client.Schemas.Validate(defaultCtx, tc.entityType, tc.entity)
assert.NoError(err)
if tc.valid {
assert.True(valid)
assert.Empty(msg)
} else {
assert.False(valid)
assert.NotEmpty(msg)
}
})
}
}

0 comments on commit 2b98025

Please sign in to comment.