BrainMinder/internal/validator/validator.go
2024-08-22 10:13:16 +02:00

41 lines
743 B
Go

package validator
type Validator struct {
Errors []string
FieldErrors map[string]string
}
func (v Validator) HasErrors() bool {
return len(v.Errors) != 0 || len(v.FieldErrors) != 0
}
func (v *Validator) AddError(message string) {
if v.Errors == nil {
v.Errors = []string{}
}
v.Errors = append(v.Errors, message)
}
func (v *Validator) AddFieldError(key, message string) {
if v.FieldErrors == nil {
v.FieldErrors = map[string]string{}
}
if _, exists := v.FieldErrors[key]; !exists {
v.FieldErrors[key] = message
}
}
func (v *Validator) Check(ok bool, message string) {
if !ok {
v.AddError(message)
}
}
func (v *Validator) CheckField(ok bool, key, message string) {
if !ok {
v.AddFieldError(key, message)
}
}