-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_unmarshal_validation.go
More file actions
54 lines (42 loc) · 1.91 KB
/
json_unmarshal_validation.go
File metadata and controls
54 lines (42 loc) · 1.91 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"encoding/json"
"errors"
"fmt"
)
/*To complete this task, you will need the json.Unmarshal function, which decodes JSON bytes into a structure.
Implement the DecodeAndValidateRequest(requestBody []byte) (CreateUserRequest, error) function, which decodes the JSON request body into a CreateUserRequest structure and validates it.
If invalid JSON is received or the structure is filled incorrectly, the function should return an error.*/
// CreateUserRequest is a request to create a new user.
type CreateUserRequest struct {
Email string `json:"email"`
Password string `json:"password"`
PasswordConfirmation string `json:"password_confirmation"`
}
// validation errors
var (
errEmailRequired = errors.New("email is required")
errPasswordRequired = errors.New("password is required")
errPasswordConfirmationRequired = errors.New("password confirmation is required")
errPasswordDoesNotMatch = errors.New("password does not match with the confirmation")
)
func DecodeAndValidateRequest(requestBody []byte) (CreateUserRequest, error) {
cur := CreateUserRequest{}
err := json.Unmarshal(requestBody, &cur)
if len(cur.Email) < 1 {
return CreateUserRequest{}, errEmailRequired
} else if len(cur.Password) < 1 {
return CreateUserRequest{}, errPasswordRequired
} else if len(cur.PasswordConfirmation) < 1 {
return CreateUserRequest{}, errPasswordConfirmationRequired
} else if cur.Password != cur.PasswordConfirmation {
return CreateUserRequest{}, errPasswordDoesNotMatch
}
return cur, err
}
func main() {
req, err := DecodeAndValidateRequest([]byte("{\"email\":\"\",\"password\":\"test\",\"password_confirmation\":\"test\"}"))
fmt.Println(req, err)
req, err = DecodeAndValidateRequest([]byte("{\"email\":\"l\",\"password\":\"l\",\"password_confirmation\":\"ll\"}"))
fmt.Println(req, err)
}