@berish/validate
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -10,4 +10,6 @@ export * from './createRule'; | ||
export * from './isRuleMap'; | ||
export * from './isRuleReference'; | ||
export * from './isRuleReferenceTuple'; | ||
export * from './isRuleTuple'; | ||
export * from './registrator'; | ||
export * from './types'; |
@@ -15,4 +15,6 @@ "use strict"; | ||
__export(require("./isRuleMap")); | ||
__export(require("./isRuleReference")); | ||
__export(require("./isRuleReferenceTuple")); | ||
__export(require("./isRuleTuple")); | ||
__export(require("./registrator")); | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const isValidateMap_1 = require("../validateMap/isValidateMap"); | ||
const isRuleReference_1 = require("./isRuleReference"); | ||
const isRuleReferenceTuple_1 = require("./isRuleReferenceTuple"); | ||
function isRuleMap(obj) { | ||
if (obj && typeof obj === 'object' && !Array.isArray(obj) && Object.entries(obj).every(m => isValidateMap_1.isValidateMap(m[1]))) | ||
if (obj && | ||
typeof obj === 'object' && | ||
!Array.isArray(obj) && | ||
Object.entries(obj).every(m => isRuleReference_1.isRuleReference(m[1]) || isRuleReferenceTuple_1.isRuleReferenceTuple(m[1]) || isValidateMap_1.isValidateMap(m[1]))) | ||
return true; | ||
@@ -7,0 +12,0 @@ return false; |
@@ -1,3 +0,6 @@ | ||
export * from './getRulesFromMap'; | ||
export * from './getValidateMapCompact'; | ||
export * from './isValidateMap'; | ||
export * from './isValidateMapCompact'; | ||
export * from './isValidateMapCompactItem'; | ||
export * from './validateMap'; | ||
export * from './zipValidateMapCompact'; |
@@ -6,5 +6,8 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./getRulesFromMap")); | ||
__export(require("./getValidateMapCompact")); | ||
__export(require("./isValidateMap")); | ||
__export(require("./isValidateMapCompact")); | ||
__export(require("./isValidateMapCompactItem")); | ||
__export(require("./validateMap")); | ||
__export(require("./zipValidateMapCompact")); | ||
//# sourceMappingURL=index.js.map |
import { IValidateRule, IRuleErrorTextResult } from '../rule/types'; | ||
export declare type RuleArrayType = IValidateRule<any>[]; | ||
export declare type RuleTupleType<T> = [RuleArrayType, RuleMapType<T>]; | ||
export declare type RuleReferenceType = { | ||
$$ref: (string | symbol | number)[]; | ||
}; | ||
export declare type RuleReferenceTupleType = [RuleArrayType, RuleReferenceType]; | ||
export declare type RuleMapType<T> = { | ||
[Key in keyof T]?: ValidateMap<T[Key]>; | ||
[Key in keyof T]?: RuleReferenceType | RuleReferenceTupleType | ValidateMap<T[Key]>; | ||
}; | ||
export declare type RuleTupleType<T> = [RuleArrayType, RuleMapType<T>]; | ||
export declare type ValidateMap<T> = RuleArrayType | RuleMapType<T> | RuleTupleType<T>; | ||
export declare type ValidateMapCompactItem = [(string | symbol | number)[], RuleArrayType | RuleReferenceType | RuleReferenceTupleType]; | ||
export declare type ValidateMapCompact = ValidateMapCompactItem[]; | ||
export interface IValidationRuleResult { | ||
@@ -17,3 +23,3 @@ name: string; | ||
} | ||
export declare function validateMapSync<T>(obj: T, map: ValidateMap<T>, showOnlyInvalid?: boolean): IValidationResult[]; | ||
export declare function validateMapAsync<T>(obj: T, map: ValidateMap<T>, showOnlyInvalid?: boolean): Promise<IValidationResult[]>; | ||
export declare function validateMapSync<T>(obj: T, map: ValidateMap<T> | ValidateMapCompact, showOnlyInvalid?: boolean): IValidationResult[]; | ||
export declare function validateMapAsync<T>(obj: T, map: ValidateMap<T> | ValidateMapCompact, showOnlyInvalid?: boolean): Promise<IValidationResult[]>; |
@@ -13,53 +13,43 @@ "use strict"; | ||
const pathof_1 = require("@berish/pathof"); | ||
const getRulesFromMap_1 = require("./getRulesFromMap"); | ||
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 rulesWithPaths = getRulesFromMap_1.getRulesFromMap(map); | ||
if (rulesWithPaths.length <= 0) | ||
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 pathResult = pathof_1.of(obj); | ||
const data = rulesWithPaths.map(([path, rules]) => { | ||
const subPathResult = path.reduce((of, key) => of(key), pathResult); | ||
const results = rules | ||
.map(rule => { | ||
if (!rule) | ||
return null; | ||
const key = subPathResult.path.length === 0 ? null : subPathResult.path[subPathResult.path.length - 1]; | ||
const value = subPathResult.get(); | ||
const executeResult = executeRule_1.executeRuleSync(rule, { target: obj, key, value }); | ||
return { | ||
name: rule.ruleName, | ||
isValid: executeResult === flags_1.FLAG_CONDITION_TRUTHY, | ||
errorText: executeResult === flags_1.FLAG_CONDITION_TRUTHY ? null : executeResult, | ||
}; | ||
}) | ||
.filter(m => !!m); | ||
return { key: subPathResult.path, rules: results }; | ||
}); | ||
return data.filter(m => { | ||
if (showOnlyInvalid) { | ||
m.rules = m.rules.filter(m => !m.isValid); | ||
if (m.rules.length <= 0) | ||
return false; | ||
} | ||
return true; | ||
}); | ||
} | ||
exports.validateMapSync = validateMapSync; | ||
function validateMapAsync(obj, map, showOnlyInvalid) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const rulesWithPaths = getRulesFromMap_1.getRulesFromMap(map); | ||
if (rulesWithPaths.length <= 0) | ||
return []; | ||
const pathResult = pathof_1.of(obj); | ||
const data = yield Promise.all(rulesWithPaths.map(([path, rules]) => __awaiter(this, void 0, void 0, function* () { | ||
const subPathResult = path.reduce((of, key) => of(key), pathResult); | ||
const results = yield Promise.all(rules | ||
.map((rule) => __awaiter(this, void 0, void 0, function* () { | ||
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 key = subPathResult.path.length === 0 ? null : subPathResult.path[subPathResult.path.length - 1]; | ||
const value = subPathResult.get(); | ||
const executeResult = yield executeRule_1.executeRuleAsync(rule, { target: obj, key, value }); | ||
const executeResult = executeRule_1.executeRuleSync(rule, { target: obj, key: dataKey, value: dataValue }); | ||
return { | ||
@@ -70,17 +60,131 @@ name: rule.ruleName, | ||
}; | ||
})) | ||
.filter(m => !!m)); | ||
return { key: subPathResult.path, rules: results }; | ||
}))); | ||
return data.filter(m => { | ||
if (showOnlyInvalid) { | ||
m.rules = m.rules.filter(m => !m.isValid); | ||
if (m.rules.length <= 0) | ||
return false; | ||
}) | ||
.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 true; | ||
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 |
{ | ||
"name": "@berish/validate", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "Validation of complex objects with support for validation maps, rules and decorators", | ||
@@ -54,3 +54,3 @@ "main": "build/index.js", | ||
"dependencies": { | ||
"@berish/linq": "^1.0.5", | ||
"@berish/linq": "^1.1.0", | ||
"@berish/pathof": "^1.1.0", | ||
@@ -57,0 +57,0 @@ "berish-guid": "^2.0.0" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
77943
86
948
Updated@berish/linq@^1.1.0