github.com/zak-blake/goa@v1.4.1/validation_test.go (about) 1 package goa_test 2 3 import ( 4 "github.com/goadesign/goa" 5 . "github.com/onsi/ginkgo" 6 . "github.com/onsi/gomega" 7 ) 8 9 var _ = Describe("ValidateFormat", func() { 10 var f goa.Format 11 var val string 12 var valErr error 13 14 BeforeEach(func() { 15 val = "" 16 }) 17 18 JustBeforeEach(func() { 19 valErr = goa.ValidateFormat(f, val) 20 }) 21 22 Context("Date", func() { 23 BeforeEach(func() { 24 f = goa.FormatDate 25 }) 26 27 Context("with an invalid value", func() { 28 BeforeEach(func() { 29 val = "201510-26" 30 }) 31 32 It("does not validates", func() { 33 Ω(valErr).Should(HaveOccurred()) 34 }) 35 }) 36 37 Context("with a valid value", func() { 38 BeforeEach(func() { 39 val = "2015-10-26" 40 }) 41 42 It("validates", func() { 43 Ω(valErr).ShouldNot(HaveOccurred()) 44 }) 45 }) 46 }) 47 48 Context("DateTime", func() { 49 BeforeEach(func() { 50 f = goa.FormatDateTime 51 }) 52 53 Context("with an invalid value", func() { 54 BeforeEach(func() { 55 val = "201510-26T08:31:23Z" 56 }) 57 58 It("does not validates", func() { 59 Ω(valErr).Should(HaveOccurred()) 60 }) 61 }) 62 63 Context("with a valid value", func() { 64 BeforeEach(func() { 65 val = "2015-10-26T08:31:23Z" 66 }) 67 68 It("validates", func() { 69 Ω(valErr).ShouldNot(HaveOccurred()) 70 }) 71 }) 72 }) 73 74 Context("UUID", func() { 75 BeforeEach(func() { 76 f = goa.FormatUUID 77 }) 78 79 Context("with an invalid value", func() { 80 BeforeEach(func() { 81 val = "96054a62-a9e45ed26688389b" 82 }) 83 84 It("does not validate", func() { 85 Ω(valErr).Should(HaveOccurred()) 86 }) 87 }) 88 89 Context("with an valid value", func() { 90 BeforeEach(func() { 91 val = "6ba7b810-9dad-11d1-80b4-00c04fd430c8" 92 }) 93 94 It("validates", func() { 95 Ω(valErr).ShouldNot(HaveOccurred()) 96 }) 97 }) 98 }) 99 100 Context("Email", func() { 101 BeforeEach(func() { 102 f = goa.FormatEmail 103 }) 104 105 Context("with an invalid value", func() { 106 BeforeEach(func() { 107 val = "foo" 108 }) 109 110 It("does not validates", func() { 111 Ω(valErr).Should(HaveOccurred()) 112 }) 113 }) 114 115 Context("with a valid value", func() { 116 BeforeEach(func() { 117 val = "raphael@goa.design" 118 }) 119 120 It("validates", func() { 121 Ω(valErr).ShouldNot(HaveOccurred()) 122 }) 123 }) 124 125 }) 126 127 Context("Hostname", func() { 128 BeforeEach(func() { 129 f = goa.FormatHostname 130 }) 131 132 Context("with an invalid value", func() { 133 BeforeEach(func() { 134 val = "_hi_" 135 }) 136 137 It("does not validates", func() { 138 Ω(valErr).Should(HaveOccurred()) 139 }) 140 }) 141 142 Context("with a valid value", func() { 143 BeforeEach(func() { 144 val = "goa.design" 145 }) 146 147 It("validates", func() { 148 Ω(valErr).ShouldNot(HaveOccurred()) 149 }) 150 }) 151 152 }) 153 154 Context("IPv4", func() { 155 BeforeEach(func() { 156 f = goa.FormatIPv4 157 }) 158 159 Context("with an invalid value", func() { 160 BeforeEach(func() { 161 val = "192-168.0.1" 162 }) 163 164 It("does not validate", func() { 165 Ω(valErr).Should(HaveOccurred()) 166 }) 167 }) 168 169 Context("with a valid IPv6 value", func() { 170 BeforeEach(func() { 171 val = "::1" 172 }) 173 174 It("does not validate", func() { 175 Ω(valErr).Should(HaveOccurred()) 176 }) 177 }) 178 179 Context("with a valid value", func() { 180 BeforeEach(func() { 181 val = "192.168.0.1" 182 }) 183 184 It("validates", func() { 185 Ω(valErr).ShouldNot(HaveOccurred()) 186 }) 187 }) 188 189 }) 190 191 Context("IPv6", func() { 192 BeforeEach(func() { 193 f = goa.FormatIPv6 194 }) 195 196 Context("with an invalid value", func() { 197 BeforeEach(func() { 198 val = "foo" 199 }) 200 201 It("does not validate", func() { 202 Ω(valErr).Should(HaveOccurred()) 203 }) 204 }) 205 206 Context("with a valid IPv4 value", func() { 207 BeforeEach(func() { 208 val = "10.10.10.10" 209 }) 210 211 It("does not validate", func() { 212 Ω(valErr).Should(HaveOccurred()) 213 }) 214 }) 215 216 Context("with a valid value", func() { 217 BeforeEach(func() { 218 val = "0:0:0:0:0:0:0:1" 219 }) 220 221 It("validates", func() { 222 Ω(valErr).ShouldNot(HaveOccurred()) 223 }) 224 }) 225 226 }) 227 228 Context("IP", func() { 229 BeforeEach(func() { 230 f = goa.FormatIP 231 }) 232 233 Context("with an invalid value", func() { 234 BeforeEach(func() { 235 val = "::1.1" 236 }) 237 238 It("does not validate", func() { 239 Ω(valErr).Should(HaveOccurred()) 240 }) 241 }) 242 243 Context("with a valid IPv4 value", func() { 244 BeforeEach(func() { 245 val = "127.0.0.1" 246 }) 247 248 It("validates", func() { 249 Ω(valErr).ShouldNot(HaveOccurred()) 250 }) 251 }) 252 253 Context("with a valid IPv6 value", func() { 254 BeforeEach(func() { 255 val = "::1" 256 }) 257 258 It("validates", func() { 259 Ω(valErr).ShouldNot(HaveOccurred()) 260 }) 261 }) 262 }) 263 264 Context("URI", func() { 265 BeforeEach(func() { 266 f = goa.FormatURI 267 }) 268 269 Context("with an invalid value", func() { 270 BeforeEach(func() { 271 val = "foo_" 272 }) 273 274 It("does not validate", func() { 275 Ω(valErr).Should(HaveOccurred()) 276 }) 277 }) 278 279 Context("with a valid value", func() { 280 BeforeEach(func() { 281 val = "hhp://goa.design/contact" 282 }) 283 284 It("validates", func() { 285 Ω(valErr).ShouldNot(HaveOccurred()) 286 }) 287 }) 288 289 }) 290 291 Context("MAC", func() { 292 BeforeEach(func() { 293 f = goa.FormatMAC 294 }) 295 296 Context("with an invalid value", func() { 297 BeforeEach(func() { 298 val = "bar" 299 }) 300 301 It("does not validate", func() { 302 Ω(valErr).Should(HaveOccurred()) 303 }) 304 }) 305 306 Context("with a valid value", func() { 307 BeforeEach(func() { 308 val = "06-00-00-00-00-00" 309 }) 310 311 It("validates", func() { 312 Ω(valErr).ShouldNot(HaveOccurred()) 313 }) 314 }) 315 316 }) 317 318 Context("CIDR", func() { 319 BeforeEach(func() { 320 f = goa.FormatCIDR 321 }) 322 323 Context("with an invalid value", func() { 324 BeforeEach(func() { 325 val = "foo" 326 }) 327 328 It("does not validate", func() { 329 Ω(valErr).Should(HaveOccurred()) 330 }) 331 }) 332 333 Context("with a valid value", func() { 334 BeforeEach(func() { 335 val = "10.0.0.0/8" 336 }) 337 338 It("validates", func() { 339 Ω(valErr).ShouldNot(HaveOccurred()) 340 }) 341 }) 342 343 }) 344 345 Context("Regexp", func() { 346 BeforeEach(func() { 347 f = goa.FormatRegexp 348 }) 349 350 Context("with an invalid value", func() { 351 BeforeEach(func() { 352 val = "foo[" 353 }) 354 355 It("does not validate", func() { 356 Ω(valErr).Should(HaveOccurred()) 357 }) 358 }) 359 360 Context("with a valid value", func() { 361 BeforeEach(func() { 362 val = "^goa$" 363 }) 364 365 It("validates", func() { 366 Ω(valErr).ShouldNot(HaveOccurred()) 367 }) 368 }) 369 370 }) 371 372 Context("RFC1123", func() { 373 BeforeEach(func() { 374 f = goa.FormatRFC1123 375 }) 376 377 Context("with an invalid value", func() { 378 BeforeEach(func() { 379 val = "Mon 04 Jun 2017 23:52:05 MST" 380 381 }) 382 383 It("does not validates", func() { 384 Ω(valErr).Should(HaveOccurred()) 385 }) 386 }) 387 388 Context("with a valid value", func() { 389 BeforeEach(func() { 390 val = "Mon, 04 Jun 2017 23:52:05 MST" 391 }) 392 393 It("validates", func() { 394 Ω(valErr).ShouldNot(HaveOccurred()) 395 }) 396 }) 397 }) 398 })