Skip to content

Commit

Permalink
Merge pull request #24 from ashbeelghouri/0.5
Browse files Browse the repository at this point in the history
0.4.4
  • Loading branch information
ashbeelghouri authored Jul 7, 2024
2 parents d56820a + 2667fe0 commit c3d4aad
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 13 deletions.
10 changes: 8 additions & 2 deletions Basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@ import (
"testing"
)

func customFunc(i interface{}, attr map[string]interface{}) error {
log.Println("--------->>> ========== >>>>> attributes are: ", attr)
return nil
}

func TestV2Validate(t *testing.T) {
schematics, err := v2.LoadJsonSchemaFile("test-data/schema/direct/v2/example-2.json")
schematics, err := v2.LoadJsonSchemaFile("test-data/schema/direct/v2/example-1.json")
if err != nil {
t.Error(err)
}
schematics.Logging.PrintDebugLogs = true
schematics.Logging.PrintErrorLogs = true
schematics.Validators.RegisterValidator("NewFun", NewFun)
schematics.Validators.RegisterValidator("customFunc", customFunc)
content, err := os.ReadFile("test-data/data/direct/example.json")
if err != nil {
t.Error(err)
Expand All @@ -25,7 +31,7 @@ func TestV2Validate(t *testing.T) {
t.Error(err)
}
errs := schematics.Validate(jsonData)
log.Println(errs.GetStrings("en", "%message\n"))
log.Println(errs.GetStrings("ar", "%message\n"))
}
func NewFun(i interface{}, attr map[string]interface{}) error {
log.Println(i)
Expand Down
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,47 @@ examples:

[**NOTE**] if the string for target is a valid regex then above * conversion wont happen as in regular expressions, the asterisk (*) is a quantifier that means "zero or more" of the preceding element

#### Add Custom Data
- Add the global data inside the schematics right before you are executing the validate function, as it will propogate to the attributes of the function
- You can add them in the main Schematics Object or add it to the schema file as well as if you want to keep the values from the data use "add_to_db" in schema file to add your value to attributes

##### Example 1
Adding Data in Schematics

```go
var s Schematics
Schematics.DB = map[string]interface{}{
"my-data":"valueofthedata"
}
```
##### Example 2
Adding data globally in schema
```json
{ "fields": [...],
"DB": {
"test": 22
}
}
```

##### Example 3
Adding data from the json that is being validated
```json
{ "fields": [{
"name":"field name"
"target_key":"user.data",
"add_to_db": true,
}],
"DB": {
"test": 22
}
}
```

**Results** Data will be propogated to the attributes map[string]interface{} like below under "DB" key.
```go
constants.Attributes["DB"] = db
```

#### Go Version

Expand Down
45 changes: 41 additions & 4 deletions data/v0/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ type Schematics struct {
Separator string
ArrayIdKey string
Locale string
DB map[string]interface{}
Logging utils.Logger
}

// add this DB to the attributes as SCHEMA_GLOBAL_DB

type Schema struct {
Version string `json:"version"`
Fields map[TargetKey]Field `json:"fields"`
Version string `json:"version"`
Fields map[TargetKey]Field `json:"fields"`
DB map[string]interface{} `json:"DB"`
}

type Field struct {
Expand All @@ -35,6 +39,7 @@ type Field struct {
Name string `json:"name"`
Type string `json:"type"`
IsRequired bool `json:"required"`
AddToDB bool `json:"add_to_db"`
Description string `json:"description"`
Validators map[string]Constant `json:"validators"`
Operators map[string]Constant `json:"operators"`
Expand Down Expand Up @@ -116,7 +121,9 @@ func (s *Schematics) LoadMap(schemaMap interface{}) error {
return nil
}

func (f *Field) Validate(value interface{}, allValidators map[string]validators.Validator, id *string) *errorHandler.Error {
// if validators >>> if passed then do *

func (f *Field) Validate(value interface{}, allValidators map[string]validators.Validator, id *string, db map[string]interface{}) *errorHandler.Error {
var err errorHandler.Error
err.Value = value
err.ID = id
Expand Down Expand Up @@ -154,6 +161,10 @@ func (f *Field) Validate(value interface{}, allValidators map[string]validators.
return &err
}

if constants.Attributes == nil {
constants.Attributes = make(map[string]interface{})
}
constants.Attributes["DB"] = db
fnError := fn(value, constants.Attributes)
f.logging.DEBUG("fnError: ", fnError)
if fnError != nil {
Expand Down Expand Up @@ -236,6 +247,9 @@ func (s *Schematics) ValidateObject(jsonData *map[string]interface{}, id *string
uniqueID = *id
}
s.Logging.DEBUG("after unique id")

db := s.Schema.GetDB(flatData)

var missingFromDependants []string
for target, field := range s.Schema.Fields {
field.logging = s.Logging
Expand Down Expand Up @@ -271,7 +285,7 @@ func (s *Schematics) ValidateObject(jsonData *map[string]interface{}, id *string
}

for key, value := range matchingKeys {
validationError := field.Validate(value, s.Validators.ValidationFns, &uniqueID)
validationError := field.Validate(value, s.Validators.ValidationFns, &uniqueID, db)
s.Logging.DEBUG(validationError)
if validationError != nil {
errorMessages.AddError(key, *validationError)
Expand All @@ -286,6 +300,29 @@ func (s *Schematics) ValidateObject(jsonData *map[string]interface{}, id *string
return nil
}

// Corrected and completed GetDB function
func (s *Schema) GetDB(flatData map[string]interface{}) map[string]interface{} {
db := s.DB
for target, field := range s.Fields {
if field.AddToDB {
matchingKeys := utils.FindMatchingKeys(flatData, string(target))
if len(matchingKeys) < 2 {
mappedKey := utils.GetFirstFromMap(matchingKeys)
if mappedKey != nil {
db[string(target)] = mappedKey
}
} else if len(matchingKeys) > 0 {
var values []interface{}
for _, match := range matchingKeys {
values = append(values, match)
}
db[string(target)] = values
}
}
}
return db
}

func (s *Schematics) ValidateArray(jsonData []map[string]interface{}) *errorHandler.Errors {
s.Logging.DEBUG("validating the array")
var errs errorHandler.Errors
Expand Down
13 changes: 10 additions & 3 deletions data/v1/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@ type Schematics struct {
Separator string
ArrayIdKey string
Locale string
DB map[string]interface{}
Logging utils.Logger
}

type Schema struct {
Version string `json:"version"`
Fields []Field `json:"fields"`
Version string `json:"version"`
Fields []Field `json:"fields"`
DB map[string]interface{} `json:"DB"`
}

type Field struct {
DependsOn []string `json:"depends_on"`
DisplayName string `json:"display_name"`
Name string `json:"name"`
TargetKey string `json:"target_key"`
AddToDB bool `json:"add_to_db"`
Type string `json:"type"`
IsRequired bool `json:"required"`
Description string `json:"description"`
Expand Down Expand Up @@ -114,19 +117,23 @@ func transformSchematics(s Schematics) *v0.Schematics {
baseSchematics.Validators.BasicValidators()
baseSchematics.Operators.LoadBasicOperations()
baseSchematics.Schema = *transformSchema(s.Schema)

if s.DB != nil {
baseSchematics.Schema.DB = utils.CombineTwoMaps(baseSchematics.Schema.DB, s.DB)
}
return &baseSchematics
}

func transformSchema(schema Schema) *v0.Schema {
var baseSchema v0.Schema
baseSchema.Version = schema.Version
baseSchema.DB = schema.DB
baseSchema.Fields = make(map[v0.TargetKey]v0.Field)
for _, field := range schema.Fields {
baseSchema.Fields[v0.TargetKey(field.TargetKey)] = v0.Field{
DependsOn: field.DependsOn,
Name: field.Name,
Type: field.Name,
AddToDB: field.AddToDB,
IsRequired: field.IsRequired,
Description: field.Description,
Validators: transformComponents(field.Validators),
Expand Down
12 changes: 10 additions & 2 deletions data/v2/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@ type Schematics struct {
Separator string
ArrayIdKey string
Locale string
DB map[string]interface{}
Logging utils.Logger
}

type Schema struct {
Version string `json:"version"`
Fields []Field `json:"fields"`
Version string `json:"version"`
Fields []Field `json:"fields"`
DB map[string]interface{} `json:"DB"`
}

type Field struct {
DependsOn []string `json:"depends_on"`
DisplayName string `json:"display_name"`
Name string `json:"name"`
TargetKey string `json:"target_key"`
AddToDB bool `json:"add_to_db"`
Type string `json:"type"`
IsRequired bool `json:"required"`
Description string `json:"description"`
Expand Down Expand Up @@ -100,18 +103,23 @@ func transformSchematics(s Schematics) *v0.Schematics {
baseSchematics.Validators.BasicValidators()
baseSchematics.Operators.LoadBasicOperations()
baseSchematics.Schema = *transformSchema(s.Schema)
if s.DB != nil {
baseSchematics.Schema.DB = utils.CombineTwoMaps(baseSchematics.Schema.DB, s.DB)
}
return &baseSchematics
}

func transformSchema(schema Schema) *v0.Schema {
var baseSchema v0.Schema
baseSchema.Version = schema.Version
baseSchema.DB = schema.DB
baseSchema.Fields = make(map[v0.TargetKey]v0.Field)

for _, field := range schema.Fields {
baseSchema.Fields[v0.TargetKey(field.TargetKey)] = v0.Field{
DependsOn: field.DependsOn,
Name: field.Name,
AddToDB: field.AddToDB,
Type: field.Name,
IsRequired: field.IsRequired,
Description: field.Description,
Expand Down
11 changes: 9 additions & 2 deletions test-data/schema/direct/v2/example-1.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
{
"version": "2",
"fields": [{
"fields": [
{
"name": "",
"display_name": "",
"required": false,
"add_to_db": true,
"depends_on": [],
"target_key": "user.profile.name.first",
"description": "",
"validators": [{
"name": "IsString"
}, {
"name": "customFunc"
}, {
"name": "MaxLengthAllowed",
"attributes": {
Expand Down Expand Up @@ -82,5 +86,8 @@
"operators": [{
"name": "Capitalize"
}]
}]
}],
"DB": {
"test": 22
}
}
19 changes: 19 additions & 0 deletions utils/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ func FindMatchingKeys(data map[string]interface{}, keyPattern string) map[string
return matchingKeys
}

func GetFirstFromMap(mapped map[string]interface{}) interface{} {
for _, m := range mapped {
return m
}
return nil
}

func IsValidJson(content []byte) (string, interface{}) {
var arr []map[string]interface{}
var obj map[string]interface{}
Expand Down Expand Up @@ -230,3 +237,15 @@ func getJsonFileAsMapArray(content []byte) ([]map[string]interface{}, error) {
}
return data, nil
}

func CombineTwoMaps(map1 map[string]interface{}, map2 map[string]interface{}) map[string]interface{} {
if len(map1) < 1 {
map1 = make(map[string]interface{})
}

finalMap := map1
for key, m := range map2 {
finalMap[key] = m
}
return finalMap
}

0 comments on commit c3d4aad

Please sign in to comment.