From 5f219e9f0d2d06165964c0aec72bd823e7d0980f Mon Sep 17 00:00:00 2001 From: DeniskaRediska Date: Thu, 31 Oct 2019 16:58:34 +0300 Subject: [PATCH] change validation schema --- cfg/domain.go | 1 + service/config.go | 49 +++++++++++++++++------------------------------ 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/cfg/domain.go b/cfg/domain.go index 4702fe0..57411f2 100644 --- a/cfg/domain.go +++ b/cfg/domain.go @@ -26,6 +26,7 @@ type Config struct { CreatedAt time.Time UpdatedAt time.Time Data map[string]interface{} + Unsafe bool } type CommonConfig struct { diff --git a/service/config.go b/service/config.go index b1b489e..5229c10 100644 --- a/service/config.go +++ b/service/config.go @@ -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 - } - } -}