Skip to content

Commit 5b4e697

Browse files
authored
Merge pull request #10 from choria-io/9
(#9) Improve validators for integers and floats
2 parents 75656b4 + 0d29c6f commit 5b4e697

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

forms/forms.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,16 @@ func (p *processor) askFloatValue(prop Property) (float64, error) {
397397

398398
var ans string
399399

400+
validation := "isFloat(value)"
401+
if prop.ValidationExpression != "" {
402+
validation = fmt.Sprintf("%s && %s", validation, prop.ValidationExpression)
403+
}
404+
400405
err = survey.AskOne(&survey.Input{
401406
Message: prop.Name,
402407
Help: prop.Help,
403408
Default: prop.Default,
404-
}, &ans, survey.WithValidator(validator.SurveyValidator("isFloat(value)", true)))
409+
}, &ans, survey.WithValidator(validator.SurveyValidator(validation, true)))
405410
if err != nil {
406411
return 0, err
407412
}
@@ -420,11 +425,16 @@ func (p *processor) askIntValue(prop Property) (int, error) {
420425

421426
var ans string
422427

428+
validation := "isInt(value)"
429+
if prop.ValidationExpression != "" {
430+
validation = fmt.Sprintf("%s && %s", validation, prop.ValidationExpression)
431+
}
432+
423433
err = survey.AskOne(&survey.Input{
424434
Message: prop.Name,
425435
Help: prop.Help,
426436
Default: prop.Default,
427-
}, &ans, survey.WithValidator(validator.SurveyValidator("isInt(value)", true)))
437+
}, &ans, survey.WithValidator(validator.SurveyValidator(validation, true)))
428438
if err != nil {
429439
return 0, err
430440
}

internal/validator/validator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func FiskValidator(validation string) fisk.OptionValidator {
2323
}
2424

2525
if !ok {
26-
return fmt.Errorf("validation using %q did not pass", validation)
26+
return fmt.Errorf("validation did not pass: %s", validation)
2727
}
2828

2929
return nil
@@ -48,7 +48,7 @@ func SurveyValidator(validation string, required bool) func(any) error {
4848
}
4949

5050
if !ok {
51-
return fmt.Errorf("validation using %q did not pass", validation)
51+
return fmt.Errorf("validation did not pass: %s", validation)
5252
}
5353

5454
return nil

0 commit comments

Comments
 (0)