Code Monkey home page Code Monkey logo

Comments (6)

jos- avatar jos- commented on August 16, 2024 2

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.

thedevsaddam avatar thedevsaddam commented on August 16, 2024 1

@jos- This would be more convenient if the field is present then apply validation rule, otherwise (field absent) skip the validation rule.

from govalidator.

thedevsaddam avatar thedevsaddam commented on August 16, 2024

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.

Arteneko avatar Arteneko commented on August 16, 2024

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.

thedevsaddam avatar thedevsaddam commented on August 16, 2024

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.

Arteneko avatar Arteneko commented on August 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.