-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Rhyanz46/extension
move example extension to another package
- Loading branch information
Showing
4 changed files
with
202 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,18 +25,26 @@ go get github.com/Rhyanz46/go-map-validator/map_validator | |
- IPv4 field check | ||
- IPv4 Network check | ||
- regex on string validation | ||
- you can create your own extension 🔥🔥🔥🔥 (example : https://github.com/Rhyanz46/go-map-validator/example_extensions/) | ||
- custom message : | ||
- on invalid regex message : ✅ | ||
- on type not match message : ✅ | ||
- on null data message : ❌ | ||
- on max data message : ❌ | ||
- on type not match message : ❌ | ||
- on enum value not match : ❌ | ||
|
||
## On Progress | ||
|
||
- extension code | ||
- validation for one data value only | ||
|
||
## Custom Message Variables | ||
|
||
| No | Variable Name | | ||
|:--:|:------------------:| | ||
| 1 | `${field}` | | ||
| 2 | `${expected_type}` | | ||
| 3 | `${actual_type}` | | ||
|
||
## Road Map | ||
|
||
- avoiding same value in some field | ||
|
@@ -77,8 +85,8 @@ if err == nil { | |
```go | ||
func handleLogin(c echo.Context) error { | ||
jsonHttp, err := map_validator.NewValidateBuilder().SetRules(map[string]map_validator.Rules{ | ||
"email": {Email: true, Max: map_validator.ToPointer[int](100)}, | ||
"password": {Type: reflect.String, Min: map_validator.ToPointer[int](6), Max: map_validator.ToPointer[int](30)}, | ||
"email": {Email: true, Max: map_validator.SetTotal(100)}, | ||
"password": {Type: reflect.String, Min: map_validator.SetTotal(6), Max: map_validator.SetTotal(30)}, | ||
}).LoadJsonHttp(c.Request()) | ||
if err != nil { | ||
return c.JSON(http.StatusBadRequest, err) | ||
|
@@ -130,3 +138,43 @@ if testBind.JK != payload["jenis_kelamin"] { | |
} | ||
|
||
``` | ||
|
||
|
||
### Example custom message | ||
```go | ||
payload := map[string]interface{}{"total": 12, "unit": "KG"} | ||
validRole := map[string]map_validator.Rules{ | ||
"total": { | ||
Type: reflect.Int, | ||
CustomMsg: map_validator.CustomMsg{ | ||
OnTypeNotMatch: map_validator.SetMessage("Total must be a number, but your input is ${actual_type}"), | ||
}, | ||
}, | ||
} | ||
check, err := map_validator.NewValidateBuilder().SetRules(validRole).Load(payload) | ||
if err != nil { | ||
t.Errorf("Expected not have error, but got error : %s", err) | ||
} | ||
_, err = check.RunValidate() | ||
if err != nil { | ||
t.Errorf("Expected not have error, but got error : %s", err) | ||
} | ||
``` | ||
|
||
|
||
### Example for regex validator | ||
```go | ||
payload := map[string]interface{}{"hp": "+62567888", "email": "[email protected]"} | ||
validRole := map[string]map_validator.Rules{ | ||
"hp": {RegexString: `^\+(?:\d{2}[- ]?\d{6}|\d{11})$`}, | ||
"email": {RegexString: `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`}, | ||
} | ||
check, err := map_validator.NewValidateBuilder().SetRules(validRole).Load(payload) | ||
if err != nil { | ||
t.Errorf("Expected not have error, but got error : %s", err) | ||
} | ||
_, err = check.RunValidate() | ||
if err != nil { | ||
t.Errorf("Expected not have error, but got error : %s", err) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package example_extensions | ||
|
||
import "github.com/Rhyanz46/go-map-validator/map_validator" | ||
|
||
type ExampleExtension struct { | ||
rules *map[string]map_validator.Rules | ||
data interface{} | ||
resetAfterValidation bool | ||
extraData *map_validator.ExtraOperationData | ||
} | ||
|
||
func (e *ExampleExtension) SetRoles(rules *map[string]map_validator.Rules) { | ||
e.rules = rules | ||
} | ||
|
||
func (e *ExampleExtension) BeforeLoad(data interface{}) error { | ||
//TODO implement me | ||
//panic("implement me") | ||
return nil | ||
} | ||
|
||
func (e *ExampleExtension) AfterLoad(data *map[string]interface{}) error { | ||
//TODO implement me | ||
//panic("implement me") | ||
return nil | ||
} | ||
|
||
func (e *ExampleExtension) BeforeValidation(data *map[string]interface{}) error { | ||
//TODO implement me | ||
//panic("implement me") | ||
return nil | ||
} | ||
|
||
func (e *ExampleExtension) AfterValidation(data *map[string]interface{}) error { | ||
if e.resetAfterValidation { | ||
empty := map[string]interface{}{} | ||
*data = empty | ||
} | ||
return nil | ||
} | ||
|
||
func (e *ExampleExtension) SetExtraData(data *map_validator.ExtraOperationData) map_validator.ExtensionType { | ||
e.extraData = data | ||
return e | ||
} | ||
|
||
func (e *ExampleExtension) ResetAfterValidation() *ExampleExtension { | ||
e.resetAfterValidation = true | ||
return e | ||
} | ||
|
||
func ManipulatorExt() *ExampleExtension { | ||
return &ExampleExtension{} | ||
} |
Oops, something went wrong.