MCPcopy
hub / github.com/go-playground/validator / UserStructLevelValidation

Function UserStructLevelValidation

_examples/struct-level/main.go:167–177  ·  view source on GitHub ↗

UserStructLevelValidation contains custom struct level validations that don't always make sense at the field validation level. For example, this function validates that either FirstName or LastName exist; could have done that with a custom field validation but then would have had to add it to both f

(sl validator.StructLevel)

Source from the content-addressed store, hash-verified

165// hooks right into validator and you can combine with validation tags and still have a
166// common error output format.
167func UserStructLevelValidation(sl validator.StructLevel) {
168
169 user := sl.Current().Interface().(User)
170
171 if len(user.FirstName) == 0 && len(user.LastName) == 0 {
172 sl.ReportError(user.FirstName, "fname", "FirstName", "fnameorlname", "")
173 sl.ReportError(user.LastName, "lname", "LastName", "fnameorlname", "")
174 }
175
176 // plus can do more, even with different tag than "fnameorlname"
177}

Callers

nothing calls this directly

Calls 2

CurrentMethod · 0.65
ReportErrorMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…