From d8f42bd7e62c470564fba90a17142adc72c88d1b Mon Sep 17 00:00:00 2001 From: Francisco Delmar Kurpiel Date: Wed, 22 Oct 2025 20:41:27 +0200 Subject: [PATCH] feat(xtypes): Add validation function to String type The `xtypes.String` type previously included a `ValueValid` method that was a no-op, always returning `nil`. This prevented consumers from implementing custom validation logic for string values. This commit introduces an optional `ValidateFn` field to the `String` struct. This function, if provided, is executed by the `ValueValid` method to determine if a given string value is valid. If the `ValidateFn` is not set, `ValueValid` returns `nil` as before, ensuring that existing code continues to function without modification. This enhancement makes the `String` type more flexible and allows for more robust input validation where specific formats or constraints are required. --- xtypes/string.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/xtypes/string.go b/xtypes/string.go index 4c449f4..d27b5c7 100644 --- a/xtypes/string.go +++ b/xtypes/string.go @@ -10,6 +10,7 @@ import ( type String struct { DefaultValue string UpdateFn func(string) + ValidateFn func(string) error content struct { value *string mutex sync.Mutex @@ -52,8 +53,12 @@ func (d *String) Value() string { // ValueValid test if the provided parameter value is valid. Has no side // effects. -func (d *String) ValueValid(_ string) error { - return nil +func (d *String) ValueValid(v string) error { + if d.ValidateFn == nil { + return nil + } + + return d.ValidateFn(v) } // GetDefaultValue will be used to read the default value when showing usage