@@ -2,6 +2,13 @@ const ANY = 'any'
22const ANY_INTEGER = 'integer'
33const ANY_FLOAT = 'float'
44const ANY_STRING = 'string'
5+ const OR = '|'
6+
7+ export function expandPath ( path , key ) {
8+ return ! path || path . length <= 0
9+ ? key
10+ : path + '.' + key
11+ }
512
613function toNotCompatible ( expected , actual , message , details = { } ) {
714 return {
@@ -51,6 +58,10 @@ function isValueEqual(expected, actual, compareExact = false) {
5158 if ( expected === ANY_STRING ) {
5259 return typeof actual === 'string'
5360 }
61+
62+ if ( expected . includes ( OR ) && expected . split ( OR ) . find ( e => e === actual ) ) {
63+ return true
64+ }
5465 }
5566
5667 if ( Number . isFinite ( expected ) && ! Number . isFinite ( actual ) ) {
@@ -89,7 +100,7 @@ function isIdenticalArrays(a, b) {
89100 return toCompatible ( )
90101}
91102
92- function isCompatibleObjects ( expected , actual , config ) {
103+ function isCompatibleObjects ( expected , actual , config , currPath ) {
93104 if ( Array . isArray ( expected ) ) {
94105 return isCompatibleArrays ( expected , actual , config )
95106 }
@@ -106,14 +117,17 @@ function isCompatibleObjects(expected, actual, config) {
106117 if ( config . ignoreJsonKeys . includes ( key ) ) {
107118 continue
108119 }
120+ else if ( config . ignoreJsonPaths . includes ( expandPath ( currPath , key ) ) ) {
121+ continue
122+ }
109123
110124 if ( expected [ key ] instanceof Object ) {
111125
112126 if ( ! ( actual [ key ] instanceof Object ) ) {
113127 return toNotCompatible ( expected , actual , '!(' + actual [ key ] + ') instanceof Object' )
114128 }
115129
116- let compatibleObjects = isCompatibleObjects ( expected [ key ] , actual [ key ] , config )
130+ let compatibleObjects = isCompatibleObjects ( expected [ key ] , actual [ key ] , config , expandPath ( currPath , key ) )
117131 if ( ! compatibleObjects . isEqual ) {
118132 return compatibleObjects
119133 }
@@ -124,7 +138,7 @@ function isCompatibleObjects(expected, actual, config) {
124138 return toNotCompatible ( expected , actual , '!Array.isArray(' + actual [ key ] + ')' )
125139 }
126140
127- let compatibleArrays = isCompatibleArrays ( expected [ key ] , actual [ key ] , config )
141+ let compatibleArrays = isCompatibleArrays ( expected [ key ] , actual [ key ] , config , expandPath ( currPath , key ) )
128142 if ( ! compatibleArrays . isEqual ) {
129143 return compatibleArrays
130144 }
@@ -140,10 +154,11 @@ function isCompatibleObjects(expected, actual, config) {
140154 }
141155 }
142156 }
157+
143158 return toCompatible ( )
144159}
145160
146- function isCompatibleArrays ( expected , actual , config ) {
161+ function isCompatibleArrays ( expected , actual , config , currPath ) {
147162 if ( ! Array . isArray ( actual ) ) {
148163 return toNotCompatible ( expected , actual , 'expected array was object' )
149164 }
@@ -165,13 +180,13 @@ function isCompatibleArrays(expected, actual, config) {
165180 const actualValue = actualToCompare [ j ]
166181
167182 if ( Array . isArray ( expectedValue ) ) {
168- let compatibilityMessage = isCompatibleArrays ( expectedValue , actualValue , config )
183+ let compatibilityMessage = isCompatibleArrays ( expectedValue , actualValue , config , expandPath ( currPath , 'n' ) )
169184 if ( ! compatibilityMessage . isEqual ) {
170185 return compatibilityMessage
171186 }
172187 }
173188 else {
174- let compatibilityMessage = isCompatibleObjects ( expectedValue , actualValue , config )
189+ let compatibilityMessage = isCompatibleObjects ( expectedValue , actualValue , config , expandPath ( currPath , 'n' ) )
175190 if ( compatibilityMessage . isEqual ) {
176191 expectedFound . push ( expectedValue )
177192 actualToCompare [ j ] = undefined
@@ -216,8 +231,8 @@ function isCompatibleArrays(expected, actual, config) {
216231
217232export function compareJson ( expected , actual , config ) {
218233 return Array . isArray ( expected )
219- ? isCompatibleArrays ( expected , actual , config )
220- : isCompatibleObjects ( expected , actual , config )
234+ ? isCompatibleArrays ( expected , actual , config , '' )
235+ : isCompatibleObjects ( expected , actual , config , '' )
221236}
222237
223238export const COMPARISON = {
@@ -227,16 +242,16 @@ export const COMPARISON = {
227242 EXACT : 'exact'
228243}
229244
230- export function toConfig ( comparison , ignoreJsonKeys , ignoreJsonPaths , allValuePaths ) {
245+ export function toConfig ( comparison , ignoreJsonKeys , ignoreJsonPaths ) {
231246 switch ( comparison . toLowerCase ( ) ) {
232247 case COMPARISON . COMPATIBLE_STRUCTURE :
233- return toConfigDto ( false , false , ignoreJsonKeys , ignoreJsonPaths , allValuePaths )
248+ return toConfigDto ( false , false , ignoreJsonKeys , ignoreJsonPaths )
234249 case COMPARISON . COMPATIBLE :
235- return toConfigDto ( true , false , ignoreJsonKeys , ignoreJsonPaths , allValuePaths )
250+ return toConfigDto ( true , false , ignoreJsonKeys , ignoreJsonPaths )
236251 case COMPARISON . EXACT_STRUCTURE :
237- return toConfigDto ( false , true , ignoreJsonKeys , ignoreJsonPaths , allValuePaths )
252+ return toConfigDto ( false , true , ignoreJsonKeys , ignoreJsonPaths )
238253 case COMPARISON . EXACT :
239- return toConfigDto ( true , true , ignoreJsonKeys , ignoreJsonPaths , allValuePaths )
254+ return toConfigDto ( true , true , ignoreJsonKeys , ignoreJsonPaths )
240255 default :
241256 throw {
242257 error : 'Comparison unsupported: ' + comparison . toLowerCase ( ) ,
@@ -245,12 +260,11 @@ export function toConfig(comparison, ignoreJsonKeys, ignoreJsonPaths, allValuePa
245260 }
246261}
247262
248- function toConfigDto ( compareValues , compareExact , ignoreJsonKeys , ignoreJsonPaths , allValuePaths ) {
263+ function toConfigDto ( compareValues , compareExact , ignoreJsonKeys , ignoreJsonPaths ) {
249264 return {
250265 compareValues,
251266 compareExact,
252267 ignoreJsonKeys,
253- ignoreJsonPaths,
254- allValuePaths
268+ ignoreJsonPaths
255269 }
256270}
0 commit comments