UNPKG

stapp-validate

Version:

Form validation module for stapp - modular state manager

47 lines 1.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var rxjs_1 = require("rxjs"); var operators_1 = require("rxjs/operators"); var stapp_formbase_1 = require("stapp-formbase"); var isPromise_1 = require("stapp/lib/helpers/is/isPromise/isPromise"); var events_1 = require("./events"); /** * @private */ exports.normalizeResult = function (fieldName, result) { var _a; // if it's a promise, unwrap if (isPromise_1.isPromise(result)) { return rxjs_1.from(result).pipe(operators_1.switchMap(function (promiseResult) { return exports.normalizeResult(fieldName, promiseResult); }), operators_1.catchError(function (error) { var _a; return rxjs_1.of(stapp_formbase_1.setError((_a = {}, _a[fieldName] = error, _a))); })); } // if it's a plain object, return if (result != null && typeof result === 'object') { return rxjs_1.of(stapp_formbase_1.setError(result)); } // in all other cases wrap to an object return rxjs_1.of(stapp_formbase_1.setError((_a = {}, _a[fieldName] = result, _a))); }; /** * @private */ exports.runValidation = function (state, fieldName, rule, flags) { var _a, _b; var result; try { result = rule(state.values[fieldName], fieldName, state, flags); } catch (error) { result = error; } var syncMode = !isPromise_1.isPromise(result); var result$ = exports.normalizeResult(fieldName, result); return syncMode ? result$ : rxjs_1.concat(rxjs_1.of(events_1.asyncValidationStart(fieldName), stapp_formbase_1.setReady((_a = {}, _a[fieldName] = false, _a))), result$, rxjs_1.of(events_1.asyncValidationEnd(fieldName), stapp_formbase_1.setReady((_b = {}, _b[fieldName] = true, _b)))); }; //# sourceMappingURL=helpers.js.map