diff --git a/src/compare.js b/src/compare.js index b96473d..35455c5 100644 --- a/src/compare.js +++ b/src/compare.js @@ -1,5 +1,6 @@ -const { isPrimitive, getType } = require('./helpers'); +const { isPrimitive, getType, isPactumMatchersLikeObj } = require('./helpers'); const patterns = require('./patterns'); +const { setRules } = require('./rules'); function compare(actual, expected, rules, path) { const regex_rules = getRegExRules(rules); @@ -83,7 +84,7 @@ function compareWithRule(actual, expected, rules, regex_rules, path, rule) { compareWithRuleRegex(actual, rule, path); break; case 'oneOf': - compareWithRuleOneOf(actual, rule, path); + compareWithRuleOneOf(...arguments); break; case 'expr': compareWithRuleExpr(actual, rule, path); @@ -162,15 +163,35 @@ function compareWithRuleRegex(actual, rule, path) { } } -function compareWithRuleOneOf(actual, rule, path) { +function compareWithRuleOneOf(actual, expected, rules, regex_rules, path, rule) { const values = rule.value; let found = false; + const errorMsg = []; for (let i = 0; i < values.length; i++) { - found = actual === values[i]; + if (isPactumMatchersLikeObj(values[i])) { + const nxtRules = {}; + setRules(nxtRules, values[i], path); + /** + * `_compare` func will throw an error if `actual` doesn't match `nxtRules` + * so if there is no error, `found` should be true + * otherwise we should continue the loop and handle the rest of `values` + */ + try { + _compare(actual, expected, nxtRules, regex_rules, path); + found = true; + } catch (error) { + errorMsg.push(error); + continue; + } + } else { + found = actual === values[i]; + } if (found) break; } if (!found) { - throw `Json doesn't have one of the expected values at "${path}" but found "${actual}"`; + errorMsg.push(`Json doesn't have one of the expected values at '${path}' but found '${actual}'`) + const str = errorMsg.join('\n') + throw str; } } diff --git a/src/helpers.js b/src/helpers.js index d8df423..1c39cf8 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -10,6 +10,16 @@ function isPrimitive(value) { return typeof value !== 'object'; } +function isPactumMatchersLikeObj(data) { + if (data === null) { + return false; + } + return ( + Object.prototype.hasOwnProperty.call(data, 'pactum_type') + && Object.prototype.hasOwnProperty.call(data, 'value') + ); +} + function getType(value) { const type = typeof value; if (type === 'object') { @@ -23,5 +33,6 @@ module.exports = { isPureObject, isObject, getType, - isPrimitive + isPrimitive, + isPactumMatchersLikeObj }; \ No newline at end of file