From f413f376be0399e57ab6090777d6aabddb3bffd4 Mon Sep 17 00:00:00 2001 From: Adam Chalkley Date: Mon, 27 Mar 2023 05:49:12 -0500 Subject: [PATCH] Refactor TopLevelCard.Validate - pull out version field validation to separate helper func - use validator.Validator functionality This set of changes updates validation for this type to match most other types in this package. Further work remains for other types. --- adaptivecard/adaptivecard.go | 106 +++++++++++++++++++---------------- 1 file changed, 57 insertions(+), 49 deletions(-) diff --git a/adaptivecard/adaptivecard.go b/adaptivecard/adaptivecard.go index b2e7a65..482d0e7 100644 --- a/adaptivecard/adaptivecard.go +++ b/adaptivecard/adaptivecard.go @@ -983,61 +983,19 @@ func (c Card) Validate() error { // Validate asserts that fields have valid values. func (tc TopLevelCard) Validate() error { + v := validator.Validator{} + // Validate embedded Card first as those validation requirements apply // here also. - if err := tc.Card.Validate(); err != nil { - return err - } + v.SelfValidate(tc.Card) // The Version field is required for top-level cards (this one), optional // for Cards nested within an Action.ShowCard. - switch { - case strings.TrimSpace(tc.Version) == "": - return fmt.Errorf( - "required field Version is empty for top-level Card: %w", - ErrMissingValue, - ) - default: - // Assert that Version value can be converted to the expected format. - versionNum, err := strconv.ParseFloat(tc.Version, 64) - if err != nil { - return fmt.Errorf( - "value %q incompatible with Version field: %w", - tc.Version, - ErrInvalidFieldValue, - ) - } - - // This is a high confidence validation failure. - if versionNum < AdaptiveCardMinVersion { - return fmt.Errorf( - "unsupported version %q;"+ - " expected minimum value of %0.1f: %w", - tc.Version, - AdaptiveCardMinVersion, - ErrInvalidFieldValue, - ) - } - - // This is *NOT* a high confidence validation failure; it is likely - // that Microsoft Teams will gain support for future versions of the - // Adaptive Card greater than the current recorded max configured - // schema version. Because the max value constant is subject to fall - // out of sync (at least briefly), this is a risky assertion to make. - // - // if versionNum < AdaptiveCardMinVersion || versionNum > AdaptiveCardMaxVersion { - // return fmt.Errorf( - // "unsupported version %q;"+ - // " expected value between %0.1f and %0.1f: %w", - // tc.Version, - // AdaptiveCardMinVersion, - // AdaptiveCardMaxVersion, - // ErrInvalidFieldValue, - // ) - // } - } + v.SuccessfulFuncCall( + func() error { return assertValidVersionFieldValue(tc.Version) }, + ) - return nil + return v.Err() } // Validate asserts that the collection of Element values are all valid. @@ -2225,3 +2183,53 @@ func assertColumnWidthValidValues(c Column) error { return nil } + +func assertValidVersionFieldValue(val string) error { + switch { + case strings.TrimSpace(val) == "": + return fmt.Errorf( + "required field Version is empty for top-level Card: %w", + ErrMissingValue, + ) + default: + // Assert that Version value can be converted to the expected format. + versionNum, err := strconv.ParseFloat(val, 64) + if err != nil { + return fmt.Errorf( + "value %q incompatible with Version field: %w", + val, + ErrInvalidFieldValue, + ) + } + + // This is a high confidence validation failure. + if versionNum < AdaptiveCardMinVersion { + return fmt.Errorf( + "unsupported version %q;"+ + " expected minimum value of %0.1f: %w", + val, + AdaptiveCardMinVersion, + ErrInvalidFieldValue, + ) + } + + // This is *NOT* a high confidence validation failure; it is likely + // that Microsoft Teams will gain support for future versions of the + // Adaptive Card greater than the current recorded max configured + // schema version. Because the max value constant is subject to fall + // out of sync (at least briefly), this is a risky assertion to make. + // + // if versionNum < AdaptiveCardMinVersion || versionNum > AdaptiveCardMaxVersion { + // return fmt.Errorf( + // "unsupported version %q;"+ + // " expected value between %0.1f and %0.1f: %w", + // tc.Version, + // AdaptiveCardMinVersion, + // AdaptiveCardMaxVersion, + // ErrInvalidFieldValue, + // ) + // } + } + + return nil +}