File tree Expand file tree Collapse file tree 2 files changed +30
-4
lines changed Expand file tree Collapse file tree 2 files changed +30
-4
lines changed Original file line number Diff line number Diff line change @@ -1421,11 +1421,16 @@ export const composeHandler = ({
1421
1421
fnLiteral += `const isNotEmptyObject=c.body&&(typeof c.body==="object"&&isNotEmpty(c.body))\n`
1422
1422
1423
1423
if ( hasProperty ( 'default' , validator . body ) ) {
1424
+ // @ts -expect-error private property
1425
+ const schema = validator . body . schema
1424
1426
const value = Value . Default (
1425
- // @ts -expect-error private property
1426
- validator . body . schema ,
1427
- // @ts -expect-error private property
1428
- validator . body . schema . type === 'object' ? { } : undefined
1427
+ schema ,
1428
+ schema . type === 'object' ||
1429
+ ( schema [ TypeBoxSymbol . kind ] === 'Import' &&
1430
+ schema . $defs [ schema . $ref ] [ TypeBoxSymbol . kind ] ===
1431
+ 'Object' )
1432
+ ? { }
1433
+ : undefined
1429
1434
)
1430
1435
1431
1436
const parsed =
Original file line number Diff line number Diff line change @@ -795,4 +795,25 @@ describe('Body Validator', () => {
795
795
796
796
expect ( response ) . toEqual ( { field : { decoded : 'bar' } , foo : 'test' } )
797
797
} )
798
+
799
+ it ( 'right rejects missed field with model' , async ( ) => {
800
+ const model = new Elysia ( ) . model (
801
+ 'user' ,
802
+ t . Object ( {
803
+ username : t . String ( ) ,
804
+ age : t . Integer ( )
805
+ } )
806
+ )
807
+
808
+ const app = new Elysia ( ) . use ( model ) . post ( '/' , ( { body } ) => body , {
809
+ body : 'user'
810
+ } )
811
+ const res = await app . handle (
812
+ post ( '/' , {
813
+ name : 'sucrose'
814
+ } )
815
+ )
816
+
817
+ expect ( res . status ) . toBe ( 422 )
818
+ } )
798
819
} )
You can’t perform that action at this time.
0 commit comments