UNPKG

unmock-core

Version:

[![npm](https://img.shields.io/npm/v/unmock-core.svg)][npmjs] [![CircleCI](https://circleci.com/gh/unmock/unmock-js.svg?style=svg)](https://circleci.com/gh/unmock/unmock-js) [![codecov](https://codecov.io/gh/unmock/unmock-js/branch/dev/graph/badge.svg)](h

89 lines 4.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const debug_1 = require("debug"); const utils_1 = require("./utils"); const debugLog = debug_1.default("unmock:state:validator"); exports.getValidStatesForOperationWithState = (operation, state, deref) => { debugLog(`getValidStatesForOperationWithState: Attempting to match and copy a ` + `partial state that matches ${JSON.stringify(state.state)} from ${JSON.stringify(operation)}`); const resps = operation.responses; const code = state.top.$code; const { responses, error } = typeof code === "number" ? getValidStatesWithSingle$code(resps[String(code)] || resps.default, state, code, deref) : getValidStates(resps, code, state, deref); return { responses, error }; }; const getValidStatesWithSingle$code = (response, state, code, deref) => { debugLog(`validStatesForStateWithCode: Looking to match ${JSON.stringify(state)} with ${JSON.stringify(response)} for status code ${code}`); const resolvedResponse = deref(response); if (resolvedResponse === undefined || resolvedResponse.content === undefined) { debugLog(`validStatesForStateWithCode: Given undefined response`); return { error: { msg: `Can't find response for given status code '${code}'!`, nestedLevel: -1, }, }; } const stateMedia = getStateFromMedia(resolvedResponse.content, state, deref); return { responses: { [code]: stateMedia.responses }, error: stateMedia.error, }; }; const getValidStates = (operationResponses, codes, state, deref) => { debugLog(`validStatesForStateWithoutCode: Looking to match ${JSON.stringify(state)} with ${JSON.stringify(operationResponses)}`); const codesToUse = (codes && codes.map(c => c.toString())) || Object.keys(operationResponses); const mapped = codesToUse.map(code => { debugLog(`validStatesForStateWithoutCode: Testing against status code ${code}`); return Object.assign({ code }, getValidStatesWithSingle$code(operationResponses[code] || operationResponses.default, state, code, deref)); }); const responses = mapped .filter(o => o.error === undefined && o.responses !== undefined) .reduce((acc, o) => { const resp = o.responses[o.code]; const filtered = Object.keys(resp) .filter(contentType => Object.keys(resp[contentType]).length > 0) .reduce((obj, contentType) => (Object.assign(Object.assign({}, obj), { [contentType]: resp[contentType] })), {}); return Object.assign(Object.assign({}, acc), (Object.keys(filtered).length > 0 ? { [o.code]: filtered } : {})); }, {}); const success = Object.keys(responses).length > 0; const error = success ? undefined : utils_1.chooseErrorFromList(mapped.map(o => o.error)); return success ? { responses } : { error }; }; const getStateFromMedia = (contentRecord, state, deref) => { debugLog(`getStateFromMedia: Attempting to copy a partial state for ${state} from given media types ${contentRecord}`); const mapped = Object.keys(contentRecord).map(mediaType => { const content = contentRecord[mediaType]; if (content === undefined || content.schema === undefined) { debugLog(`getStateFromMedia: No schema defined in ${mediaType}`); return { mediaType, error: { msg: `No schema defined in '${JSON.stringify(content)}'!`, nestedLevel: -1, }, }; } const { spreadState, error } = state.gen(deref(content.schema)); debugLog(`getStateFromMedia: Spread state for ${mediaType} ${error !== undefined ? "has errors" : "is valid"}`); return error !== undefined ? { mediaType, error: { msg: error, nestedLevel: 0 } } : { mediaType, spreadState }; }); const responses = mapped .filter(obj => obj.spreadState !== undefined) .reduce((acc, obj) => (Object.assign(Object.assign({}, acc), { [obj.mediaType]: obj.spreadState })), {}); const success = Object.keys(responses).length > 0; return { responses, error: success ? undefined : utils_1.chooseErrorFromList(mapped.map(obj => obj.error)), }; }; //# sourceMappingURL=validator.js.map