1
- import { Type as T , TypeBoxError } from '@sinclair/typebox' ;
1
+ import { Type as T } from '@sinclair/typebox'
2
2
import { Hono } from 'hono'
3
3
import type { Equal , Expect } from 'hono/utils/types'
4
4
import { tbValidator } from '../src'
5
- import { ValueError } from '@sinclair/typebox/value' ;
5
+ import { ValueError } from '@sinclair/typebox/value'
6
6
7
7
// eslint-disable-next-line @typescript-eslint/no-unused-vars
8
8
type ExtractSchema < T > = T extends Hono < infer _ , infer S > ? S : never
@@ -106,7 +106,7 @@ describe('With Hook', () => {
106
106
success : true ,
107
107
message : `${ data . id } is ${ data . title } ` ,
108
108
} )
109
- }
109
+ } ,
110
110
) . post (
111
111
'/errorTest' ,
112
112
tbValidator ( 'json' , schema , ( result , c ) => {
@@ -118,7 +118,7 @@ describe('With Hook', () => {
118
118
success : true ,
119
119
message : `${ data . id } is ${ data . title } ` ,
120
120
} )
121
- }
121
+ } ,
122
122
)
123
123
124
124
it ( 'Should return 200 response' , async ( ) => {
@@ -168,24 +168,156 @@ describe('With Hook', () => {
168
168
expect ( res ) . not . toBeNull ( )
169
169
expect ( res . status ) . toBe ( 400 )
170
170
171
- const { errors, success} = ( await res . json ( ) ) as { success : boolean ; errors : any [ ] }
171
+ const { errors, success } = ( await res . json ( ) ) as { success : boolean ; errors : any [ ] }
172
172
expect ( success ) . toBe ( false )
173
173
expect ( Array . isArray ( errors ) ) . toBe ( true )
174
174
expect ( errors . map ( ( e : ValueError ) => ( {
175
175
'type' : e ?. schema ?. type ,
176
- path : e ?. path ,
177
- message : e ?. message
176
+ path : e ?. path ,
177
+ message : e ?. message ,
178
178
} ) ) ) . toEqual ( [
179
179
{
180
- " type" : " string" ,
181
- " path" : " /title" ,
182
- " message" : " Required property"
180
+ ' type' : ' string' ,
181
+ ' path' : ' /title' ,
182
+ ' message' : ' Required property' ,
183
183
} ,
184
184
{
185
- " type" : " string" ,
186
- " path" : " /title" ,
187
- " message" : " Expected string"
188
- }
185
+ ' type' : ' string' ,
186
+ ' path' : ' /title' ,
187
+ ' message' : ' Expected string' ,
188
+ } ,
189
189
] )
190
190
} )
191
191
} )
192
+
193
+ describe ( 'Remove non schema items' , ( ) => {
194
+ const app = new Hono ( )
195
+ const schema = T . Object ( {
196
+ id : T . Number ( ) ,
197
+ title : T . String ( ) ,
198
+ } )
199
+
200
+ const nestedSchema = T . Object ( {
201
+ id : T . Number ( ) ,
202
+ itemArray : T . Array ( schema ) ,
203
+ item : schema ,
204
+ itemObject : T . Object ( {
205
+ item1 : schema ,
206
+ item2 : schema ,
207
+ } ) ,
208
+ } )
209
+
210
+ app . post (
211
+ '/stripValuesNested' ,
212
+ tbValidator ( 'json' , nestedSchema , undefined , true ) ,
213
+ ( c ) => {
214
+ return c . json ( {
215
+ success : true ,
216
+ message : c . req . valid ( 'json' ) ,
217
+ } )
218
+ } ,
219
+ ) . post (
220
+ '/stripValuesArray' ,
221
+ tbValidator ( 'json' , T . Array ( schema ) , undefined , true ) ,
222
+ ( c ) => {
223
+ return c . json ( {
224
+ success : true ,
225
+ message : c . req . valid ( 'json' ) ,
226
+ } )
227
+ } ,
228
+ )
229
+
230
+ it ( 'Should remove all the values in the nested object and return a 200 response' , async ( ) => {
231
+ const req = new Request ( 'http://localhost/stripValuesNested' , {
232
+ body : JSON . stringify ( {
233
+ id : 123 ,
234
+ nonExistentKey : 'error' ,
235
+ itemArray : [
236
+ {
237
+ id : 123 ,
238
+ title : 'Hello' ,
239
+ nonExistentKey : 'error' ,
240
+ } ,
241
+ {
242
+ id : 123 ,
243
+ title : 'Hello' ,
244
+ nonExistentKey : 'error' ,
245
+ nonExistentKey2 : 'error 2' ,
246
+ } ,
247
+ ] ,
248
+ item : {
249
+ id : 123 ,
250
+ title : 'Hello' ,
251
+ nonExistentKey : 'error' ,
252
+ } ,
253
+ itemObject : {
254
+ item1 : {
255
+ id : 123 ,
256
+ title : 'Hello' ,
257
+ imaginaryKey : 'error' ,
258
+ } ,
259
+ item2 : {
260
+ id : 123 ,
261
+ title : 'Hello' ,
262
+ error : 'error' ,
263
+ } ,
264
+ } ,
265
+ } ) ,
266
+ method : 'POST' ,
267
+ headers : {
268
+ 'Content-Type' : 'application/json' ,
269
+ } ,
270
+ } )
271
+ const res = await app . request ( req )
272
+ expect ( res ) . not . toBeNull ( )
273
+ expect ( res . status ) . toBe ( 200 )
274
+
275
+ const { message, success } = ( await res . json ( ) ) as { success : boolean ; message : any }
276
+ expect ( success ) . toBe ( true )
277
+ expect ( message ) . toEqual (
278
+ {
279
+ 'id' : 123 ,
280
+ 'itemArray' : [ { 'id' : 123 , 'title' : 'Hello' } , {
281
+ 'id' : 123 ,
282
+ 'title' : 'Hello' ,
283
+ } ] ,
284
+ 'item' : { 'id' : 123 , 'title' : 'Hello' } ,
285
+ 'itemObject' : {
286
+ 'item1' : { 'id' : 123 , 'title' : 'Hello' } ,
287
+ 'item2' : { 'id' : 123 , 'title' : 'Hello' } ,
288
+ } ,
289
+ } ,
290
+ )
291
+ } )
292
+
293
+ it ( 'Should remove all the values in the array and return a 200 response' , async ( ) => {
294
+ const req = new Request ( 'http://localhost/stripValuesArray' , {
295
+ body : JSON . stringify ( [
296
+ {
297
+ id : 123 ,
298
+ title : 'Hello' ,
299
+ nonExistentKey : 'error' ,
300
+ } ,
301
+ {
302
+ id : 123 ,
303
+ title : 'Hello 2' ,
304
+ nonExistentKey : 'error' ,
305
+ } ,
306
+ ] ) , method : 'POST' ,
307
+ headers : {
308
+ 'Content-Type' : 'application/json' ,
309
+ } ,
310
+ } )
311
+
312
+ const res = await app . request ( req )
313
+ const { message, success } = ( await res . json ( ) ) as { success : boolean ; message : Array < any > }
314
+ expect ( res . status ) . toBe ( 200 )
315
+ expect ( success ) . toBe ( true )
316
+ expect ( message ) . toEqual ( [ { 'id' : 123 , 'title' : 'Hello' } , {
317
+ 'id' : 123 ,
318
+ 'title' : 'Hello 2' ,
319
+ } ] ,
320
+ )
321
+ } )
322
+ } )
323
+
0 commit comments