change validation schema

This commit is contained in:
DeniskaRediska committed 2019-10-31 16:58:34 +03:00
1 parent cf23b95af1
commit 5f219e9f0d
2 files changed
+19 -31

No files matched your search

+1
View File
@@ -26,6 +26,7 @@ type Config struct {
CreatedAt time.Time
UpdatedAt time.Time
Data map[string]interface{}
Unsafe bool
}
type CommonConfig struct {
+18 -31
View File
@@ -5,7 +5,7 @@ import (
"fmt"
"github.com/integration-system/isp-lib/config/schema"
"github.com/pkg/errors"
"github.com/xeipuuv/gojsonschema"
epb "google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"isp-ctl/cfg"
@@ -60,14 +60,25 @@ func (c configService) CreateUpdateConfig(stringToChange string, configuration *
}
}
if ok, err := c.checkSchema(configuration.ModuleId, newData); err != nil {
return nil, err
} else if !ok {
return nil, nil
}
configuration.Unsafe = c.UnsafeEnable
configuration.Data = newData
if resp, err := configClient.CreateUpdateConfig(*configuration); err != nil {
if errorStatus, ok := status.FromError(err); ok {
switch errorStatus.Code() {
case codes.InvalidArgument:
fmt.Print("doesn't match with schema:\n")
for _, value := range errorStatus.Details() {
if a, ok := value.(*epb.BadRequest); ok {
for _, value := range a.FieldViolations {
fmt.Printf("%s | %s\n", value.Field, value.Description)
}
} else {
fmt.Println(value)
}
return nil, nil
}
}
}
return nil, err
} else {
return resp.Data, nil
@@ -247,27 +258,3 @@ func (c configService) linked(configName, moduleName string,
}
return nil
}
func (c configService) checkSchema(moduleId string, object map[string]interface{}) (bool, error) {
if c.UnsafeEnable {
return true, nil
}
if resp, err := configClient.GetSchemaByModuleId(moduleId); err != nil {
return false, err
} else {
schemaLoader := gojsonschema.NewGoLoader(resp.Schema)
documentLoader := gojsonschema.NewGoLoader(object)
if result, err := gojsonschema.Validate(schemaLoader, documentLoader); err != nil {
return false, err
} else if result.Valid() {
return true, nil
} else {
for _, desc := range result.Errors() {
fmt.Printf("- %s\n", desc)
}
return false, nil
}
}
}