From b823d5746aa1656922584943226acbbf5c12e9af Mon Sep 17 00:00:00 2001 From: masoud Date: Mon, 4 Mar 2024 15:10:12 +0330 Subject: [PATCH 1/2] added: Required float64 validation rule --- required.go | 6 ++++++ required_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/required.go b/required.go index 3f23e9f..202df1c 100644 --- a/required.go +++ b/required.go @@ -22,3 +22,9 @@ func (v *Validator) RequiredInt(i int, field string, msg string) *Validator { return v } + +func (v *Validator) RequiredFloat(f float64, field string, msg string) *Validator { + v.Check(f != 0.0, field, v.msg(Required, msg, field)) + + return v +} diff --git a/required_test.go b/required_test.go index 3b2e96e..bd990dc 100644 --- a/required_test.go +++ b/required_test.go @@ -1,6 +1,7 @@ package validator import ( + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -48,3 +49,47 @@ func TestValidator_RequiredString(t *testing.T) { } } } + +func TestValidator_RequiredFloat(t *testing.T) { + tests := []struct { + tag string + value float64 + message string + isPassed bool + expectedMsg string + }{ + { + tag: "t0", + value: 1.0, + message: "", + isPassed: true, + expectedMsg: "f2 is required", + }, + { + tag: "t1", + value: -1.0, + message: "f2 is required", + isPassed: true, + expectedMsg: fmt.Sprintf(RequiredMsg, "t1"), + }, + { + tag: "t2", + value: 0.0, + message: "f2 is required", + isPassed: false, + expectedMsg: "f2 is required", + }, + } + + v := New() + + for _, test := range tests { + v.RequiredFloat(test.value, test.tag, test.message) + + assert.Equal(t, test.isPassed, v.IsPassed()) + + if !test.isPassed { + assert.Equal(t, test.expectedMsg, v.Errors()[test.tag]) + } + } +} From 8af3abdcc864d515d3c4ce2603578302b4396c82 Mon Sep 17 00:00:00 2001 From: masoud Date: Tue, 5 Mar 2024 09:35:01 +0330 Subject: [PATCH 2/2] resolved: conflic --- required.go | 10 +++++- required_test.go | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/required.go b/required.go index 202df1c..a7448b3 100644 --- a/required.go +++ b/required.go @@ -18,11 +18,19 @@ func (v *Validator) RequiredString(s, field string, msg string) *Validator { // RequiredInt checks if an integer value is provided or not. func (v *Validator) RequiredInt(i int, field string, msg string) *Validator { - v.Check(i == 0, field, v.msg(Required, msg, field)) + v.Check(i != 0, field, v.msg(Required, msg, field)) return v } +// RequiredSlice checks if a slice has any value or not. +func (v *Validator) RequiredSlice(s []any, field string, msg string) *Validator { + v.Check(len(s) > 0, field, v.msg(Required, msg, field)) + + return v +} + +// RequiredFloat checks if float value is provided or not. func (v *Validator) RequiredFloat(f float64, field string, msg string) *Validator { v.Check(f != 0.0, field, v.msg(Required, msg, field)) diff --git a/required_test.go b/required_test.go index bd990dc..59abb6a 100644 --- a/required_test.go +++ b/required_test.go @@ -7,6 +7,43 @@ import ( "github.com/stretchr/testify/assert" ) +func TestValidator_RequiredInt(t *testing.T) { + tests := []struct { + tag string + value int + message string + isPassed bool + expectedMsg string + }{ + { + tag: "t0", + value: 1, + message: "", + isPassed: true, + expectedMsg: "", + }, + { + tag: "t1", + value: 0, + message: "t1 is required", + isPassed: false, + expectedMsg: "t1 is required", + }, + } + + v := New() + + for _, test := range tests { + v.RequiredInt(test.value, test.tag, test.message) + + assert.Equal(t, test.isPassed, v.IsPassed()) + + if !test.isPassed { + assert.Equal(t, test.expectedMsg, v.Errors()[test.tag]) + } + } +} + func TestValidator_RequiredString(t *testing.T) { tests := []struct { tag string @@ -93,3 +130,53 @@ func TestValidator_RequiredFloat(t *testing.T) { } } } + +func TestValidator_RequiredSlice(t *testing.T) { + tests := []struct { + tag string + value []any + message string + isPassed bool + expectedMsg string + }{ + { + tag: "t0", + value: []any{1, 2, 3}, + message: "", + isPassed: true, + expectedMsg: "", + }, + { + tag: "t1", + value: []any{1.1, 5.2}, + message: "", + isPassed: true, + expectedMsg: "", + }, + { + tag: "t2", + value: []any{}, + message: "t2 is required", + isPassed: false, + expectedMsg: "t2 is required", + }, + { + tag: "t3", + value: []any{"Taylor, Smith", "Davies, O'Brien", "Wilson, Byrne"}, + message: "", + isPassed: true, + expectedMsg: "", + }, + } + + v := New() + for _, test := range tests { + v.RequiredSlice(test.value, test.tag, test.message) + + assert.Equal(t, test.isPassed, v.IsPassed()) + + if !test.isPassed { + assert.Equal(t, test.expectedMsg, v.Errors()[test.tag]) + } + } +}