Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update readme #7

Merged
merged 2 commits into from
Feb 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 69 additions & 71 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,121 +10,119 @@ examples : https://github.com/Rhyanz46/go-map-validator-examples
go get github.com/Rhyanz46/go-map-validator/map_validator
```

## Features

- validate value in `map[string]interface{}` by keys
- validate data from `http.Request` json/multipart
- support file upload
- enum value check
- min/max length data check
- email field check
- uuid field check
- IPv4 field check
- IPv4 Network check
- regex on string validation
- custom message :
- on invalid regex message : ✅
- on null data message : ❌
- on max data message : ❌
- on type not match message : ❌
- on enum value not match : ❌

## On Progress

- validation for one data value only

## Road Map

- avoiding same value in some field
- ex case : `old_password` and `new_password`
- get from urls params http
- validation for `base64`
- handle file size on multipart
- extension for generate OpenAPI Spec that support with this package
- image resolution validation
- nested data


## example :

### Example 1

```go
payload := map[string]interface{}{"jenis_kelamin": "laki-laki", "hoby": "Main PS", "umur": 1, "menikah": true}
err := map_validator.NewValidateBuilder().SetRules(map[string]map_validator.Rules{
"jenis_kelamin": {Enum: &map_validator.EnumField[any]{Items: []string{"laki-laki", "perempuan"}}},
"hoby": {Type: reflect.String, Null: false},
"menikah": {Type: reflect.Bool, Null: false},
"jenis_kelamin": {Enum: &map_validator.EnumField[any]{Items: []string{"laki-laki", "perempuan"}}},
"hoby": {Type: reflect.String, Null: false},
"menikah": {Type: reflect.Bool, Null: false},
}).Load(payload).RunValidate()
if err != nil {
t.Errorf("Expected not have error, but got error : %s", err)
t.Errorf("Expected not have error, but got error : %s", err)
}

err = map_validator.NewValidateBuilder().SetRules(map[string]map_validator.Rules{
"jenis_kelamin": {Enum: &map_validator.EnumField[any]{Items: []string{"laki-laki", "perempuan"}}},
"hoby": {Type: reflect.Int, Null: false},
"menikah": {Type: reflect.Bool, Null: false},
"jenis_kelamin": {Enum: &map_validator.EnumField[any]{Items: []string{"laki-laki", "perempuan"}}},
"hoby": {Type: reflect.Int, Null: false},
"menikah": {Type: reflect.Bool, Null: false},
}).Load(payload).RunValidate()
if err == nil {
t.Error("Expected have an error, but you got no error")
t.Error("Expected have an error, but you got no error")
}
```

### Example Echo Framework

```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)},
}).LoadJsonHttp(c.Request())
if err != nil {
return c.JSON(http.StatusBadRequest, err)
}
err = jsonHttp.RunValidate()
if err != nil {
return c.JSON(http.StatusBadRequest, err)
}
return c.NoContent(http.StatusOK)
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)},
}).LoadJsonHttp(c.Request())
if err != nil {
return c.JSON(http.StatusBadRequest, err)
}
err = jsonHttp.RunValidate()
if err != nil {
return c.JSON(http.StatusBadRequest, err)
}
return c.NoContent(http.StatusOK)
}

func main() {
e := echo.New()
e.POST("/login", handleLogin)
e.Start(":3000")
e := echo.New()
e.POST("/login", handleLogin)
e.Start(":3000")
}

```

### Example Bind To Struct

```go
type Data struct {
JK string `map_validator:"jenis_kelamin"`
Hoby string `map_validator:"hoby"`
Menikah bool `map_validator:"menikah"`
JK string `map_validator:"jenis_kelamin"`
Hoby string `map_validator:"hoby"`
Menikah bool `map_validator:"menikah"`
}

payload := map[string]interface{}{"jenis_kelamin": "laki-laki", "hoby": "Main PS", "umur": 1, "menikah": true}
err := map_validator.NewValidateBuilder().SetRules(map[string]map_validator.Rules{
"jenis_kelamin": {Enum: &map_validator.EnumField[any]{Items: []string{"laki-laki", "perempuan"}}},
"hoby": {Type: reflect.String, Null: false},
"menikah": {Type: reflect.Bool, Null: false},
"jenis_kelamin": {Enum: &map_validator.EnumField[any]{Items: []string{"laki-laki", "perempuan"}}},
"hoby": {Type: reflect.String, Null: false},
"menikah": {Type: reflect.Bool, Null: false},
}).Load(payload).RunValidate()
if err != nil {
t.Errorf("Expected not have error, but got error : %s", err)
t.Errorf("Expected not have error, but got error : %s", err)
}

testBind := &Data{}
if testBind.JK != "" {
t.Errorf("Expected : '' But you got : %s", testBind.JK)
t.Errorf("Expected : '' But you got : %s", testBind.JK)
}
err = extraCheck.Bind(testBind)
if err != nil {
t.Errorf("Error : %s ", err)
t.Errorf("Error : %s ", err)
}

if testBind.JK != payload["jenis_kelamin"] {
t.Errorf("Expected : %s But you got : %s", payload["jenis_kelamin"], testBind.JK)
t.Errorf("Expected : %s But you got : %s", payload["jenis_kelamin"], testBind.JK)
}

```

## Features

- validate value in `map[string]interface{}` by keys
- validate data from `http.Request` json/multipart
- support file upload
- enum value check
- min/max length data check
- email field check
- uuid field check
- IPv4 field check
- IPv4 Network check
- regex on string validation
- custom message :
- on invalid regex message : ✅
- on null data message : ❌
- on max data message : ❌
- on type not match message : ❌
- on enum value not match : ❌

## On Progress

- validation for one data value only

## Road Map

- avoiding same value in some field
- ex case : `old_password` and `new_password`
- get from urls params http
- validation for `base64`
- handle file size on multipart
- extension for generate OpenAPI Spec that support with this package
- image resolution validation
- nested data
```
Loading