forked from stripe/stripe-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.go
135 lines (105 loc) · 3.59 KB
/
validate.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package validators
import (
"errors"
"fmt"
"net/http"
"strconv"
"strings"
)
// ArgValidator is an argument validator. It accepts a string and returns an
// error if the string is invalid, or nil otherwise.
type ArgValidator func(string) error
// CallNonEmptyArray calls an argument validator on all non-empty elements of
// a string array.
func CallNonEmptyArray(validator ArgValidator, values []string) error {
if len(values) == 0 {
return nil
}
for _, value := range values {
err := CallNonEmpty(validator, value)
if err != nil {
return err
}
}
return nil
}
// CallNonEmpty calls an argument validator on a string if the string is not
// empty.
func CallNonEmpty(validator ArgValidator, value string) error {
if value == "" {
return nil
}
return validator(value)
}
// APIKey validates that a string looks like an API key.
func APIKey(input string) error {
if len(input) == 0 {
return errors.New("you have not configured API keys yet. To do so, run `stripe login` which will configure your API keys from Stripe")
} else if len(input) < 12 {
return errors.New("the API key provided is too short, it must be at least 12 characters long")
}
keyParts := strings.Split(input, "_")
if len(keyParts) < 3 {
return errors.New("you are using a legacy-style API key which is unsupported by the CLI. Please generate a new test mode API key")
}
if keyParts[0] != "sk" && keyParts[0] != "rk" {
return errors.New("the CLI only supports using a secret or restricted key")
}
return nil
}
// Account validates that a string is an acceptable account filter.
func Account(account string) error {
accountUpper := strings.ToUpper(account)
if accountUpper == "CONNECT_IN" || accountUpper == "CONNECT_OUT" || accountUpper == "SELF" {
return nil
}
return fmt.Errorf("%s is not an acceptable account filter (CONNECT_IN, CONNECT_OUT, SELF)", account)
}
// HTTPMethod validates that a string is an acceptable HTTP method.
func HTTPMethod(method string) error {
methodUpper := strings.ToUpper(method)
if methodUpper == http.MethodGet || methodUpper == http.MethodPost || methodUpper == http.MethodDelete {
return nil
}
return fmt.Errorf("%s is not an acceptable HTTP method (GET, POST, DELETE)", method)
}
// RequestSource validates that a string is an acceptable request source.
func RequestSource(source string) error {
sourceUpper := strings.ToUpper(source)
if sourceUpper == "API" || sourceUpper == "DASHBOARD" {
return nil
}
return fmt.Errorf("%s is not an acceptable source (API, DASHBOARD)", source)
}
// RequestStatus validates that a string is an acceptable request status.
func RequestStatus(status string) error {
statusUpper := strings.ToUpper(status)
if statusUpper == "SUCCEEDED" || statusUpper == "FAILED" {
return nil
}
return fmt.Errorf("%s is not an acceptable request status (SUCCEEDED, FAILED)", status)
}
// StatusCode validates that a provided status code is within the range of
// those used in the Stripe API.
func StatusCode(code string) error {
num, err := strconv.Atoi(code)
if err != nil {
return err
}
if num >= 200 && num < 300 {
return nil
}
if num >= 400 && num < 600 {
return nil
}
return fmt.Errorf("Provided status code %s is not in the range of acceptable status codes (200's, 400's, 500's)", code)
}
// StatusCodeType validates that a provided status code type is one of those
// used in the Stripe API.
func StatusCodeType(code string) error {
codeUpper := strings.ToUpper(code)
if codeUpper != "2XX" && codeUpper != "4XX" && codeUpper != "5XX" {
return fmt.Errorf("Provided status code type %s is not a valid type (2XX, 4XX, 5XX)", code)
}
return nil
}