-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemail.go
41 lines (34 loc) · 966 Bytes
/
email.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright (c) 2023-2024 Onur Cinar.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// https://github.com/cinar/checker
package v2
import (
"net/mail"
"reflect"
)
const (
// nameEmail is the name of the email check.
nameEmail = "email"
)
var (
// ErrNotEmail indicates that the given value is not a valid email address.
ErrNotEmail = NewCheckError("NOT_EMAIL")
)
// IsEmail checks if the value is a valid email address.
func IsEmail(value string) (string, error) {
_, err := mail.ParseAddress(value)
if err != nil {
return value, ErrNotEmail
}
return value, nil
}
// checkEmail checks if the value is a valid email address.
func checkEmail(value reflect.Value) (reflect.Value, error) {
_, err := IsEmail(value.Interface().(string))
return value, err
}
// makeEmail makes a checker function for the email checker.
func makeEmail(_ string) CheckFunc[reflect.Value] {
return checkEmail
}