github.com/brycereitano/goa@v0.0.0-20170315073847-8ffa6c85e265/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("DateTime", func() { 23 BeforeEach(func() { 24 f = goa.FormatDateTime 25 }) 26 27 Context("with an invalid value", func() { 28 BeforeEach(func() { 29 val = "201510-26T08:31:23Z" 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-26T08:31:23Z" 40 }) 41 42 It("validates", func() { 43 Ω(valErr).ShouldNot(HaveOccurred()) 44 }) 45 }) 46 }) 47 48 Context("UUID", func() { 49 BeforeEach(func() { 50 f = goa.FormatUUID 51 }) 52 53 Context("with an invalid value", func() { 54 BeforeEach(func() { 55 val = "96054a62-a9e45ed26688389b" 56 }) 57 58 It("does not validate", func() { 59 Ω(valErr).Should(HaveOccurred()) 60 }) 61 }) 62 63 Context("with an valid value", func() { 64 BeforeEach(func() { 65 val = "6ba7b810-9dad-11d1-80b4-00c04fd430c8" 66 }) 67 68 It("validates", func() { 69 Ω(valErr).ShouldNot(HaveOccurred()) 70 }) 71 }) 72 }) 73 74 Context("Email", func() { 75 BeforeEach(func() { 76 f = goa.FormatEmail 77 }) 78 79 Context("with an invalid value", func() { 80 BeforeEach(func() { 81 val = "foo" 82 }) 83 84 It("does not validates", func() { 85 Ω(valErr).Should(HaveOccurred()) 86 }) 87 }) 88 89 Context("with a valid value", func() { 90 BeforeEach(func() { 91 val = "raphael@goa.design" 92 }) 93 94 It("validates", func() { 95 Ω(valErr).ShouldNot(HaveOccurred()) 96 }) 97 }) 98 99 }) 100 101 Context("Hostname", func() { 102 BeforeEach(func() { 103 f = goa.FormatHostname 104 }) 105 106 Context("with an invalid value", func() { 107 BeforeEach(func() { 108 val = "_hi_" 109 }) 110 111 It("does not validates", func() { 112 Ω(valErr).Should(HaveOccurred()) 113 }) 114 }) 115 116 Context("with a valid value", func() { 117 BeforeEach(func() { 118 val = "goa.design" 119 }) 120 121 It("validates", func() { 122 Ω(valErr).ShouldNot(HaveOccurred()) 123 }) 124 }) 125 126 }) 127 128 Context("IPv4", func() { 129 BeforeEach(func() { 130 f = goa.FormatIPv4 131 }) 132 133 Context("with an invalid value", func() { 134 BeforeEach(func() { 135 val = "192-168.0.1" 136 }) 137 138 It("does not validate", func() { 139 Ω(valErr).Should(HaveOccurred()) 140 }) 141 }) 142 143 Context("with a valid IPv6 value", func() { 144 BeforeEach(func() { 145 val = "::1" 146 }) 147 148 It("does not validate", func() { 149 Ω(valErr).Should(HaveOccurred()) 150 }) 151 }) 152 153 Context("with a valid value", func() { 154 BeforeEach(func() { 155 val = "192.168.0.1" 156 }) 157 158 It("validates", func() { 159 Ω(valErr).ShouldNot(HaveOccurred()) 160 }) 161 }) 162 163 }) 164 165 Context("IPv6", func() { 166 BeforeEach(func() { 167 f = goa.FormatIPv6 168 }) 169 170 Context("with an invalid value", func() { 171 BeforeEach(func() { 172 val = "foo" 173 }) 174 175 It("does not validate", func() { 176 Ω(valErr).Should(HaveOccurred()) 177 }) 178 }) 179 180 Context("with a valid IPv4 value", func() { 181 BeforeEach(func() { 182 val = "10.10.10.10" 183 }) 184 185 It("does not validate", func() { 186 Ω(valErr).Should(HaveOccurred()) 187 }) 188 }) 189 190 Context("with a valid value", func() { 191 BeforeEach(func() { 192 val = "0:0:0:0:0:0:0:1" 193 }) 194 195 It("validates", func() { 196 Ω(valErr).ShouldNot(HaveOccurred()) 197 }) 198 }) 199 200 }) 201 202 Context("IP", func() { 203 BeforeEach(func() { 204 f = goa.FormatIP 205 }) 206 207 Context("with an invalid value", func() { 208 BeforeEach(func() { 209 val = "::1.1" 210 }) 211 212 It("does not validate", func() { 213 Ω(valErr).Should(HaveOccurred()) 214 }) 215 }) 216 217 Context("with a valid IPv4 value", func() { 218 BeforeEach(func() { 219 val = "127.0.0.1" 220 }) 221 222 It("validates", func() { 223 Ω(valErr).ShouldNot(HaveOccurred()) 224 }) 225 }) 226 227 Context("with a valid IPv6 value", func() { 228 BeforeEach(func() { 229 val = "::1" 230 }) 231 232 It("validates", func() { 233 Ω(valErr).ShouldNot(HaveOccurred()) 234 }) 235 }) 236 }) 237 238 Context("URI", func() { 239 BeforeEach(func() { 240 f = goa.FormatURI 241 }) 242 243 Context("with an invalid value", func() { 244 BeforeEach(func() { 245 val = "foo_" 246 }) 247 248 It("does not validate", func() { 249 Ω(valErr).Should(HaveOccurred()) 250 }) 251 }) 252 253 Context("with a valid value", func() { 254 BeforeEach(func() { 255 val = "hhp://goa.design/contact" 256 }) 257 258 It("validates", func() { 259 Ω(valErr).ShouldNot(HaveOccurred()) 260 }) 261 }) 262 263 }) 264 265 Context("MAC", func() { 266 BeforeEach(func() { 267 f = goa.FormatMAC 268 }) 269 270 Context("with an invalid value", func() { 271 BeforeEach(func() { 272 val = "bar" 273 }) 274 275 It("does not validate", func() { 276 Ω(valErr).Should(HaveOccurred()) 277 }) 278 }) 279 280 Context("with a valid value", func() { 281 BeforeEach(func() { 282 val = "06-00-00-00-00-00" 283 }) 284 285 It("validates", func() { 286 Ω(valErr).ShouldNot(HaveOccurred()) 287 }) 288 }) 289 290 }) 291 292 Context("CIDR", func() { 293 BeforeEach(func() { 294 f = goa.FormatCIDR 295 }) 296 297 Context("with an invalid value", func() { 298 BeforeEach(func() { 299 val = "foo" 300 }) 301 302 It("does not validate", func() { 303 Ω(valErr).Should(HaveOccurred()) 304 }) 305 }) 306 307 Context("with a valid value", func() { 308 BeforeEach(func() { 309 val = "10.0.0.0/8" 310 }) 311 312 It("validates", func() { 313 Ω(valErr).ShouldNot(HaveOccurred()) 314 }) 315 }) 316 317 }) 318 319 Context("Regexp", func() { 320 BeforeEach(func() { 321 f = goa.FormatRegexp 322 }) 323 324 Context("with an invalid value", func() { 325 BeforeEach(func() { 326 val = "foo[" 327 }) 328 329 It("does not validate", func() { 330 Ω(valErr).Should(HaveOccurred()) 331 }) 332 }) 333 334 Context("with a valid value", func() { 335 BeforeEach(func() { 336 val = "^goa$" 337 }) 338 339 It("validates", func() { 340 Ω(valErr).ShouldNot(HaveOccurred()) 341 }) 342 }) 343 344 }) 345 })