change validation schema
This commit is contained in:
1 parent
cf23b95af1
commit
5f219e9f0d
2 files changed
+19
-31
No files matched your search
@@ -26,6 +26,7 @@ type Config struct {
|
|||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
Data map[string]interface{}
|
Data map[string]interface{}
|
||||||
|
Unsafe bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type CommonConfig struct {
|
type CommonConfig struct {
|
||||||
|
|||||||
+18
-31
@@ -5,7 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/integration-system/isp-lib/config/schema"
|
"github.com/integration-system/isp-lib/config/schema"
|
||||||
"github.com/pkg/errors"
|
"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/codes"
|
||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
"isp-ctl/cfg"
|
"isp-ctl/cfg"
|
||||||
@@ -60,14 +60,25 @@ func (c configService) CreateUpdateConfig(stringToChange string, configuration *
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ok, err := c.checkSchema(configuration.ModuleId, newData); err != nil {
|
configuration.Unsafe = c.UnsafeEnable
|
||||||
return nil, err
|
|
||||||
} else if !ok {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
configuration.Data = newData
|
configuration.Data = newData
|
||||||
if resp, err := configClient.CreateUpdateConfig(*configuration); err != nil {
|
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
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
return resp.Data, nil
|
return resp.Data, nil
|
||||||
@@ -247,27 +258,3 @@ func (c configService) linked(configName, moduleName string,
|
|||||||
}
|
}
|
||||||
return nil
|
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user