Skip to content

Commit 58c02e7

Browse files
authored
Merge pull request #107 from devopsdays/add-event-field-validation
Add additional event validation
2 parents 5c33b23 + a67cea6 commit 58c02e7

File tree

4 files changed

+110
-2
lines changed

4 files changed

+110
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Change Log
22

3+
## [0.16.4](https://github.com/devopsdays/devopsdays-cli/tree/0.16.4) (2017-10-06)
4+
[Full Changelog](https://github.com/devopsdays/devopsdays-cli/compare/0.16.3...0.16.4)
5+
36
## [0.16.3](https://github.com/devopsdays/devopsdays-cli/tree/0.16.3) (2017-10-06)
47
[Full Changelog](https://github.com/devopsdays/devopsdays-cli/compare/0.16.2...0.16.3)
58

event/event.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,38 @@ var qsCreateEvent = []*survey.Question{
4949
Message: "Enter your Google Analytics ID [optional]:",
5050
Help: "This will allow your page to be tracked. Example: UA-74738648-1.",
5151
},
52+
Validate: func(val interface{}) error {
53+
if str, _ := val.(string); (str != "") && (helpers.ValidateField(str, "googleanalytics") == false) {
54+
return errors.New("Please enter a valid Google Analytics ID. Example: UA-74738648-1")
55+
}
56+
return nil
57+
},
5258
},
5359
{
5460
Name: "startdate",
5561
Prompt: &survey.Input{
56-
Message: "Enter the event's start date [optional]:",
62+
Message: "Enter the event's start date in the format of YYYY-MM-YY [optional]:",
5763
Help: "You can only provide a date for your event if you have a signed contract with your venue.",
5864
},
65+
Validate: func(val interface{}) error {
66+
if str, _ := val.(string); (str != "") && (helpers.ValidateField(str, "date") == false) {
67+
return errors.New("Please enter a valid date. Example: 2018-01-30.")
68+
}
69+
return nil
70+
},
5971
},
6072
{
6173
Name: "enddate",
6274
Prompt: &survey.Input{
63-
Message: "Enter the event's end date [optional]:",
75+
Message: "Enter the event's end date in the format of YYYY-MM-YY [optional]:",
6476
Help: "For single-day events make the end date the same as the start date. You can only provide a date for your event if you have a signed contract with your venue.",
6577
},
78+
Validate: func(val interface{}) error {
79+
if str, _ := val.(string); (str != "") && (helpers.ValidateField(str, "date") == false) {
80+
return errors.New("Please enter a valid date. Example: 2018-01-30.")
81+
}
82+
return nil
83+
},
6684
},
6785
{
6886
Name: "coordinates",
@@ -151,6 +169,11 @@ func CreateEvent(city, year string) (err error) {
151169
survey.AskOne(prompt, &year, survey.Required)
152170
}
153171

172+
if CheckEvent(city, year) {
173+
fmt.Println("This event already exists. If you would like to edit it, please run `devopsdays-cli edit event`")
174+
return
175+
}
176+
154177
surveyErr := survey.Ask(qsCreateEvent, &answers)
155178
if surveyErr != nil {
156179
fmt.Println(surveyErr.Error())

helpers/validate_field.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package helpers
22

33
import (
4+
"regexp"
45
"strconv"
56
"strings"
67

@@ -60,6 +61,15 @@ func ValidateField(input, field string) bool {
6061
case "filepath":
6162
ret, _ := govalidator.IsFilePath(input)
6263
return ret
64+
case "date":
65+
ret := govalidator.IsTime(input, "2006-01-02")
66+
return ret
67+
case "googleanalytics":
68+
ret, _ := regexp.MatchString(`UA-[0-9]{8}-[0-9]`, input)
69+
return ret
70+
case "coordinates":
71+
ret, _ := regexp.MatchString(`-?[0-9]+\.[0-9]+,\W?-?[0-9]+.[0-9]+`, input)
72+
return ret
6373
}
6474
return true // TODO: Make this return an error if no field was matched
6575
}

helpers/validate_field_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,76 @@ func TestValidateField(t *testing.T) {
150150
})
151151
})
152152
})
153+
Convey("Given a validation request for a date", t, func() {
154+
Convey("When the string is 2016-01-05", func() {
155+
date := "2016-01-05"
156+
Convey("Then the response should be true", func() {
157+
So(ValidateField(date, "date"), ShouldEqual, true)
158+
})
159+
})
160+
Convey("When the string is 16-01-05", func() {
161+
date := "16-01-05"
162+
Convey("Then the response should be false", func() {
163+
So(ValidateField(date, "date"), ShouldEqual, false)
164+
})
165+
})
166+
Convey("When the date is March 1, 2016", func() {
167+
date := "March 1, 2016"
168+
Convey("Then the response should be false", func() {
169+
So(ValidateField(date, "date"), ShouldEqual, false)
170+
})
171+
})
172+
Convey("When the date is 2016-30-01", func() {
173+
date := "2016-30-01"
174+
Convey("Then the response should be false", func() {
175+
So(ValidateField(date, "date"), ShouldEqual, false)
176+
})
177+
})
178+
})
179+
Convey("Given a validation request for a GA ID", t, func() {
180+
Convey("When the ID is UA-74738648-1", func() {
181+
googleanalytics := "UA-74738648-1"
182+
Convey("Then the response should be true", func() {
183+
So(ValidateField(googleanalytics, "googleanalytics"), ShouldEqual, true)
184+
})
185+
})
186+
Convey("When the ID is 123456", func() {
187+
googleanalytics := "123456"
188+
Convey("Then the response should be false", func() {
189+
So(ValidateField(googleanalytics, "googleanalytics"), ShouldEqual, false)
190+
})
191+
})
192+
})
193+
Convey("Given a validation request for coordinates", t, func() {
194+
Convey("When the string is 41.882219, -87.640530", func() {
195+
coordinates := "41.882219, -87.640530"
196+
Convey("Then the response should be true", func() {
197+
So(ValidateField(coordinates, "coordinates"), ShouldEqual, true)
198+
})
199+
})
200+
Convey("When the string is 41.882219,-87.640530", func() {
201+
coordinates := "41.882219,-87.640530"
202+
Convey("Then the response should be true", func() {
203+
So(ValidateField(coordinates, "coordinates"), ShouldEqual, true)
204+
})
205+
})
206+
Convey("When the string is 41.882219, -87.640530", func() {
207+
coordinates := "41.882219, -87.640530"
208+
Convey("Then the response should be false", func() {
209+
So(ValidateField(coordinates, "coordinates"), ShouldEqual, false)
210+
})
211+
})
212+
Convey("When the string is 41, -87", func() {
213+
coordinates := "41, -87"
214+
Convey("Then the response should be false", func() {
215+
So(ValidateField(coordinates, "coordinates"), ShouldEqual, false)
216+
})
217+
})
218+
Convey("When the string is 41.882219 -87.640530", func() {
219+
coordinates := "41.882219 -87.640530"
220+
Convey("Then the response should be false", func() {
221+
So(ValidateField(coordinates, "coordinates"), ShouldEqual, false)
222+
})
223+
})
224+
})
153225
}

0 commit comments

Comments
 (0)