UNPKG

@berish/validate

Version:

Validation of complex objects with support for validation maps, rules and decorators

188 lines 10.4 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const pathof_1 = require("@berish/pathof"); const getValidateMapCompact_1 = require("./getValidateMapCompact"); const zipValidateMapCompact_1 = require("./zipValidateMapCompact"); const executeRule_1 = require("../rule/executeRule"); const flags_1 = require("../rule/flags"); const isValidateMapCompact_1 = require("./isValidateMapCompact"); const isValidateMap_1 = require("./isValidateMap"); const rule_1 = require("../rule"); const linq_1 = require("@berish/linq"); function validateMapSync(obj, map, showOnlyInvalid) { const globalValidateMapCompact = isValidateMapCompact_1.isValidateMapCompact(map) ? zipValidateMapCompact_1.zipValidateMapCompact(map) : isValidateMap_1.isValidateMap(map) ? getValidateMapCompact_1.getValidateMapCompact(map) : null; if (!globalValidateMapCompact) return null; if (globalValidateMapCompact.length <= 0) return []; const cacheSet = new WeakSet(); const _validateMapMethod = (obj, validateMapCompact) => { const currentPathResult = pathof_1.of(obj); const data = validateMapCompact.map(([dataPath, rulesRef]) => { const dataKey = dataPath.length === 0 ? null : dataPath[dataPath.length - 1]; const dataValue = dataPath.reduce((of, key) => of(key), currentPathResult).get(); const isObject = dataValue !== null && typeof dataValue === 'object'; const isCache = isObject && cacheSet.has(dataValue); const [rules, ref] = rule_1.isRuleReference(rulesRef) ? [[], rulesRef] : rule_1.isRuleReferenceTuple(rulesRef) ? rulesRef : rule_1.isRuleArray(rulesRef) ? [rulesRef, null] : [[], null]; if (isObject && !isCache) cacheSet.add(dataValue); const validateResults = []; const resultOfRules = rules .map(rule => { if (!rule) return null; const executeResult = executeRule_1.executeRuleSync(rule, { target: obj, key: dataKey, value: dataValue }); return { name: rule.ruleName, isValid: executeResult === flags_1.FLAG_CONDITION_TRUTHY, errorText: executeResult === flags_1.FLAG_CONDITION_TRUTHY ? null : executeResult, }; }) .filter(m => !!m); validateResults.push({ key: dataPath, rules: resultOfRules }); if (ref && isObject && !isCache) { const { $$ref } = ref; const refMap = globalValidateMapCompact // Получаем элементы из глобальной карты по началу пути .filter(m => $$ref.every((key, i) => m[0][i] === key)) // Очищаем найденные пути от текущего пути .map(m => [m[0].filter((key, i) => typeof $$ref[i] === 'undefined' || $$ref[i] !== key), m[1]]); // console.log(refMap); if (refMap.length > 0) { const items = _validateMapMethod(dataValue, refMap); if (items.length > 0) { const itemsWithCorretKeys = items.map(m => ({ key: dataPath.concat(...m.key), rules: m.rules })); const itemsNeedConcat = itemsWithCorretKeys.filter(m => m.key.length === dataPath.length && m.key.every((k, i) => dataPath[i] === k)); if (itemsNeedConcat.length > 0) { validateResults[0].rules = [ ...validateResults[0].rules, ...linq_1.default.from(itemsNeedConcat) .selectMany(m => m.rules) .toArray(), ]; validateResults.push(...itemsWithCorretKeys.filter(m => itemsNeedConcat.indexOf(m) === -1)); } else validateResults.push(...itemsWithCorretKeys); } } } return validateResults; // return { key: subPathResult.path, rules: validateRuleResults }; }); return linq_1.default.from(data) .selectMany(m => m) .toArray(); }; const results = _validateMapMethod(obj, globalValidateMapCompact); if (!showOnlyInvalid) return results; return results.filter(m => { m.rules = m.rules.filter(m => !m.isValid); return m.rules.length > 0; }); } exports.validateMapSync = validateMapSync; function validateMapAsync(obj, map, showOnlyInvalid) { return __awaiter(this, void 0, void 0, function* () { const globalValidateMapCompact = isValidateMapCompact_1.isValidateMapCompact(map) ? zipValidateMapCompact_1.zipValidateMapCompact(map) : isValidateMap_1.isValidateMap(map) ? getValidateMapCompact_1.getValidateMapCompact(map) : null; if (!globalValidateMapCompact) return null; if (globalValidateMapCompact.length <= 0) return []; const cacheSet = new WeakSet(); const _validateMapMethod = (obj, validateMapCompact) => __awaiter(this, void 0, void 0, function* () { const currentPathResult = pathof_1.of(obj); const data = yield Promise.all(validateMapCompact.map(([dataPath, rulesRef]) => __awaiter(this, void 0, void 0, function* () { const dataKey = dataPath.length === 0 ? null : dataPath[dataPath.length - 1]; const dataValue = dataPath.reduce((of, key) => of(key), currentPathResult).get(); const isObject = dataValue !== null && typeof dataValue === 'object'; const isCache = isObject && cacheSet.has(dataValue); const [rules, ref] = rule_1.isRuleReference(rulesRef) ? [[], rulesRef] : rule_1.isRuleReferenceTuple(rulesRef) ? rulesRef : rule_1.isRuleArray(rulesRef) ? [rulesRef, null] : [[], null]; if (isObject && !isCache) cacheSet.add(dataValue); const validateResults = []; const resultOfRules = yield Promise.all(rules.map((rule) => __awaiter(this, void 0, void 0, function* () { if (!rule) return null; const executeResult = yield executeRule_1.executeRuleAsync(rule, { target: obj, key: dataKey, value: dataValue }); return { name: rule.ruleName, isValid: executeResult === flags_1.FLAG_CONDITION_TRUTHY, errorText: executeResult === flags_1.FLAG_CONDITION_TRUTHY ? null : executeResult, }; }))).then(results => results.filter(m => !!m)); validateResults.push({ key: dataPath, rules: resultOfRules }); if (ref && isObject && !isCache) { const { $$ref } = ref; const refMap = globalValidateMapCompact // Получаем элементы из глобальной карты по началу пути .filter(m => $$ref.every((key, i) => m[0][i] === key)) // Очищаем найденные пути от текущего пути .map(m => [m[0].filter((key, i) => typeof $$ref[i] === 'undefined' || $$ref[i] !== key), m[1]]); // console.log(refMap); if (refMap.length > 0) { const items = yield _validateMapMethod(dataValue, refMap); if (items.length > 0) { const itemsWithCorretKeys = items.map(m => ({ key: dataPath.concat(...m.key), rules: m.rules })); const itemsNeedConcat = itemsWithCorretKeys.filter(m => m.key.length === dataPath.length && m.key.every((k, i) => dataPath[i] === k)); if (itemsNeedConcat.length > 0) { validateResults[0].rules = [ ...validateResults[0].rules, ...linq_1.default.from(itemsNeedConcat) .selectMany(m => m.rules) .toArray(), ]; validateResults.push(...itemsWithCorretKeys.filter(m => itemsNeedConcat.indexOf(m) === -1)); } else validateResults.push(...itemsWithCorretKeys); } } } return validateResults; }))); return linq_1.default.from(data) .selectMany(m => m) .toArray(); }); const results = yield _validateMapMethod(obj, globalValidateMapCompact); if (!showOnlyInvalid) return results; return results.filter(m => { m.rules = m.rules.filter(m => !m.isValid); return m.rules.length > 0; }); }); } exports.validateMapAsync = validateMapAsync; //# sourceMappingURL=validateMap.js.map