go-ozzo / ozzo-validation

An idiomatic Go (golang) validation package. Supports configurable and extensible validation rules (validators) using normal language constructs instead of error-prone struct tags.
MIT License
3.73k stars 224 forks source link

about omitempty and NilOrNotEmpty #85

Closed aomirun closed 4 years ago

aomirun commented 4 years ago

package main

import ( "fmt" "os"

validation "github.com/go-ozzo/ozzo-validation"

)

type user struct { Name string json:"name,omitempty" Age int json:"age,omitempty" }

func init() { u := user{} if err := validation.ValidateStruct(&u, validation.Field(&u.Name, validation.NilOrNotEmpty, validation.Length(3, 20)), ); err != nil { fmt.Println(err) os.Exit(0) } } // ouput: name: cannot be blank.

qiangxue commented 4 years ago

This is expected because Name is an empty string and the rule requires it not empty. omitempty has nothing to do here.