@@ -61,48 +61,119 @@ var _ = Describe("NewHandler", func() {
6161 })
6262 })
6363
64- When ("error is JSONSchemaValidationErrors " , func () {
64+ When ("error is jsonschema.ValidateError " , func () {
6565 var recorder * httptest.ResponseRecorder
6666
67- BeforeEach (func () {
68- recorder = testHandler (func (w http.ResponseWriter , r * http.Request ) error {
69- _ , err := ValidateJSONSchema (
70- & extv1.JSON {
71- Raw : testutil .MustMarshalJSON (map [string ]interface {}{
72- "type" : "object" ,
73- "properties" : map [string ]interface {}{
74- "foo" : map [string ]interface {}{
75- "type" : "string" ,
76- },
77- "bar" : map [string ]interface {}{
78- "type" : "string" ,
67+ Context ("single error" , func () {
68+ BeforeEach (func () {
69+ recorder = testHandler (func (w http.ResponseWriter , r * http.Request ) error {
70+ _ , err := ValidateJSONSchema (
71+ & extv1.JSON {
72+ Raw : testutil .MustMarshalJSON (map [string ]interface {}{
73+ "type" : "object" ,
74+ "properties" : map [string ]interface {}{
75+ "foo" : map [string ]interface {}{
76+ "type" : "string" ,
77+ },
7978 },
80- },
81- }),
82- },
83- & extv1.JSON {
84- Raw : testutil .MustMarshalJSON (map [string ]interface {}{
85- "foo" : 3 ,
86- "bar" : 4 ,
87- }),
88- },
89- )
79+ }),
80+ },
81+ & extv1.JSON {
82+ Raw : testutil .MustMarshalJSON (map [string ]interface {}{
83+ "foo" : true ,
84+ }),
85+ },
86+ )
9087
91- return err
88+ return err
89+ })
90+ })
91+
92+ It ("should respond 400" , func () {
93+ Expect (recorder ).To (HaveHTTPStatus (http .StatusBadRequest ))
94+ })
95+
96+ It ("should respond errors" , func () {
97+ var res httputil.Response
98+ Expect (json .NewDecoder (recorder .Body ).Decode (& res )).To (Succeed ())
99+ Expect (res .Errors ).To (ConsistOf ([]httputil.Error {
100+ {Field : "foo" , Description : "expected string, but got boolean" },
101+ }))
92102 })
93103 })
94104
95- It ("should respond 400" , func () {
96- Expect (recorder ).To (HaveHTTPStatus (http .StatusBadRequest ))
105+ Context ("multi errors" , func () {
106+ BeforeEach (func () {
107+ recorder = testHandler (func (w http.ResponseWriter , r * http.Request ) error {
108+ _ , err := ValidateJSONSchema (
109+ & extv1.JSON {
110+ Raw : testutil .MustMarshalJSON (map [string ]interface {}{
111+ "type" : "object" ,
112+ "properties" : map [string ]interface {}{
113+ "foo" : map [string ]interface {}{
114+ "type" : "string" ,
115+ },
116+ "bar" : map [string ]interface {}{
117+ "type" : "string" ,
118+ },
119+ },
120+ }),
121+ },
122+ & extv1.JSON {
123+ Raw : testutil .MustMarshalJSON (map [string ]interface {}{
124+ "foo" : 3 ,
125+ "bar" : 4 ,
126+ }),
127+ },
128+ )
129+
130+ return err
131+ })
132+ })
133+
134+ It ("should respond 400" , func () {
135+ Expect (recorder ).To (HaveHTTPStatus (http .StatusBadRequest ))
136+ })
137+
138+ It ("should respond errors" , func () {
139+ var res httputil.Response
140+ Expect (json .NewDecoder (recorder .Body ).Decode (& res )).To (Succeed ())
141+ Expect (res .Errors ).To (ConsistOf ([]httputil.Error {
142+ {Field : "foo" , Description : "expected string, but got number" },
143+ {Field : "bar" , Description : "expected string, but got number" },
144+ }))
145+ })
97146 })
98147
99- It ("should respond errors" , func () {
100- var res httputil.Response
101- Expect (json .NewDecoder (recorder .Body ).Decode (& res )).To (Succeed ())
102- Expect (res .Errors ).To (ConsistOf ([]httputil.Error {
103- {Type : "invalid_type" , Field : "foo" , Description : "Invalid type. Expected: string, given: integer" },
104- {Type : "invalid_type" , Field : "bar" , Description : "Invalid type. Expected: string, given: integer" },
105- }))
148+ Context ("root error" , func () {
149+ BeforeEach (func () {
150+ recorder = testHandler (func (w http.ResponseWriter , r * http.Request ) error {
151+ _ , err := ValidateJSONSchema (
152+ & extv1.JSON {
153+ Raw : testutil .MustMarshalJSON (map [string ]interface {}{
154+ "type" : "object" ,
155+ }),
156+ },
157+ & extv1.JSON {
158+ Raw : []byte ("null" ),
159+ },
160+ )
161+
162+ return err
163+ })
164+ })
165+
166+ It ("should respond 400" , func () {
167+ Expect (recorder ).To (HaveHTTPStatus (http .StatusBadRequest ))
168+ })
169+
170+ It ("should respond errors" , func () {
171+ var res httputil.Response
172+ Expect (json .NewDecoder (recorder .Body ).Decode (& res )).To (Succeed ())
173+ Expect (res .Errors ).To (ConsistOf ([]httputil.Error {
174+ {Description : "expected object, but got null" },
175+ }))
176+ })
106177 })
107178 })
108179
@@ -129,12 +200,21 @@ var _ = Describe("NewHandler", func() {
129200 })
130201 })
131202
132- When ("error is JSONSchemaValidateError " , func () {
203+ When ("error is jsonschema.SchemaError " , func () {
133204 var recorder * httptest.ResponseRecorder
134205
135206 BeforeEach (func () {
136207 recorder = testHandler (func (w http.ResponseWriter , r * http.Request ) error {
137- return JSONSchemaValidateError {}
208+ _ , err := ValidateJSONSchema (
209+ & extv1.JSON {
210+ Raw : testutil .MustMarshalJSON (map [string ]interface {}{
211+ "type" : "what" ,
212+ }),
213+ },
214+ & extv1.JSON {},
215+ )
216+
217+ return err
138218 })
139219 })
140220
@@ -145,7 +225,7 @@ var _ = Describe("NewHandler", func() {
145225 It ("should respond errors" , func () {
146226 Expect (recorder .Body ).To (MatchJSON (testutil .MustMarshalJSON (httputil.Response {
147227 Errors : []httputil.Error {
148- {Description : "Failed to validate against JSON schema" },
228+ {Description : "Invalid JSON schema" },
149229 },
150230 })))
151231 })
0 commit comments