File tree Expand file tree Collapse file tree 2 files changed +55
-5
lines changed
Expand file tree Collapse file tree 2 files changed +55
-5
lines changed Original file line number Diff line number Diff line change @@ -419,17 +419,23 @@ export default class ObjectSchema<
419419 if ( this . fields [ key ] ) picked [ key ] = this . fields [ key ] ;
420420 }
421421
422- return this . setFields < { [ K in TKey ] : TIn [ K ] } , TDefault > ( picked ) ;
422+ return this . setFields < { [ K in TKey ] : TIn [ K ] } , TDefault > (
423+ picked ,
424+ this . _excludedEdges . filter (
425+ ( [ a , b ] ) => keys . includes ( a as TKey ) && keys . includes ( b as TKey ) ,
426+ ) ,
427+ ) ;
423428 }
424429
425430 omit < TKey extends keyof TIn > ( keys : readonly TKey [ ] ) {
426- const fields = { ... this . fields } ;
431+ const remaining : TKey [ ] = [ ] ;
427432
428- for ( const key of keys ) {
429- delete fields [ key ] ;
433+ for ( const key of Object . keys ( this . fields ) as TKey [ ] ) {
434+ if ( keys . includes ( key ) ) continue ;
435+ remaining . push ( key ) ;
430436 }
431437
432- return this . setFields < Omit < TIn , TKey > , TDefault > ( fields ) ;
438+ return this . pick ( remaining ) ;
433439 }
434440
435441 from ( from : string , to : keyof TIn , alias ?: boolean ) {
Original file line number Diff line number Diff line change @@ -1121,4 +1121,48 @@ describe('Object types', () => {
11211121 await inst . omit ( [ 'age' , 'name' ] ) . validate ( { color : 'mauve' } ) ,
11221122 ) . toEqual ( { color : 'mauve' } ) ;
11231123 } ) ;
1124+
1125+ it ( 'should pick and omit with excluded edges' , async ( ) => {
1126+ const inst = object ( ) . shape (
1127+ {
1128+ a1 : string ( ) . when ( 'a2' , {
1129+ is : undefined ,
1130+ then : ( schema ) => schema . required ( ) ,
1131+ } ) ,
1132+ a2 : string ( ) . when ( 'a1' , {
1133+ is : undefined ,
1134+ then : ( schema ) => schema . required ( ) ,
1135+ } ) ,
1136+ a3 : string ( ) . required ( ) ,
1137+ } ,
1138+ [ [ 'a1' , 'a2' ] ] ,
1139+ ) ;
1140+
1141+ expect (
1142+ inst . pick ( [ 'a1' , 'a2' ] ) . isValid ( {
1143+ a1 : undefined ,
1144+ a2 : 'over9000' ,
1145+ } ) ,
1146+ ) . resolves . toEqual ( true ) ;
1147+
1148+ expect (
1149+ inst . pick ( [ 'a1' , 'a3' ] ) . isValid ( {
1150+ a1 : 'required' ,
1151+ a3 : 'asfasf' ,
1152+ } ) ,
1153+ ) . resolves . toEqual ( true ) ;
1154+
1155+ expect (
1156+ inst . omit ( [ 'a1' , 'a2' ] ) . isValid ( {
1157+ a3 : 'asfasf' ,
1158+ } ) ,
1159+ ) . resolves . toEqual ( true ) ;
1160+
1161+ expect (
1162+ inst . omit ( [ 'a1' ] ) . isValid ( {
1163+ a1 : undefined ,
1164+ a3 : 'asfasf' ,
1165+ } ) ,
1166+ ) . resolves . toEqual ( false ) ;
1167+ } ) ;
11241168} ) ;
You can’t perform that action at this time.
0 commit comments