hasValidations returns true if a UserType contains validations.
(attCtx *AttributeContext, ut expr.UserType)
| 469 | |
| 470 | // hasValidations returns true if a UserType contains validations. |
| 471 | func hasValidations(attCtx *AttributeContext, ut expr.UserType) bool { |
| 472 | // We need to check empirically whether there are validations to be |
| 473 | // generated. We can't call recurseValidationCode() to avoid infinite |
| 474 | // recursions, but we can use validationCode() for the local (non-recursive) |
| 475 | // attribute-level checks — it is the source of truth for whether a given |
| 476 | // attribute produces any validation output, including any skips (e.g. |
| 477 | // format checks on struct:field:type attributes). For nested user types |
| 478 | // and required-field checks we keep the structural walk. |
| 479 | res := false |
| 480 | done := errors.New("done") |
| 481 | Walk(ut.Attribute(), func(a *expr.AttributeExpr) error { // nolint: errcheck |
| 482 | if a.Validation == nil { |
| 483 | return nil |
| 484 | } |
| 485 | // Use validationCode() as the source of truth for whether this |
| 486 | // attribute produces any local validation output. This naturally |
| 487 | // handles all codegen skips (e.g. Format on struct:field:type) |
| 488 | // without hasValidations() needing to mirror those conditions. |
| 489 | if validationCode(a, attCtx, true, false, "x", "x") != "" { |
| 490 | res = true |
| 491 | return done |
| 492 | } |
| 493 | return nil |
| 494 | }) |
| 495 | return res |
| 496 | } |
| 497 | |
| 498 | // There is a case where there is validation but no actual validation code: if |
| 499 | // the validation is a required validation that applies to attributes that |
no test coverage detected