UNPKG

asl-validator

Version:
139 lines (138 loc) 4.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExactlyOne = exports.None = exports.AtMostOne = exports.stateChecks = exports.IsChoice = exports.IsFail = exports.IsSucceed = exports.IsWait = exports.IsTask = exports.IsMap = void 0; var get_states_1 = require("./get-states"); var jsonpath_plus_1 = require("jsonpath-plus"); var IsMap = function (_a) { var state = _a.state; return state.Type === "Map"; }; exports.IsMap = IsMap; var IsTask = function (_a) { var state = _a.state; return state.Type === "Task"; }; exports.IsTask = IsTask; var IsWait = function (_a) { var state = _a.state; return state.Type === "Wait"; }; exports.IsWait = IsWait; var IsSucceed = function (_a) { var state = _a.state; return state.Type === "Succeed"; }; exports.IsSucceed = IsSucceed; var IsFail = function (_a) { var state = _a.state; return state.Type === "Fail"; }; exports.IsFail = IsFail; var IsChoice = function (_a) { var state = _a.state; return state.Type === "Choice"; }; exports.IsChoice = IsChoice; var stateChecks = function (definition, options, checks) { var errorMessages = []; // for each embedded state machine (0, get_states_1.getStatesContainer)(definition).forEach(function (states) { // for each state entry (state + name) (0, get_states_1.getStates)(states).forEach(function (entry) { // walk all checks and see if any care about this entry. // if so, invoke that checker and append any errors errorMessages.push.apply(errorMessages, checks .filter(function (check) { return check.filter(entry); }) .map(function (_a) { var checker = _a.checker; return checker(entry); }) .filter(function (maybeErr) { return !!maybeErr; }) .map(function (err) { return err; })); }); }); return errorMessages; }; exports.stateChecks = stateChecks; var getPropertyCount = function (_a) { var props = _a.props, object = _a.object; return props .map(function (prop) { return (prop in object ? 1 : 0); }) .map(function (val) { return Number(val); }) .reduce(function (prev, curr) { return prev + curr; }, 0); }; var enforceMaxCount = function (_a) { var props = _a.props, errorCode = _a.errorCode, path = _a.path, maxCount = _a.maxCount, errorMessage = _a.errorMessage; return function (_a) { var state = _a.state, stateName = _a.stateName; var object = path ? (0, jsonpath_plus_1.JSONPath)({ path: path, json: state, wrap: false, }) : state; if (!object) { return null; } var count = getPropertyCount({ object: object, props: props }); if (count > maxCount) { return { "Error code": errorCode, // Use of JSONPath within the error message is unnecessary // since the state names are unique. Message: "State \"".concat(stateName, "\" ").concat(errorMessage, " ").concat(props .map(function (p) { return "\"".concat(p, "\""); }) .join(", ")), }; } return null; }; }; var AtMostOne = function (_a) { var props = _a.props, errorCode = _a.errorCode, path = _a.path; return enforceMaxCount({ maxCount: 1, props: props, errorCode: errorCode, path: path, errorMessage: "MUST contain at most one of", }); }; exports.AtMostOne = AtMostOne; var None = function (_a) { var props = _a.props, errorCode = _a.errorCode, path = _a.path; return enforceMaxCount({ maxCount: 0, props: props, errorCode: errorCode, path: path, errorMessage: "MUST NOT contain any of", }); }; exports.None = None; var ExactlyOne = function (_a) { var props = _a.props, errorCode = _a.errorCode; return function (_a) { var state = _a.state, stateName = _a.stateName; var count = getPropertyCount({ object: state, props: props }); if (count !== 1) { return { "Error code": errorCode, // Use of JSONPath within the error message is unnecessary // since the state names are unique. Message: "State \"".concat(stateName, "\" MUST contain exactly one of ").concat(props .map(function (p) { return "\"".concat(p, "\""); }) .join(", ")), }; } return null; }; }; exports.ExactlyOne = ExactlyOne;