-
Notifications
You must be signed in to change notification settings - Fork 11
/
validate.go
197 lines (160 loc) · 4.61 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"reflect"
"regexp"
"unicode"
"github.com/go-playground/validator/v10"
)
type Project struct {
Category string `json:"category" validate:"required,oneof=article repo video"`
Id string `json:"id" validate:"required,unique_id,alphanumeric_dashes"`
Title string `json:"title" validate:"required,plaintext"`
Author string `json:"author" validate:"required,plaintext"`
Url string `json:"url" validate:"required,url,stable_url"`
Description string `json:"description" validate:"omitempty,plaintext"`
CreatedAt string `json:"createdAt" validate:"required,datetime=2006-01-02"`
Tags []string `json:"tags" validate:"required,dive,plaintext"`
}
func main() {
var validatedProjects []Project
currentBranch := getEnvVar("CURRENT_BRANCH")
fmt.Println("Current branch:", currentBranch)
branchData, err := ioutil.ReadFile("projects.json")
if err != nil {
fmt.Println("Error reading first file:", err)
os.Exit(1)
}
branchProjects := projectsFromJson(branchData)
if currentBranch == "main" {
validatedProjects = branchProjects
} else {
mainData := getEnvVar("MAIN_PROJECTS_DATA")
mainProjects := projectsFromJson([]byte(mainData))
for i, projectOnCurrentBranch := range branchProjects {
projectUpdated := i < len(mainProjects) && !reflect.DeepEqual(projectOnCurrentBranch, mainProjects[i])
projectNewlyAdded := i >= len(mainProjects)
if projectUpdated || projectNewlyAdded {
validatedProjects = append(validatedProjects, projectOnCurrentBranch)
}
}
}
client := http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
validationFailed := false
validate := validator.New()
validate.RegisterValidation("unique_id", UniqueId)
validate.RegisterValidation("alphanumeric_dashes", AlphaNumDashes)
validate.RegisterValidation("plaintext", func(fl validator.FieldLevel) bool {
return PrintOnly(fl) && NoEmojis(fl) && NoUrls(fl) && NoHtmlChars(fl)
})
validate.RegisterValidation("stable_url", func(fl validator.FieldLevel) bool {
return StableUrl(fl, client)
})
validatingJSON, _ := json.MarshalIndent(validatedProjects, "", " ")
fmt.Println("Validating projects:\n", string(validatingJSON))
for i, p := range validatedProjects {
err = validate.Struct(p)
if err != nil {
fmt.Printf("Error validating project %d: %s\n", i+1, err)
validationFailed = true
}
}
if validationFailed {
os.Exit(1)
} else {
os.Exit(0)
}
}
func projectsFromJson(data []byte) []Project {
var projects []Project
err := json.Unmarshal([]byte(data), &projects)
if err != nil {
fmt.Println("Error unmarshaling data as JSON:", err)
os.Exit(1)
}
return projects
}
func getEnvVar(name string) string {
value, ok := os.LookupEnv(name)
if !ok || len(value) == 0 {
fmt.Printf("%s not set\n", name)
os.Exit(1)
}
return value
}
var ids = map[string]bool{}
func UniqueId(fl validator.FieldLevel) bool {
value := fl.Field().String()
if ids[value] {
return false
}
ids[value] = true
return true
}
func PrintOnly(fl validator.FieldLevel) bool {
for _, r := range fl.Field().String() {
if !unicode.IsPrint(r) {
return false
}
}
return true
}
func AlphaNumDashes(fl validator.FieldLevel) bool {
value := fl.Field().String()
match, _ := regexp.MatchString("^[a-zA-Z0-9-]+$", value)
return match
}
func NoEmojis(fl validator.FieldLevel) bool {
for _, r := range fl.Field().String() {
if r >= 0x1F600 && r <= 0x1F64F {
return false
}
}
return true
}
func NoUrls(fl validator.FieldLevel) bool {
value := fl.Field().String()
re := regexp.MustCompile(`https?://\S+`)
return !re.MatchString(value)
}
func NoHtmlChars(fl validator.FieldLevel) bool {
value := fl.Field().String()
// Keeping this pretty loose as it's not uncommon
// for titles to have ampersands and quotes in them,
// and the client is going to encode it anyhow
re := regexp.MustCompile(`[<|>]`)
return !re.MatchString(value)
}
func StableUrl(fl validator.FieldLevel, client http.Client) bool {
value := fl.Field().String()
res, err := client.Get(value)
if err != nil {
return false
}
defer res.Body.Close()
// Medium articles with a custom domain do a redirect through
// medium.com, so this is a special case to allow the 307
if res.StatusCode == 307 {
url, err := url.Parse(res.Header.Get("Location"))
if err != nil {
return false
}
if url.Host == "medium.com" {
return true
}
}
pass := res.StatusCode == 200
if !pass {
fmt.Printf("Received status code %d from %s\n", res.StatusCode, value)
}
return pass
}