github.com/99designs/gqlgen@v0.17.45/codegen/testserver/singlefile/mutation_with_custom_scalar.go (about) 1 package singlefile 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io" 7 "regexp" 8 ) 9 10 var re = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$") 11 12 type Email string 13 14 func (value *Email) UnmarshalGQL(v interface{}) error { 15 input, ok := v.(string) 16 if !ok { 17 return fmt.Errorf("email expects a string value") 18 } 19 if !re.MatchString(input) { 20 return fmt.Errorf("invalid email format") 21 } 22 *value = Email(input) 23 return nil 24 } 25 26 func (value Email) MarshalGQL(w io.Writer) { 27 output, _ := json.Marshal(string(value)) 28 w.Write(output) 29 }