diff --git a/Basic_test.go b/Basic_test.go index a620286..1dd587f 100644 --- a/Basic_test.go +++ b/Basic_test.go @@ -13,7 +13,8 @@ func TestV2Validate(t *testing.T) { if err != nil { t.Error(err) } - content, err := os.ReadFile("test-data/data/direct/v2/example-2.json") + schematics.Validators.RegisterValidator("NewFun", NewFun) + content, err := os.ReadFile("test-data/data/direct/v2/example.json") if err != nil { t.Error(err) } @@ -24,3 +25,9 @@ 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) + return nil +} diff --git a/data/v0/schema.go b/data/v0/schema.go index c3b56b1..cbaaa9d 100644 --- a/data/v0/schema.go +++ b/data/v0/schema.go @@ -75,6 +75,7 @@ func (s *Schematics) LoadJsonSchemaFile(path string) error { Logs.ERROR("Failed to unmarshall schema file", err) return err } + Logs.DEBUG("Schema Loaded From File: ", schema) s.Schema = schema s.Validators.BasicValidators() s.Operators.LoadBasicOperations() @@ -99,6 +100,7 @@ func (s *Schematics) LoadMap(schemaMap interface{}) error { Logs.ERROR("Invalid Schema", err) return err } + Logs.DEBUG("Schema Loaded From MAP: ", schema) s.Schema = schema s.Validators.BasicValidators() s.Operators.LoadBasicOperations() @@ -118,11 +120,13 @@ func (f *Field) Validate(value interface{}, allValidators map[string]validators. err.Validator = "unknown" for name, constants := range f.Validators { if name != "" { + Logs.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("ERR: ", err) return &err } @@ -180,10 +184,22 @@ func (s *Schematics) Validate(jsonData interface{}) *errorHandler.Errors { return &errs } if dataType == "object" { - obj := item.(map[string]interface{}) + obj, ok := item.(map[string]interface{}) + if !ok { + baseError.AddMessage("en", "invalid format provided for the data, can only be map[string]interface or []map[string]interface") + errs.AddError("whole-data-obj", baseError) + return &errs + } + Logs.DEBUG("validating the object", obj) return s.ValidateObject(obj, nil) } else { - arr := item.([]map[string]interface{}) + arr, ok := item.([]map[string]interface{}) + if !ok { + baseError.AddMessage("en", "invalid format provided for the data, can only be map[string]interface or []map[string]interface") + errs.AddError("whole-data-arr", baseError) + return &errs + } + Logs.DEBUG("validating the array", arr) return s.ValidateArray(arr) } } @@ -226,6 +242,8 @@ func (s *Schematics) ValidateObject(jsonData map[string]interface{}, id *string) for key, value := range matchingKeys { validationError := field.Validate(value, s.Validators.ValidationFns, id) + Logs.DEBUG(validationError) + Logs.DEBUG("validation error with pointer", *validationError) if validationError != nil { errorMessages.AddError(key, *validationError) } @@ -240,7 +258,7 @@ func (s *Schematics) ValidateObject(jsonData map[string]interface{}, id *string) } func (s *Schematics) ValidateArray(jsonData []map[string]interface{}) *errorHandler.Errors { - log.Println("validating the array") + Logs.DEBUG("validating the array") var errs errorHandler.Errors i := 0 for _, d := range jsonData { diff --git a/errorHandler/messages.go b/errorHandler/messages.go index 5fd6ca1..4051c6e 100644 --- a/errorHandler/messages.go +++ b/errorHandler/messages.go @@ -32,10 +32,10 @@ func (e *Error) AddMessage(local string, message string) { } func (e *Error) updateData(target string) Target { var t string - convertedID, ok := e.ID.(*string) + convertedID, ok := e.ID.(string) if ok && e.ID != nil { - t = fmt.Sprintf("%s:%s", *convertedID, target) + t = fmt.Sprintf("%s:%s", convertedID, target) } else { t = fmt.Sprintf("%s", target) } diff --git a/utils/logger.go b/utils/logger.go index f93dc57..d0608ff 100644 --- a/utils/logger.go +++ b/utils/logger.go @@ -8,6 +8,7 @@ import ( type Logger struct { PrintDebugLogs bool PrintErrorLogs bool + PreText string } func (l *Logger) DEBUG(v ...interface{}) { @@ -16,7 +17,7 @@ func (l *Logger) DEBUG(v ...interface{}) { if err != nil { log.Println(err) } - log.Println(string(bytes)) + log.Println(l.PreText, string(bytes)) } } @@ -26,6 +27,6 @@ func (l *Logger) ERROR(v ...interface{}) { if err != nil { log.Println(err) } - log.Println("------ [ERROR] ------", string(bytes)) + log.Println(l.PreText, "------ [ERROR] ------", string(bytes)) } }