File tree 2 files changed +55
-5
lines changed
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<
419
419
if ( this . fields [ key ] ) picked [ key ] = this . fields [ key ] ;
420
420
}
421
421
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
+ ) ;
423
428
}
424
429
425
430
omit < TKey extends keyof TIn > ( keys : readonly TKey [ ] ) {
426
- const fields = { ... this . fields } ;
431
+ const remaining : TKey [ ] = [ ] ;
427
432
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 ) ;
430
436
}
431
437
432
- return this . setFields < Omit < TIn , TKey > , TDefault > ( fields ) ;
438
+ return this . pick ( remaining ) ;
433
439
}
434
440
435
441
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', () => {
1121
1121
await inst . omit ( [ 'age' , 'name' ] ) . validate ( { color : 'mauve' } ) ,
1122
1122
) . toEqual ( { color : 'mauve' } ) ;
1123
1123
} ) ;
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
+ } ) ;
1124
1168
} ) ;
You can’t perform that action at this time.
0 commit comments