Comments (6)
For form data, it's much more convenient if input can be validated only if non-empty.
It would be great if there were a nullable
rule for optional fields, such as Laravel has.
from govalidator.
@jos- This would be more convenient if the field is present then apply validation rule, otherwise (field absent) skip the validation rule.
from govalidator.
If you are validating form-data
this code should work.
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/thedevsaddam/govalidator"
)
func handler(w http.ResponseWriter, r *http.Request) {
rules := govalidator.MapData{
"email": []string{"email"},
}
opts := govalidator.Options{
Request: r, // request object
Rules: rules, // rules map
RequiredDefault: false, // all the field to be pass the rules
}
v := govalidator.New(opts)
e := v.Validate()
err := map[string]interface{}{"validationError": e}
w.Header().Set("Content-type", "application/json")
json.NewEncoder(w).Encode(err)
}
func main() {
http.HandleFunc("/", handler)
fmt.Println("Listening on port: 9999")
http.ListenAndServe(":9999", nil)
}
and if you are validating json payload
this code should work:
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/thedevsaddam/govalidator"
)
func handler(w http.ResponseWriter, r *http.Request) {
rules := govalidator.MapData{
"email": []string{"email"},
}
data := struct {
Email string `json:"email"`
}{}
opts := govalidator.Options{
Request: r, // request object
Rules: rules, // rules map
Data: &data,
RequiredDefault: false, // all the field to be pass the rules
}
v := govalidator.New(opts)
e := v.ValidateJSON()
err := map[string]interface{}{"validationError": e}
w.Header().Set("Content-type", "application/json")
json.NewEncoder(w).Encode(err)
}
func main() {
http.HandleFunc("/", handler)
fmt.Println("Listening on port: 9999")
http.ListenAndServe(":9999", nil)
}
Go Version: go version go1.10.3 darwin/amd64
Govalidator: v1.9.1
Note: If the field is present and the value is empty/invalid then the validator will throw an error. But if the field is absent in payload then it will not throw validation error.
from govalidator.
I'm stuck with the same problem for the url
validation type on an optional field.
I always obtain validation errors because the field is empty while I only gave the url
validator ("PersonalWebsite": []string{"url"},
).
from govalidator.
I'm stuck with the same problem for the
url
validation type on an optional field.I always obtain validation errors because the field is empty while I only gave the
url
validator ("PersonalWebsite": []string{"url"},
).
Remove the field if empty from client end, if the field present in the body then it'll be stuck by validation rule
from govalidator.
Remove the field if empty from client end, if the field present in the body then it'll be stuck by validation rule
That seems hacky imo, and that also means that you'd have to do a first request form handling round to build a temporary url.Values
set of non-empty fields.
(after trying out a few existing validation libraries, I built my own, which does the job for me)
from govalidator.
Related Issues (20)
- How can I verify the uint64 type? I canβt verify it with numeric. HOT 1
- decimal.Decimal field can not be parsed correctly.
- Reject superfluous properties
- How to use it on GoFiber? HOT 3
- confirm password validation HOT 1
- numeric_between not considering float value in range.
- How to validate with nested struct ? HOT 1
- Support for Validating multiple files.
- go get error,constant -9223372036854775808 overflows int
- Regex for blank space is not working
- Is there any way to check if a custom rule already exists or not? HOT 2
- How to validate slice of struct ?
- how to validate struct in struct? HOT 5
- Invalid Read on closed Body
- Can I use this with Echo? HOT 1
- Nested validation doesn't working HOT 1
- Unmarshalling failure happens before validation failure HOT 1
- Why not custom message with rule between when value is float32 and float64?
- Map validated query\form to a struct. HOT 2
- Latitude & Longitude doesn't validate
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from govalidator.