You've already forked pocketbase
mirror of
https://github.com/pocketbase/pocketbase.git
synced 2025-11-06 09:29:19 +02:00
[#3113] added NoDecimal number field option
This commit is contained in:
@@ -158,6 +158,10 @@ func (validator *RecordDataValidator) checkNumberValue(field *schema.SchemaField
|
||||
|
||||
options, _ := field.Options.(*schema.NumberOptions)
|
||||
|
||||
if options.NoDecimals && val != float64(int64(val)) {
|
||||
return validation.NewError("validation_no_decimals_constraint", "Decimal numbers are not allowed")
|
||||
}
|
||||
|
||||
if options.Min != nil && val < *options.Min {
|
||||
return validation.NewError("validation_min_number_constraint", fmt.Sprintf("Must be larger than %f", *options.Min))
|
||||
}
|
||||
|
||||
@@ -180,6 +180,13 @@ func TestRecordDataValidatorValidateNumber(t *testing.T) {
|
||||
Max: &max,
|
||||
},
|
||||
},
|
||||
&schema.SchemaField{
|
||||
Name: "field4",
|
||||
Type: schema.FieldTypeNumber,
|
||||
Options: &schema.NumberOptions{
|
||||
NoDecimals: true,
|
||||
},
|
||||
},
|
||||
)
|
||||
if err := app.Dao().SaveCollection(collection); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -201,6 +208,7 @@ func TestRecordDataValidatorValidateNumber(t *testing.T) {
|
||||
"field1": nil,
|
||||
"field2": nil,
|
||||
"field3": nil,
|
||||
"field4": nil,
|
||||
},
|
||||
nil,
|
||||
[]string{"field2"},
|
||||
@@ -211,6 +219,7 @@ func TestRecordDataValidatorValidateNumber(t *testing.T) {
|
||||
"field1": "invalid",
|
||||
"field2": "invalid",
|
||||
"field3": "invalid",
|
||||
"field4": "invalid",
|
||||
},
|
||||
nil,
|
||||
[]string{"field2"},
|
||||
@@ -244,6 +253,15 @@ func TestRecordDataValidatorValidateNumber(t *testing.T) {
|
||||
nil,
|
||||
[]string{"field3"},
|
||||
},
|
||||
{
|
||||
"(number) check NoDecimals",
|
||||
map[string]any{
|
||||
"field2": 1,
|
||||
"field4": 456.789,
|
||||
},
|
||||
nil,
|
||||
[]string{"field4"},
|
||||
},
|
||||
{
|
||||
"(number) valid data (only required)",
|
||||
map[string]any{
|
||||
@@ -258,6 +276,7 @@ func TestRecordDataValidatorValidateNumber(t *testing.T) {
|
||||
"field1": nil,
|
||||
"field2": 123, // test value cast
|
||||
"field3": max,
|
||||
"field4": 456,
|
||||
},
|
||||
nil,
|
||||
[]string{},
|
||||
|
||||
Reference in New Issue
Block a user