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

Function TestNestedStructValidation

validator_test.go:15245–15420  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

15243}
15244
15245func TestNestedStructValidation(t *testing.T) {
15246 validator := New(WithRequiredStructEnabled())
15247
15248 t.Run("required", func(t *testing.T) {
15249 type (
15250 value struct {
15251 Field string
15252 }
15253 topLevel struct {
15254 Nested value `validate:"required"`
15255 }
15256 )
15257
15258 var validationErrs ValidationErrors
15259 if errs := validator.Struct(topLevel{}); errs != nil {
15260 validationErrs = errs.(ValidationErrors)
15261 }
15262
15263 Equal(t, 1, len(validationErrs))
15264 AssertError(t, validationErrs, "topLevel.Nested", "topLevel.Nested", "Nested", "Nested", "required")
15265
15266 Equal(t, validator.Struct(topLevel{value{"potato"}}), nil)
15267 })
15268
15269 t.Run("omitempty", func(t *testing.T) {
15270 type (
15271 value struct {
15272 Field string
15273 }
15274 topLevel struct {
15275 Nested value `validate:"omitempty,required"`
15276 }
15277 )
15278
15279 errs := validator.Struct(topLevel{})
15280 Equal(t, errs, nil)
15281 })
15282
15283 t.Run("excluded_if", func(t *testing.T) {
15284 type (
15285 value struct {
15286 Field string
15287 }
15288 topLevel struct {
15289 Field string
15290 Nested value `validate:"excluded_if=Field potato"`
15291 }
15292 )
15293
15294 errs := validator.Struct(topLevel{Field: "test", Nested: value{"potato"}})
15295 Equal(t, errs, nil)
15296
15297 errs = validator.Struct(topLevel{Field: "potato"})
15298 Equal(t, errs, nil)
15299
15300 errs = validator.Struct(topLevel{Field: "potato", Nested: value{"potato"}})
15301 AssertError(t, errs, "topLevel.Nested", "topLevel.Nested", "Nested", "Nested", "excluded_if")
15302 })

Callers

nothing calls this directly

Calls 6

NewFunction · 0.85
AssertErrorFunction · 0.85
StructMethod · 0.80
RegisterValidationMethod · 0.80
FieldMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…