Skip to content

Commit

Permalink
skip number validator on zero-default
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Nov 17, 2022
1 parent 3b9a9df commit 341bcc4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
8 changes: 4 additions & 4 deletions forms/validators/record_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (validator *RecordDataValidator) checkFieldValue(field *schema.SchemaField,
func (validator *RecordDataValidator) checkTextValue(field *schema.SchemaField, value any) error {
val, _ := value.(string)
if val == "" {
return nil // nothing to check
return nil // nothing to check (skip zero-defaults)
}

options, _ := field.Options.(*schema.TextOptions)
Expand All @@ -159,11 +159,11 @@ func (validator *RecordDataValidator) checkTextValue(field *schema.SchemaField,
}

func (validator *RecordDataValidator) checkNumberValue(field *schema.SchemaField, value any) error {
if value == nil {
return nil // nothing to check
val, _ := value.(float64)
if val == 0 {
return nil // nothing to check (skip zero-defaults)
}

val, _ := value.(float64)
options, _ := field.Options.(*schema.NumberOptions)

if options.Min != nil && val < *options.Min {
Expand Down
11 changes: 10 additions & 1 deletion forms/validators/record_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func TestRecordDataValidatorValidateNumber(t *testing.T) {
// create new test collection
collection := &models.Collection{}
collection.Name = "validate_test"
min := 0.0
min := 2.0
max := 150.0
collection.Schema = schema.NewSchema(
&schema.SchemaField{
Expand Down Expand Up @@ -244,6 +244,15 @@ func TestRecordDataValidatorValidateNumber(t *testing.T) {
nil,
[]string{"field3"},
},
{
"(number) check min with zero-default",
map[string]any{
"field2": 1,
"field3": 0,
},
nil,
[]string{},
},
{
"(number) check max constraint",
map[string]any{
Expand Down

0 comments on commit 341bcc4

Please sign in to comment.