88import type {
99 DataMaskingConstructorOptions ,
1010 DecryptOptions ,
11- EncryptOptions ,
1211 EncryptionProvider ,
12+ EncryptOptions ,
1313 EraseOptions ,
1414 MaskingRule ,
1515} from './types.js' ;
@@ -52,7 +52,10 @@ export class DataMasking {
5252
5353 if ( options . maskingRules ) {
5454 for ( const [ field , rule ] of Object . entries ( options . maskingRules ) ) {
55- for ( const path of this . #resolveFieldPaths( copy as Record < string , unknown > , field ) ) {
55+ for ( const path of this . #resolveFieldPaths(
56+ copy as Record < string , unknown > ,
57+ field
58+ ) ) {
5659 const value = getAtPath ( copy , path ) ;
5760 if ( typeof value !== 'string' ) {
5861 throw new DataMaskingUnsupportedTypeError (
@@ -67,7 +70,10 @@ export class DataMasking {
6770 }
6871
6972 for ( const field of options . fields ?? [ ] ) {
70- const paths = this . #resolveFieldPaths( copy as Record < string , unknown > , field ) ;
73+ const paths = this . #resolveFieldPaths(
74+ copy as Record < string , unknown > ,
75+ field
76+ ) ;
7177 if ( paths . length === 0 && this . #throwOnMissingField) {
7278 throw new DataMaskingFieldNotFoundError ( `Field not found: '${ field } '` ) ;
7379 }
@@ -164,7 +170,10 @@ export class DataMasking {
164170 const operations : Promise < void > [ ] = [ ] ;
165171
166172 for ( const field of fields ) {
167- for ( const path of this . #resolveFieldPaths( data as Record < string , unknown > , field ) ) {
173+ for ( const path of this . #resolveFieldPaths(
174+ data as Record < string , unknown > ,
175+ field
176+ ) ) {
168177 operations . push (
169178 transform ( getAtPath ( data , path ) ) . then ( ( result ) =>
170179 setAtPath ( data , path , result )
@@ -176,7 +185,10 @@ export class DataMasking {
176185 await Promise . all ( operations ) ;
177186 }
178187
179- #resolveFieldPaths( data : Record < string , unknown > , expression : string ) : string [ ] [ ] {
188+ #resolveFieldPaths(
189+ data : Record < string , unknown > ,
190+ expression : string
191+ ) : string [ ] [ ] {
180192 // JMESPath validates the expression and checks if it matches anything
181193 const matched = search ( expression , data ) ;
182194 if ( matched == null || ( Array . isArray ( matched ) && matched . length === 0 ) ) {
@@ -234,8 +246,8 @@ function setAtPath(data: unknown, path: string[], value: unknown): void {
234246 if ( current == null || typeof current !== 'object' ) return ;
235247 current = ( current as Record < string , unknown > ) [ path [ i ] ] ;
236248 }
237- const lastKey = path [ path . length - 1 ] ;
238- if ( RESERVED_KEYS . has ( lastKey ) ) return ;
249+ const lastKey = path . at ( - 1 ) ;
250+ if ( ! lastKey || RESERVED_KEYS . has ( lastKey ) ) return ;
239251 if ( current != null && typeof current === 'object' ) {
240252 ( current as Record < string , unknown > ) [ lastKey ] = value ;
241253 }
0 commit comments