@@ -313,7 +313,62 @@ module.exports.nodeVersionSupportsJWE = function () {
313313 return nodeMajorVersion > 12 ;
314314} ;
315315
316- module . exports . hasConfig = function ( config , endpoint ) {
316+ module . exports . checkConfigFieldsArePopulated = function ( config , propertiesBasic , propertiesField , propertiesHeader ) {
317+ const contains = ( props ) => {
318+ return props . every ( ( elem ) => {
319+ return config [ elem ] !== null && typeof config [ elem ] !== "undefined" ;
320+ } ) ;
321+ } ;
322+ if ( typeof config !== "object" || config === null ) {
323+ throw Error ( "Config not valid: config should be an object." ) ;
324+ }
325+ if (
326+ config [ "paths" ] === null ||
327+ typeof config [ "paths" ] === "undefined" ||
328+ ! ( config [ "paths" ] instanceof Array )
329+ ) {
330+ throw Error ( "Config not valid: paths should be an array of path element." ) ;
331+ }
332+ if (
333+ ! contains ( propertiesBasic )
334+ ) {
335+ throw Error (
336+ "Config not valid: please check that all the properties are defined."
337+ ) ;
338+ }
339+ if (
340+ propertiesField && propertiesHeader && ! contains ( propertiesField ) && ! contains ( propertiesHeader )
341+ ) {
342+ throw Error (
343+ "Config not valid: please check that all the properties are defined."
344+ ) ;
345+ }
346+ if ( config [ "paths" ] . length === 0 ) {
347+ throw Error ( "Config not valid: paths should be not empty." ) ;
348+ }
349+ return contains ;
350+ } ;
351+
352+ module . exports . validateRootMapping = function ( config ) {
353+ function multipleRoots ( elem ) {
354+ return (
355+ elem . length !== 1 &&
356+ elem . some ( ( item ) => {
357+ return item . obj === "$" || item . element === "$" ;
358+ } )
359+ ) ;
360+ }
361+
362+ config . paths . forEach ( ( path ) => {
363+ if ( multipleRoots ( path . toEncrypt ) || multipleRoots ( path . toDecrypt ) ) {
364+ throw Error (
365+ "Config not valid: found multiple configurations encrypt/decrypt with root mapping"
366+ ) ;
367+ }
368+ } ) ;
369+ } ;
370+
371+ module . exports . hasConfig = function ( config , endpoint ) {
317372 if ( config && endpoint ) {
318373 endpoint = endpoint . split ( "?" ) . shift ( ) ;
319374 const conf = config . paths . find ( ( elem ) => {
@@ -344,10 +399,11 @@ module.exports.elemFromPath = function (path, obj) {
344399 }
345400} ;
346401
347- module . exports . isJsonRoot = function ( elem ) {
402+ module . exports . isJsonRoot = function ( elem ) {
348403 return isJsonRoot ( elem ) ;
349404} ;
350- function isJsonRoot ( elem ) {
405+
406+ function isJsonRoot ( elem ) {
351407 return elem === "$" ;
352408}
353409
@@ -359,21 +415,5 @@ function hasEncryptionParam(encParams, bodyMap) {
359415 return encParams && encParams . length === 1 && bodyMap && bodyMap [ 0 ] ;
360416}
361417
362- module . exports . validateRootMapping = function ( config ) {
363- function multipleRoots ( elem ) {
364- return (
365- elem . length !== 1 &&
366- elem . some ( ( item ) => {
367- return item . obj === "$" || item . element === "$" ;
368- } )
369- ) ;
370- }
371418
372- config . paths . forEach ( ( path ) => {
373- if ( multipleRoots ( path . toEncrypt ) || multipleRoots ( path . toDecrypt ) ) {
374- throw Error (
375- "Config not valid: found multiple configurations encrypt/decrypt with root mapping"
376- ) ;
377- }
378- } ) ;
379- } ;
419+
0 commit comments