Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added more rules for int #7

Merged
merged 1 commit into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 106 additions & 1 deletion int.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ func (s *IntSchema) Lt(min int) *IntSchema {
return s
}

func (s *IntSchema) Lte(min int) *IntSchema {
validator := Validator[int]{
MessageFunc: func(value int) string {
return fmt.Sprintf("Int must be less than or equal to %d", min)
},
ValidateFunc: func(value int) bool {
return value <= min
},
}

s.validators = append(s.validators, validator)

return s
}

func (s *IntSchema) Gt(min int) *IntSchema {
validator := Validator[int]{
MessageFunc: func(value int) string {
Expand All @@ -40,4 +55,94 @@ func (s *IntSchema) Gt(min int) *IntSchema {
s.validators = append(s.validators, validator)

return s
}
}

func (s *IntSchema) Gte(min int) *IntSchema {
validator := Validator[int]{
MessageFunc: func(value int) string {
return fmt.Sprintf("Int must be greater than or equal to %d", min)
},
ValidateFunc: func(value int) bool {
return value >= min
},
}

s.validators = append(s.validators, validator)

return s
}

func (s *IntSchema) Positive() *IntSchema {
validator := Validator[int]{
MessageFunc: func(value int) string {
return "Int must be greater than 0"
},
ValidateFunc: func(value int) bool {
return value > 0
},
}

s.validators = append(s.validators, validator)

return s
}

func (s *IntSchema) Nonnegative() *IntSchema {
validator := Validator[int]{
MessageFunc: func(value int) string {
return "Int must be greater than or equal to 0"
},
ValidateFunc: func(value int) bool {
return value >= 0
},
}

s.validators = append(s.validators, validator)

return s
}

func (s *IntSchema) Negative() *IntSchema {
validator := Validator[int]{
MessageFunc: func(value int) string {
return "Int must be less than 0"
},
ValidateFunc: func(value int) bool {
return value < 0
},
}

s.validators = append(s.validators, validator)

return s
}

func (s *IntSchema) Nonpositive() *IntSchema {
validator := Validator[int]{
MessageFunc: func(value int) string {
return "Int must be less than or equal to 0"
},
ValidateFunc: func(value int) bool {
return value <= 0
},
}

s.validators = append(s.validators, validator)

return s
}

func (s *IntSchema) MultipleOf(multiple int) *IntSchema {
validator := Validator[int]{
MessageFunc: func(value int) string {
return fmt.Sprintf("Int must be a multiple of %d", multiple)
},
ValidateFunc: func(value int) bool {
return value%multiple == 0
},
}

s.validators = append(s.validators, validator)

return s
}
75 changes: 75 additions & 0 deletions int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ func TestInt_Lt(t *testing.T) {
assert.False(t, s.Parse(500).IsValid())
}

func TestInt_Lte(t *testing.T) {
s := schema.Int().Lte(5)

assert.True(t, s.Parse(4).IsValid())
assert.True(t, s.Parse(-5).IsValid())
assert.True(t, s.Parse(5).IsValid())

assert.False(t, s.Parse(6).IsValid())
assert.False(t, s.Parse(500).IsValid())
}

func TestInt_Gt(t *testing.T) {
s := schema.Int().Gt(5)

Expand All @@ -53,3 +64,67 @@ func TestInt_Gt(t *testing.T) {
assert.False(t, s.Parse(5).IsValid())
assert.False(t, s.Parse(-5).IsValid())
}

func TestInt_Gte(t *testing.T) {
s := schema.Int().Gte(5)

assert.True(t, s.Parse(5).IsValid())
assert.True(t, s.Parse(6).IsValid())
assert.True(t, s.Parse(500).IsValid())

assert.False(t, s.Parse(4).IsValid())
assert.False(t, s.Parse(-5).IsValid())
}

func TestInt_Positive(t *testing.T) {
s := schema.Int().Positive()

assert.True(t, s.Parse(1).IsValid())
assert.True(t, s.Parse(6).IsValid())
assert.True(t, s.Parse(500).IsValid())

assert.False(t, s.Parse(0).IsValid())
assert.False(t, s.Parse(-5).IsValid())
}

func TestInt_Nonnegative(t *testing.T) {
s := schema.Int().Nonnegative()

assert.True(t, s.Parse(0).IsValid())
assert.True(t, s.Parse(6).IsValid())
assert.True(t, s.Parse(500).IsValid())

assert.False(t, s.Parse(-5).IsValid())
}

func TestInt_Negative(t *testing.T) {
s := schema.Int().Negative()

assert.True(t, s.Parse(-1).IsValid())
assert.True(t, s.Parse(-500).IsValid())

assert.False(t, s.Parse(0).IsValid())
assert.False(t, s.Parse(5).IsValid())
}

func TestInt_Nonpositive(t *testing.T) {
s := schema.Int().Nonpositive()

assert.True(t, s.Parse(0).IsValid())
assert.True(t, s.Parse(-500).IsValid())

assert.False(t, s.Parse(1).IsValid())
assert.False(t, s.Parse(5).IsValid())
}

func TestInt_MultipleOf(t *testing.T) {
s := schema.Int().MultipleOf(5)

assert.True(t, s.Parse(0).IsValid())
assert.True(t, s.Parse(-5).IsValid())
assert.True(t, s.Parse(-25).IsValid())
assert.True(t, s.Parse(50).IsValid())

assert.False(t, s.Parse(1).IsValid())
assert.False(t, s.Parse(3).IsValid())
}