Skip to content

Commit

Permalink
Merge pull request #16 from ashbeelghouri/0.5
Browse files Browse the repository at this point in the history
0.4.1
  • Loading branch information
ashbeelghouri authored Jul 1, 2024
2 parents ece319c + b263b47 commit b255f32
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
9 changes: 8 additions & 1 deletion Basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
}
24 changes: 21 additions & 3 deletions data/v0/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions errorHandler/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 3 additions & 2 deletions utils/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
type Logger struct {
PrintDebugLogs bool
PrintErrorLogs bool
PreText string
}

func (l *Logger) DEBUG(v ...interface{}) {
Expand All @@ -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))
}
}

Expand All @@ -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))
}
}

0 comments on commit b255f32

Please sign in to comment.