UNPKG

pact-gen-ts

Version:

Generating pact files from typescript definitions

34 lines (33 loc) 1.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.changeObjectRepresentationIntoMatchingRules = void 0; const object_type_1 = require("../utils/object-type"); const matching_regex_formats_1 = require("../consts/matching-regex-formats"); const changeObjectRepresentationIntoMatchingRules = (objectRepresentation, level) => { const matchingRules = {}; const findAllMatchingRulesRecursive = (objectRepresentation, currentLevel) => { if ((0, object_type_1.isLiteralObject)(objectRepresentation.type)) { Object.entries(objectRepresentation.type).forEach(([fieldName, fieldObjectRepresentation]) => findAllMatchingRulesRecursive(fieldObjectRepresentation, `${currentLevel}.${fieldName}`)); } else if (objectRepresentation.isEnum) { matchingRules[currentLevel] = { match: 'regex', regex: objectRepresentation.enumValues?.join('|'), }; } else { const matchedFormat = matching_regex_formats_1.matchingRegexFormats[objectRepresentation.type || '']; if (matchedFormat) { matchingRules[currentLevel] = { match: 'regex', regex: matchedFormat, }; } } }; findAllMatchingRulesRecursive(objectRepresentation, level); if ((0, object_type_1.isEmptyObject)(matchingRules) === false) { return { [level]: { match: 'type' }, ...matchingRules }; } }; exports.changeObjectRepresentationIntoMatchingRules = changeObjectRepresentationIntoMatchingRules;