github.com/danlock/goa@v1.4.0/dslengine/validation_test.go (about)

     1  package dslengine_test
     2  
     3  import (
     4  	. "github.com/goadesign/goa/design"
     5  	. "github.com/goadesign/goa/design/apidsl"
     6  	"github.com/goadesign/goa/dslengine"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  )
    10  
    11  var _ = Describe("Validation", func() {
    12  	Context("with a type attribute", func() {
    13  		const attName = "attName"
    14  		var dsl func()
    15  
    16  		var att *AttributeDefinition
    17  
    18  		JustBeforeEach(func() {
    19  			dslengine.Reset()
    20  			Type("bar", func() {
    21  				dsl()
    22  			})
    23  			dslengine.Run()
    24  			if dslengine.Errors == nil {
    25  				Ω(Design.Types).ShouldNot(BeNil())
    26  				Ω(Design.Types).Should(HaveKey("bar"))
    27  				Ω(Design.Types["bar"]).ShouldNot(BeNil())
    28  				Ω(Design.Types["bar"].Type).Should(BeAssignableToTypeOf(Object{}))
    29  				o := Design.Types["bar"].Type.(Object)
    30  				Ω(o).Should(HaveKey(attName))
    31  				att = o[attName]
    32  			}
    33  		})
    34  
    35  		Context("with a valid enum validation", func() {
    36  			BeforeEach(func() {
    37  				dsl = func() {
    38  					Attribute(attName, String, func() {
    39  						Enum("red", "blue")
    40  					})
    41  				}
    42  			})
    43  
    44  			It("records the validation", func() {
    45  				Ω(dslengine.Errors).ShouldNot(HaveOccurred())
    46  				Ω(att.Validation).ShouldNot(BeNil())
    47  				Ω(att.Validation.Values).Should(Equal([]interface{}{"red", "blue"}))
    48  			})
    49  		})
    50  
    51  		Context("with an incompatible enum validation type", func() {
    52  			BeforeEach(func() {
    53  				dsl = func() {
    54  					Attribute(attName, Integer, func() {
    55  						Enum(1, "blue")
    56  					})
    57  				}
    58  			})
    59  
    60  			It("produces an error", func() {
    61  				Ω(dslengine.Errors).Should(HaveOccurred())
    62  			})
    63  		})
    64  
    65  		Context("with a valid format validation", func() {
    66  			BeforeEach(func() {
    67  				dsl = func() {
    68  					Attribute(attName, String, func() {
    69  						Format("email")
    70  					})
    71  				}
    72  			})
    73  
    74  			It("records the validation", func() {
    75  				Ω(dslengine.Errors).ShouldNot(HaveOccurred())
    76  				Ω(att.Validation).ShouldNot(BeNil())
    77  				Ω(att.Validation.Format).Should(Equal("email"))
    78  			})
    79  		})
    80  
    81  		Context("with an invalid format validation", func() {
    82  			BeforeEach(func() {
    83  				dsl = func() {
    84  					Attribute(attName, String, func() {
    85  						Format("emailz")
    86  					})
    87  				}
    88  			})
    89  
    90  			It("produces an error", func() {
    91  				Ω(dslengine.Errors).Should(HaveOccurred())
    92  			})
    93  		})
    94  
    95  		Context("with a valid pattern validation", func() {
    96  			BeforeEach(func() {
    97  				dsl = func() {
    98  					Attribute(attName, String, func() {
    99  						Pattern("^foo$")
   100  					})
   101  				}
   102  			})
   103  
   104  			It("records the validation", func() {
   105  				Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   106  				Ω(att.Validation).ShouldNot(BeNil())
   107  				Ω(att.Validation.Pattern).Should(Equal("^foo$"))
   108  			})
   109  		})
   110  
   111  		Context("with an invalid pattern validation", func() {
   112  			BeforeEach(func() {
   113  				dsl = func() {
   114  					Attribute(attName, String, func() {
   115  						Pattern("[invalid")
   116  					})
   117  				}
   118  			})
   119  
   120  			It("produces an error", func() {
   121  				Ω(dslengine.Errors).Should(HaveOccurred())
   122  			})
   123  		})
   124  
   125  		Context("with an invalid format validation type", func() {
   126  			BeforeEach(func() {
   127  				dsl = func() {
   128  					Attribute(attName, Integer, func() {
   129  						Format("email")
   130  					})
   131  				}
   132  			})
   133  
   134  			It("produces an error", func() {
   135  				Ω(dslengine.Errors).Should(HaveOccurred())
   136  			})
   137  		})
   138  
   139  		Context("with a valid min value validation", func() {
   140  			BeforeEach(func() {
   141  				dsl = func() {
   142  					Attribute(attName, Integer, func() {
   143  						Minimum(2)
   144  					})
   145  				}
   146  			})
   147  
   148  			It("records the validation", func() {
   149  				Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   150  				Ω(att.Validation).ShouldNot(BeNil())
   151  				Ω(att.Validation.Minimum).ShouldNot(BeNil())
   152  				Ω(*att.Validation.Minimum).Should(Equal(float64(2)))
   153  			})
   154  		})
   155  
   156  		Context("with an invalid min value validation", func() {
   157  			BeforeEach(func() {
   158  				dsl = func() {
   159  					Attribute(attName, String, func() {
   160  						Minimum(2)
   161  					})
   162  				}
   163  			})
   164  
   165  			It("produces an error", func() {
   166  				Ω(dslengine.Errors).Should(HaveOccurred())
   167  			})
   168  		})
   169  
   170  		Context("with a valid max value validation", func() {
   171  			BeforeEach(func() {
   172  				dsl = func() {
   173  					Attribute(attName, Integer, func() {
   174  						Maximum(2)
   175  					})
   176  				}
   177  			})
   178  
   179  			It("records the validation", func() {
   180  				Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   181  				Ω(att.Validation).ShouldNot(BeNil())
   182  				Ω(att.Validation.Maximum).ShouldNot(BeNil())
   183  				Ω(*att.Validation.Maximum).Should(Equal(float64(2)))
   184  			})
   185  		})
   186  
   187  		Context("with an invalid max value validation", func() {
   188  			BeforeEach(func() {
   189  				dsl = func() {
   190  					Attribute(attName, String, func() {
   191  						Maximum(2)
   192  					})
   193  				}
   194  			})
   195  
   196  			It("produces an error", func() {
   197  				Ω(dslengine.Errors).Should(HaveOccurred())
   198  			})
   199  		})
   200  
   201  		Context("with a valid min length validation", func() {
   202  			BeforeEach(func() {
   203  				dsl = func() {
   204  					Attribute(attName, ArrayOf(Integer), func() {
   205  						MinLength(2)
   206  					})
   207  				}
   208  			})
   209  
   210  			It("records the validation", func() {
   211  				Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   212  				Ω(att.Validation).ShouldNot(BeNil())
   213  				Ω(att.Validation.MinLength).ShouldNot(BeNil())
   214  				Ω(*att.Validation.MinLength).Should(Equal(2))
   215  			})
   216  		})
   217  
   218  		Context("with an invalid min length validation", func() {
   219  			BeforeEach(func() {
   220  				dsl = func() {
   221  					Attribute(attName, Integer, func() {
   222  						MinLength(2)
   223  					})
   224  				}
   225  			})
   226  
   227  			It("produces an error", func() {
   228  				Ω(dslengine.Errors).Should(HaveOccurred())
   229  			})
   230  		})
   231  
   232  		Context("with a valid max length validation", func() {
   233  			BeforeEach(func() {
   234  				dsl = func() {
   235  					Attribute(attName, String, func() {
   236  						MaxLength(2)
   237  					})
   238  				}
   239  			})
   240  
   241  			It("records the validation", func() {
   242  				Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   243  				Ω(att.Validation.MaxLength).ShouldNot(BeNil())
   244  				Ω(*att.Validation.MaxLength).Should(Equal(2))
   245  			})
   246  		})
   247  
   248  		Context("with an invalid max length validation", func() {
   249  			BeforeEach(func() {
   250  				dsl = func() {
   251  					Attribute(attName, Integer, func() {
   252  						MaxLength(2)
   253  					})
   254  				}
   255  			})
   256  
   257  			It("produces an error", func() {
   258  				Ω(dslengine.Errors).Should(HaveOccurred())
   259  			})
   260  		})
   261  
   262  		Context("with a required field validation", func() {
   263  			BeforeEach(func() {
   264  				dsl = func() {
   265  					Attribute(attName, String)
   266  					Required(attName)
   267  				}
   268  			})
   269  
   270  			It("records the validation", func() {
   271  				Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   272  				Ω(Design.Types["bar"].Validation).ShouldNot(BeNil())
   273  				Ω(Design.Types["bar"].Validation.Required).Should(Equal([]string{attName}))
   274  			})
   275  		})
   276  	})
   277  })