2022-05-22 19:47:27 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 CAcert Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-05-22 19:15:54 +00:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
2022-06-03 18:57:20 +00:00
|
|
|
"net"
|
|
|
|
"net/mail"
|
2022-05-27 18:45:04 +00:00
|
|
|
"reflect"
|
2022-05-22 19:15:54 +00:00
|
|
|
"strings"
|
|
|
|
"unicode/utf8"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Validator struct {
|
|
|
|
FieldErrors map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Validator) Valid() bool {
|
|
|
|
return len(v.FieldErrors) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Validator) AddFieldError(key, message string) {
|
|
|
|
if v.FieldErrors == nil {
|
|
|
|
v.FieldErrors = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, exists := v.FieldErrors[key]; !exists {
|
|
|
|
v.FieldErrors[key] = message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Validator) CheckField(ok bool, key, message string) {
|
|
|
|
if !ok {
|
|
|
|
v.AddFieldError(key, message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NotBlank(value string) bool {
|
|
|
|
return strings.TrimSpace(value) != ""
|
|
|
|
}
|
|
|
|
|
2022-05-27 18:45:04 +00:00
|
|
|
func NotNil(value any) bool {
|
|
|
|
val := reflect.ValueOf(value)
|
2022-05-29 13:43:45 +00:00
|
|
|
|
2022-05-27 18:45:04 +00:00
|
|
|
return !val.IsNil()
|
|
|
|
}
|
|
|
|
|
2022-05-22 19:15:54 +00:00
|
|
|
func MaxChars(value string, n int) bool {
|
2022-05-27 18:45:04 +00:00
|
|
|
return utf8.RuneCountInString(strings.TrimSpace(value)) <= n
|
2022-05-22 19:15:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func MinChars(value string, n int) bool {
|
2022-05-27 18:45:04 +00:00
|
|
|
return utf8.RuneCountInString(strings.TrimSpace(value)) >= n
|
2022-05-22 19:15:54 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 13:27:25 +00:00
|
|
|
func PermittedInt(value int, permittedValues ...int) bool {
|
2022-05-22 19:15:54 +00:00
|
|
|
for i := range permittedValues {
|
|
|
|
if value == permittedValues[i] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2022-06-02 21:14:38 +00:00
|
|
|
|
|
|
|
func PermittedString(value string, permittedValues ...string) bool {
|
|
|
|
for i := range permittedValues {
|
|
|
|
if value == permittedValues[i] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func PermittedStringSet(value []string, permittedValues []string) bool {
|
2022-09-26 09:58:36 +00:00
|
|
|
if len(value) == 0 {
|
2022-06-02 21:14:38 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
valueMap := make(map[string]struct{}, len(permittedValues))
|
|
|
|
for _, v := range permittedValues {
|
|
|
|
valueMap[v] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for j := range value {
|
|
|
|
if _, ok := valueMap[value[j]]; !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2022-06-03 18:57:20 +00:00
|
|
|
|
|
|
|
func IsEmail(value string) bool {
|
|
|
|
addr, err := mail.ParseAddress(value)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.SplitN(addr.Address, "@", 2)
|
2022-09-26 09:58:36 +00:00
|
|
|
|
2022-06-03 18:57:20 +00:00
|
|
|
mxs, err := net.LookupMX(parts[1])
|
|
|
|
if err != nil || len(mxs) < 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|