From 221d0bf3e43d60e8b1720ffedc6566e2e7f7fe0f Mon Sep 17 00:00:00 2001 From: asbheelghouri Date: Mon, 1 Jul 2024 23:18:39 +0400 Subject: [PATCH] Logs --- Basic_test.go | 7 +-- data/v0/schema.go | 61 +++++++++++------------ data/v2/schema.go | 30 +++-------- test-data/data/direct/v2/example-3.json | 60 ++++++++++++++++++++++ test-data/schema/direct/v2/example-2.json | 45 +++++++++++++++++ 5 files changed, 147 insertions(+), 56 deletions(-) create mode 100644 test-data/data/direct/v2/example-3.json create mode 100644 test-data/schema/direct/v2/example-2.json diff --git a/Basic_test.go b/Basic_test.go index 1dd587f..bb50a3c 100644 --- a/Basic_test.go +++ b/Basic_test.go @@ -9,12 +9,14 @@ import ( ) func TestV2Validate(t *testing.T) { - schematics, err := v2.LoadJsonSchemaFile("test-data/schema/direct/v2/example-1.json") + schematics, err := v2.LoadJsonSchemaFile("test-data/schema/direct/v2/example-2.json") if err != nil { t.Error(err) } + schematics.Logging.PrintDebugLogs = true + schematics.Logging.PrintErrorLogs = true schematics.Validators.RegisterValidator("NewFun", NewFun) - content, err := os.ReadFile("test-data/data/direct/v2/example.json") + content, err := os.ReadFile("test-data/data/direct/v2/example-3.json") if err != nil { t.Error(err) } @@ -25,7 +27,6 @@ func TestV2Validate(t *testing.T) { errs := schematics.Validate(jsonData) log.Println(errs.GetStrings("en", "%data\n")) } - func NewFun(i interface{}, attr map[string]interface{}) error { log.Println(i) log.Println(attr) diff --git a/data/v0/schema.go b/data/v0/schema.go index 81aa280..e8fc7f5 100644 --- a/data/v0/schema.go +++ b/data/v0/schema.go @@ -12,8 +12,6 @@ import ( "strings" ) -var Logs utils.Logger - type TargetKey string type Schematics struct { @@ -42,6 +40,7 @@ type Field struct { Operators map[string]Constant `json:"operators"` L10n map[string]interface{} `json:"l10n"` AdditionalInformation map[string]interface{} `json:"additional_information"` + logging utils.Logger } type Constant struct { @@ -51,31 +50,30 @@ type Constant struct { } func (s *Schematics) Configs() { - Logs = s.Logging if s.Logging.PrintDebugLogs { log.Println("debugger is on") } if s.Logging.PrintErrorLogs { log.Println("error logging is on") } - s.Validators.Logger = Logs - s.Operators.Logger = Logs + s.Validators.Logger = s.Logging + s.Operators.Logger = s.Logging } func (s *Schematics) LoadJsonSchemaFile(path string) error { s.Configs() content, err := os.ReadFile(path) if err != nil { - Logs.ERROR("Failed to load schema file", err) + s.Logging.ERROR("Failed to load schema file", err) return err } var schema Schema err = json.Unmarshal(content, &schema) if err != nil { - Logs.ERROR("Failed to unmarshall schema file", err) + s.Logging.ERROR("Failed to unmarshall schema file", err) return err } - Logs.DEBUG("Schema Loaded From File: ", schema) + s.Logging.DEBUG("Schema Loaded From File: ", schema) s.Schema = schema s.Validators.BasicValidators() s.Operators.LoadBasicOperations() @@ -91,16 +89,16 @@ func (s *Schematics) LoadJsonSchemaFile(path string) error { func (s *Schematics) LoadMap(schemaMap interface{}) error { JSON, err := json.Marshal(schemaMap) if err != nil { - Logs.ERROR("Schema should be valid json map[string]interface", err) + s.Logging.ERROR("Schema should be valid json map[string]interface", err) return err } var schema Schema err = json.Unmarshal(JSON, &schema) if err != nil { - Logs.ERROR("Invalid Schema", err) + s.Logging.ERROR("Invalid Schema", err) return err } - Logs.DEBUG("Schema Loaded From MAP: ", schema) + s.Logging.DEBUG("Schema Loaded From MAP: ", schema) s.Schema = schema s.Validators.BasicValidators() s.Operators.LoadBasicOperations() @@ -124,15 +122,15 @@ func (f *Field) Validate(value interface{}, allValidators map[string]validators. } for name, constants := range f.Validators { err.Validator = name - Logs.DEBUG("Validator: ", name, constants) + f.logging.DEBUG("Validator: ", name, constants) if name != "" { - Logs.DEBUG("Name of the validator is not given: ", name) + f.logging.DEBUG("Name of the validator is not given: ", name) err.Validator = name } if f.IsRequired && value == nil { err.Validator = "Required" err.AddMessage("en", "this is a required field") - Logs.DEBUG("Field is required but value is null") + f.logging.DEBUG("Field is required but value is null") return &err } @@ -142,25 +140,25 @@ func (f *Field) Validate(value interface{}, allValidators map[string]validators. var fn validators.Validator fn, exists := allValidators[name] - Logs.DEBUG("function exists? ", exists) + f.logging.DEBUG("function exists? ", exists) if !exists { - Logs.ERROR("function not found", name) + f.logging.ERROR("function not found", name) err.AddMessage("en", "validator not registered") return &err } fnError := fn(value, constants.Attributes) - Logs.DEBUG("fnError: ", fnError) + f.logging.DEBUG("fnError: ", fnError) if fnError != nil { if constants.Error != "" { - Logs.DEBUG("Custom Error is Defined", constants.Error) + f.logging.DEBUG("Custom Error is Defined", constants.Error) err.AddMessage("en", constants.Error) } if f.L10n != nil { for locale, msg := range f.L10n { if msg != nil { - Logs.DEBUG("L10n: ", locale, msg) + f.logging.DEBUG("L10n: ", locale, msg) err.AddMessage(locale, msg.(string)) } } @@ -212,22 +210,23 @@ func (s *Schematics) Validate(jsonData interface{}) *errorHandler.Errors { } func (s *Schematics) ValidateObject(jsonData *map[string]interface{}, id *string) *errorHandler.Errors { - Logs.DEBUG("validating the object") + s.Logging.DEBUG("validating the object") var errorMessages errorHandler.Errors var baseError errorHandler.Error flatData := *s.makeFlat(*jsonData) - Logs.DEBUG("here after flat data --> ", flatData) + s.Logging.DEBUG("here after flat data --> ", flatData) uniqueID := "" if id != nil { uniqueID = *id } - Logs.DEBUG("after unique id") + s.Logging.DEBUG("after unique id") var missingFromDependants []string for target, field := range s.Schema.Fields { + field.logging = s.Logging baseError.Validator = "is-required" matchingKeys := utils.FindMatchingKeys(flatData, string(target)) - Logs.DEBUG("matching keys --> ", matchingKeys) + s.Logging.DEBUG("matching keys --> ", matchingKeys) if len(matchingKeys) == 0 { if field.IsRequired { baseError.AddMessage("en", "this field is required") @@ -235,14 +234,14 @@ func (s *Schematics) ValidateObject(jsonData *map[string]interface{}, id *string } continue } - Logs.DEBUG("after is required --> ", matchingKeys) + s.Logging.DEBUG("after is required --> ", matchingKeys) // check for dependencies if len(field.DependsOn) > 0 { missing := false for _, d := range field.DependsOn { matchDependsOn := utils.FindMatchingKeys(flatData, d) if !(utils.StringInStrings(string(target), missingFromDependants) == false && len(matchDependsOn) > 0) { - Logs.DEBUG("matched depends on", matchDependsOn) + s.Logging.DEBUG("matched depends on", matchDependsOn) baseError.Validator = "depends-on" baseError.AddMessage("en", "this field depends on other values which do not exists") errorMessages.AddError(string(target), baseError) @@ -258,7 +257,7 @@ func (s *Schematics) ValidateObject(jsonData *map[string]interface{}, id *string for key, value := range matchingKeys { validationError := field.Validate(value, s.Validators.ValidationFns, &uniqueID) - Logs.DEBUG(validationError) + s.Logging.DEBUG(validationError) if validationError != nil { errorMessages.AddError(key, *validationError) } @@ -273,7 +272,7 @@ func (s *Schematics) ValidateObject(jsonData *map[string]interface{}, id *string } func (s *Schematics) ValidateArray(jsonData []map[string]interface{}) *errorHandler.Errors { - Logs.DEBUG("validating the array") + s.Logging.DEBUG("validating the array") var errs errorHandler.Errors i := 0 for _, d := range jsonData { @@ -289,7 +288,7 @@ func (s *Schematics) ValidateArray(jsonData []map[string]interface{}) *errorHand id := arrayId.(string) errorMessages = s.ValidateObject(&d, &id) if errorMessages.HasErrors() { - Logs.ERROR("has errors", errorMessages.GetStrings("en", "%data\n")) + s.Logging.ERROR("has errors", errorMessages.GetStrings("en", "%data\n")) errs.MergeErrors(errorMessages) } i = i + 1 @@ -307,7 +306,7 @@ func (f *Field) Operate(value interface{}, allOperations map[string]operators.Op for operationName, operationConstants := range f.Operators { customValidator, exists := allOperations[operationName] if !exists { - Logs.ERROR("This operation does not exists in basic or custom operators", operationName) + f.logging.ERROR("This operation does not exists in basic or custom operators", operationName) return nil } result := customValidator(value, operationConstants.Attributes) @@ -324,7 +323,7 @@ func (s *Schematics) Operate(data interface{}) (interface{}, *errorHandler.Error baseError.Validator = "operate-on-schema" bytes, err := json.Marshal(data) if err != nil { - Logs.ERROR("[operate] error converting the data into bytes", err) + s.Logging.ERROR("[operate] error converting the data into bytes", err) baseError.AddMessage("en", "data is not valid json") errorMessages.AddError("whole-data", baseError) return nil, &errorMessages @@ -332,7 +331,7 @@ func (s *Schematics) Operate(data interface{}) (interface{}, *errorHandler.Error dataType, item := utils.IsValidJson(bytes) if item == nil { - Logs.ERROR("[operate] error occurred when checking if this data is an array or object") + s.Logging.ERROR("[operate] error occurred when checking if this data is an array or object") baseError.AddMessage("en", "can not convert the data into json") errorMessages.AddError("whole-data", baseError) return nil, &errorMessages diff --git a/data/v2/schema.go b/data/v2/schema.go index 6465b9b..7c6e9e5 100644 --- a/data/v2/schema.go +++ b/data/v2/schema.go @@ -7,12 +7,9 @@ import ( "github.com/ashbeelghouri/jsonschematics/operators" "github.com/ashbeelghouri/jsonschematics/utils" "github.com/ashbeelghouri/jsonschematics/validators" - "log" "os" ) -var Logs utils.Logger - type Schematics struct { Schema Schema Validators validators.Validators @@ -49,30 +46,15 @@ type Component struct { L10n map[string]interface{} `json:"l10n"` } -func (s *Schematics) Configs() { - Logs = s.Logging - if s.Logging.PrintDebugLogs { - log.Println("debugger is on") - } - if s.Logging.PrintErrorLogs { - log.Println("error logging is on") - } - s.Validators.Logger = Logs - s.Operators.Logger = Logs -} - func LoadJsonSchemaFile(path string) (*v0.Schematics, error) { var s Schematics - s.Configs() content, err := os.ReadFile(path) if err != nil { - Logs.ERROR("Failed to load schema file", err) return nil, err } var schema Schema err = json.Unmarshal(content, &schema) if err != nil { - Logs.ERROR("Failed to unmarshall schema file", err) return nil, err } s.Schema = schema @@ -86,16 +68,13 @@ func LoadJsonSchemaFile(path string) (*v0.Schematics, error) { func LoadMap(schemaMap interface{}) (*v0.Schematics, error) { var s Schematics - s.Configs() jsonBytes, err := json.Marshal(schemaMap) if err != nil { - Logs.ERROR("Schema should be valid json map[string]interface", err) return nil, err } var schema Schema err = json.Unmarshal(jsonBytes, &schema) if err != nil { - Logs.ERROR("Failed to unmarshall schema file", err) return nil, err } s.Schema = schema @@ -104,7 +83,13 @@ func LoadMap(schemaMap interface{}) (*v0.Schematics, error) { func transformSchematics(s Schematics) *v0.Schematics { var baseSchematics v0.Schematics - baseSchematics.Logging = s.Logging + if s.Logging.PrintDebugLogs { + baseSchematics.Logging.PrintDebugLogs = true + } + if s.Logging.PrintErrorLogs { + baseSchematics.Logging.PrintErrorLogs = true + } + baseSchematics.ArrayIdKey = s.ArrayIdKey baseSchematics.Separator = s.Separator baseSchematics.Validators.BasicValidators() @@ -117,6 +102,7 @@ func transformSchema(schema Schema) *v0.Schema { var baseSchema v0.Schema baseSchema.Version = schema.Version baseSchema.Fields = make(map[v0.TargetKey]v0.Field) + for _, field := range schema.Fields { baseSchema.Fields[v0.TargetKey(field.TargetKey)] = v0.Field{ DependsOn: field.DependsOn, diff --git a/test-data/data/direct/v2/example-3.json b/test-data/data/direct/v2/example-3.json new file mode 100644 index 0000000..f36ca4c --- /dev/null +++ b/test-data/data/direct/v2/example-3.json @@ -0,0 +1,60 @@ +{ + "name": "iphone 15 - Blue - 128", + "phone": [ + { + "label": "home", + "hidden": false, + "number": "+971523906401" + } + ], + "Search": { + "tags": [ + "iphone", + "15", + "blue", + "phone" + ] + }, + "saleor": { + "channelID": "", + "productID": "", + "variantID": "", + "warehouseID": "" + }, + "features": [ + { + "name": "Color", + "value": "Blue", + "additional_data": {} + }, + { + "name": "Storage", + "value": "128", + "additional_data": {} + }, + { + "name": "Height", + "value": "5.81 in", + "additional_data": {} + }, + { + "name": "Width", + "value": "71.6 in", + "additional_data": {} + }, + { + "name": "Depth", + "value": "0.31 in", + "additional_data": {} + }, + { + "name": "Weight", + "value": "171 g", + "additional_data": {} + } + ], + "quantity": 1, + "product_id": "2688f465-689c-4c55-9e77-c8c932e4043d", + "category_id": "10749d14-89b6-4c4a-a03c-873ecb1d3cb3", + "description": "Iphone 15, color is blue and have 256 GB of storage" +} \ No newline at end of file diff --git a/test-data/schema/direct/v2/example-2.json b/test-data/schema/direct/v2/example-2.json new file mode 100644 index 0000000..7e67e76 --- /dev/null +++ b/test-data/schema/direct/v2/example-2.json @@ -0,0 +1,45 @@ +{ + "fields": [ + { + "display_name": "Quantity", + "name": "Quantity", + "type": "number", + "required": true, + "description": "check the quantity for 0 values", + "depends_on": [], + "target_key": "quantity", + "validators": [ + { + "name": "IsNumber", + "error": "quantity should be numeric value" + }, + { + "name": "MinAllowed", + "error": "least 1 item should be provided as a quantity to create a variant", + "attributes": { + "min": 1 + } + } + ] + }, + { + "name": "Product ID", + "type": "string", + "required": false, + "description": "check if this is a valid product", + "depends_on": [], + "target_key": "product_id", + "validators": [ + { + "name": "IsString", + "error": "Product id should be a string" + }, + { + "name": "ValidProductID", + "error": "This product does not exists in our system" + } + ] + } + ], + "version": "2" +} \ No newline at end of file