Custom Validators
You can create custom validators by implementing theValidator interface.
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Creating custom validators in Ziva
Validator interface.
type Validator interface {
Validate(input string) error
}
type PasswordValidator struct {
MinLength int
}
func (v *PasswordValidator) Validate(input string) error {
if len(input) < v.MinLength {
return fmt.Errorf("password must be at least %d characters", v.MinLength)
}
return nil
}
// Usage
validator := &PasswordValidator{MinLength: 10}
input := ziva.NewInputTask("Password", "Enter password:").
WithValidator(validator)
type ChainValidator struct {
validators []Validator
}
func (c *ChainValidator) Validate(input string) error {
for _, v := range c.validators {
if err := v.Validate(input); err != nil {
return err
}
}
return nil
}
type ConditionalValidator struct {
condition func(string) bool
validator Validator
}
func (c *ConditionalValidator) Validate(input string) error {
if c.condition(input) {
return c.validator.Validate(input)
}
return nil
}