diff --git a/README.md b/README.md index 5289e22..d0b0be2 100644 --- a/README.md +++ b/README.md @@ -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 +``` \ No newline at end of file