Skip to content

Commit 1c86e53

Browse files
committed
feat: add boolean validation
1 parent 90631a5 commit 1c86e53

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed

examples/boolean/validate_false.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/mkafonso/schema/pkg/validator"
7+
)
8+
9+
func ValidateFalseExample() {
10+
boolValidator := validator.NewBooleanValidator().
11+
IsFalse("Value must be false")
12+
13+
result := boolValidator.Validate(false)
14+
fmt.Println("Example - Validating false:")
15+
fmt.Printf("Result: %v\n", result.IsValid)
16+
if !result.IsValid {
17+
fmt.Printf("Errors: %v\n", result.Errors)
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/mkafonso/schema/pkg/validator"
7+
)
8+
9+
func ValidateInvalidTypeExample() {
10+
boolValidator := validator.NewBooleanValidator()
11+
12+
result := boolValidator.ValidateInterface("not a boolean")
13+
fmt.Println("Example - Validating invalid type:")
14+
fmt.Printf("Result: %v\n", result.IsValid)
15+
if !result.IsValid {
16+
fmt.Printf("Errors: %v\n", result.Errors)
17+
}
18+
}

examples/boolean/validate_true.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/mkafonso/schema/pkg/validator"
7+
)
8+
9+
func ValidateTrueExample() {
10+
boolValidator := validator.NewBooleanValidator().
11+
IsTrue("Value must be true")
12+
13+
result := boolValidator.Validate(true)
14+
fmt.Println("Example - Validating true:")
15+
fmt.Printf("Result: %v\n", result.IsValid)
16+
if !result.IsValid {
17+
fmt.Printf("Errors: %v\n", result.Errors)
18+
}
19+
}

internal/validator/boolean.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package validator
2+
3+
// BooleanValidator is a validator for boolean values.
4+
type BooleanValidator struct {
5+
validations []func(bool) *ValidationResult
6+
}
7+
8+
// NewBooleanValidator creates a new boolean validator.
9+
//
10+
// Returns:
11+
// - A pointer to the newly created BooleanValidator instance.
12+
func NewBooleanValidator() *BooleanValidator {
13+
return &BooleanValidator{}
14+
}
15+
16+
// addValidation adds a custom validation function.
17+
func (v *BooleanValidator) addValidation(validation func(bool) *ValidationResult) {
18+
v.validations = append(v.validations, validation)
19+
}
20+
21+
// IsTrue adds a validation to check if the boolean is true.
22+
//
23+
// Parameters:
24+
// - errorMessage: The error message to be returned if validation fails.
25+
//
26+
// Returns:
27+
// - A pointer to the BooleanValidator instance.
28+
func (v *BooleanValidator) IsTrue(errorMessage string) *BooleanValidator {
29+
v.addValidation(func(value bool) *ValidationResult {
30+
if !value {
31+
return &ValidationResult{IsValid: false, Errors: []string{errorMessage}}
32+
}
33+
return &ValidationResult{IsValid: true}
34+
})
35+
return v
36+
}
37+
38+
// IsFalse adds a validation to check if the boolean is false.
39+
//
40+
// Parameters:
41+
// - errorMessage: The error message to be returned if validation fails.
42+
//
43+
// Returns:
44+
// - A pointer to the BooleanValidator instance.
45+
func (v *BooleanValidator) IsFalse(errorMessage string) *BooleanValidator {
46+
v.addValidation(func(value bool) *ValidationResult {
47+
if value {
48+
return &ValidationResult{IsValid: false, Errors: []string{errorMessage}}
49+
}
50+
return &ValidationResult{IsValid: true}
51+
})
52+
return v
53+
}
54+
55+
// Validate executes all validations.
56+
//
57+
// Parameters:
58+
// - value: The boolean value to be validated.
59+
//
60+
// Returns:
61+
// - The validation result.
62+
func (v *BooleanValidator) Validate(value bool) *ValidationResult {
63+
result := &ValidationResult{IsValid: true}
64+
for _, validation := range v.validations {
65+
validationResult := validation(value)
66+
if !validationResult.IsValid {
67+
result.IsValid = false
68+
result.Errors = append(result.Errors, validationResult.Errors...)
69+
}
70+
}
71+
return result
72+
}
73+
74+
// ValidateInterface validates the input value, implementing ValidatorInterface.
75+
//
76+
// Parameters:
77+
// - value: The value to be validated.
78+
//
79+
// Returns:
80+
// - The validation result.
81+
func (v *BooleanValidator) ValidateInterface(value interface{}) *ValidationResult {
82+
if boolVal, ok := value.(bool); ok {
83+
return v.Validate(boolVal)
84+
}
85+
return &ValidationResult{IsValid: false, Errors: []string{"Invalid type, expected boolean"}}
86+
}

pkg/validator/validator.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,13 @@ func NewStructValidator() *validator.StructValidator {
2828
return validator.NewStructValidator()
2929
}
3030

31+
// NewBooleanValidator creates a new instance of the BooleanValidator.
32+
//
33+
// Returns:
34+
// - A pointer to the newly created BooleanValidator instance.
35+
func NewBooleanValidator() *validator.BooleanValidator {
36+
return validator.NewBooleanValidator()
37+
}
38+
3139
// ValidationResult represents the result of a validation.
3240
type ValidationResult = validator.ValidationResult

0 commit comments

Comments
 (0)