Skip to content

Commit

Permalink
Merge pull request #19 from ashbeelghouri/0.5
Browse files Browse the repository at this point in the history
0.4.2.2
  • Loading branch information
ashbeelghouri committed Jul 1, 2024
2 parents ab50c60 + d5c16b0 commit 3c99e5c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
32 changes: 23 additions & 9 deletions data/v0/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,21 @@ func (f *Field) Validate(value interface{}, allValidators map[string]validators.
err.Value = value
err.ID = id
err.Validator = "unknown"
if f.Validators == nil {
err.AddMessage("en", "no validators defined")
return &err
}
for name, constants := range f.Validators {
err.Validator = name
Logs.DEBUG("Validator: ", name, constants)
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)
Logs.DEBUG("Field is required but value is null")
return &err
}

Expand All @@ -136,19 +142,27 @@ func (f *Field) Validate(value interface{}, allValidators map[string]validators.

var fn validators.Validator
fn, exists := allValidators[name]
Logs.DEBUG("function exists? ", exists)
if !exists {
Logs.ERROR("does not exists here!!", name)
Logs.ERROR("function not found", name)
err.AddMessage("en", "validator not registered")
return &err
}
if err1 := fn(value, constants.Attributes); err1 != nil {
if !(constants.Error != "" && f.L10n != nil) {
err.AddMessage("en", err1.Error())
return &err

fnError := fn(value, constants.Attributes)
Logs.DEBUG("fnError: ", fnError)
if fnError != nil {
if constants.Error != "" {
Logs.DEBUG("Custom Error is Defined", constants.Error)
err.AddMessage("en", constants.Error)
}
for locale, msg := range f.L10n {
if msg == nil {
err.AddMessage(locale, msg.(string))

if f.L10n != nil {
for locale, msg := range f.L10n {
if msg != nil {
Logs.DEBUG("L10n: ", locale, msg)
err.AddMessage(locale, msg.(string))
}
}
}
return &err
Expand Down
12 changes: 7 additions & 5 deletions data/v2/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v2

import (
"encoding/json"
"errors"
v0 "github.com/ashbeelghouri/jsonschematics/data/v0"
"github.com/ashbeelghouri/jsonschematics/operators"
"github.com/ashbeelghouri/jsonschematics/utils"
Expand Down Expand Up @@ -75,8 +76,12 @@ func LoadJsonSchemaFile(path string) (*v0.Schematics, error) {
return nil, err
}
s.Schema = schema

return transformSchematics(s), nil
baseSchematics := transformSchematics(s)
if baseSchematics != nil {
return baseSchematics, nil
} else {
return nil, errors.New("could not load the base schema")
}
}

func LoadMap(schemaMap interface{}) (*v0.Schematics, error) {
Expand All @@ -99,12 +104,9 @@ func LoadMap(schemaMap interface{}) (*v0.Schematics, error) {

func transformSchematics(s Schematics) *v0.Schematics {
var baseSchematics v0.Schematics
baseSchematics.Locale = s.Locale
baseSchematics.Logging = s.Logging
baseSchematics.ArrayIdKey = s.ArrayIdKey
baseSchematics.Separator = s.Separator
baseSchematics.Validators = s.Validators
baseSchematics.Operators = s.Operators
baseSchematics.Validators.BasicValidators()
baseSchematics.Operators.LoadBasicOperations()
baseSchematics.Schema = *transformSchema(s.Schema)
Expand Down

0 comments on commit 3c99e5c

Please sign in to comment.