@uform/validator
Advanced tools
Comparing version 1.0.0-alpha.4 to 1.0.0-alpha.5
@@ -105,3 +105,3 @@ "use strict"; | ||
var length = getLength(value); | ||
var min = Number(rule.len); | ||
var min = Number(rule.min); | ||
return length < min ? getRuleMessage(rule, 'min') : ''; | ||
@@ -108,0 +108,0 @@ }, |
@@ -0,3 +1,5 @@ | ||
import { FormPathPattern } from '@uform/shared'; | ||
export interface ValidatorOptions { | ||
validateFirst?: boolean; | ||
matchStrategy?: (pattern: FormPathPattern, field: any) => boolean; | ||
} | ||
@@ -4,0 +6,0 @@ export declare type ValidateNode = (options: ValidateFieldOptions) => Promise<{ |
@@ -6,2 +6,3 @@ import { ValidatorOptions, ValidatePatternRules, ValidateRules, ValidateFormatsMap, ValidateRulesMap, ValidateResponse, ValidateDescription, ValidateFieldOptions, ValidateCalculator, ValidateNodeResult } from './types'; | ||
private nodes; | ||
private matchStrategy; | ||
constructor(options?: ValidatorOptions); | ||
@@ -8,0 +9,0 @@ transformRules(rules: ValidatePatternRules): any; |
@@ -87,2 +87,3 @@ "use strict"; | ||
return new Promise(function (resolve, reject) { | ||
var tmpResult; | ||
var validate = function (value, rules) { return __awaiter(_this, void 0, void 0, function () { | ||
@@ -93,6 +94,6 @@ var data; | ||
return [2, this.internalValidate(value, this.transformRules(rules), data).then(function (payload) { | ||
resolve(payload); | ||
tmpResult = payload; | ||
return payload; | ||
}, function (payload) { | ||
reject(payload); | ||
tmpResult = payload; | ||
return Promise.reject(payload); | ||
@@ -102,3 +103,7 @@ })]; | ||
}); }; | ||
calculator(validate); | ||
Promise.resolve(calculator(validate)).then(function () { | ||
resolve(tmpResult); | ||
}, function () { | ||
reject(tmpResult); | ||
}); | ||
}); | ||
@@ -112,2 +117,3 @@ }; | ||
this.validateFirst = options.validateFirst; | ||
this.matchStrategy = options.matchStrategy; | ||
this.nodes = {}; | ||
@@ -242,3 +248,5 @@ } | ||
shared_1.each(this.nodes, function (validator, path) { | ||
if (pattern.match(path)) { | ||
if (shared_1.isFn(_this.matchStrategy) | ||
? _this.matchStrategy(pattern, path) | ||
: pattern.match(path)) { | ||
promise = promise.then(function (_a) { | ||
@@ -245,0 +253,0 @@ var errors = _a.errors, warnings = _a.warnings; |
{ | ||
"name": "@uform/validator", | ||
"version": "1.0.0-alpha.4", | ||
"version": "1.0.0-alpha.5", | ||
"license": "MIT", | ||
@@ -28,3 +28,3 @@ "main": "lib", | ||
"dependencies": { | ||
"@uform/shared": "^1.0.0-alpha.4", | ||
"@uform/shared": "^1.0.0-alpha.5", | ||
"@uform/types": "^0.4.0" | ||
@@ -35,3 +35,3 @@ }, | ||
}, | ||
"gitHead": "2353a4274039c562194eb19829345e13ce1cfb95" | ||
"gitHead": "7bf702d14e0e554872392beba93d8844c2e28dc9" | ||
} |
@@ -73,3 +73,3 @@ import { getMessage } from './message' | ||
const length = getLength(value) | ||
const min = Number(rule.len) | ||
const min = Number(rule.min) | ||
return length < min ? getRuleMessage(rule, 'min') : '' | ||
@@ -76,0 +76,0 @@ }, |
@@ -0,3 +1,6 @@ | ||
import { FormPathPattern } from '@uform/shared' | ||
export interface ValidatorOptions { | ||
validateFirst?: boolean | ||
matchStrategy?: (pattern: FormPathPattern, field: any) => boolean | ||
} | ||
@@ -4,0 +7,0 @@ |
@@ -54,5 +54,7 @@ import { | ||
private nodes: ValidateNodeMap | ||
private matchStrategy: ValidatorOptions['matchStrategy'] | ||
constructor(options: ValidatorOptions = {}) { | ||
this.validateFirst = options.validateFirst | ||
this.matchStrategy = options.matchStrategy | ||
this.nodes = {} | ||
@@ -168,3 +170,7 @@ } | ||
each<ValidateNodeMap, ValidateNode>(this.nodes, (validator, path) => { | ||
if (pattern.match(path)) { | ||
if ( | ||
isFn(this.matchStrategy) | ||
? this.matchStrategy(pattern, path) | ||
: pattern.match(path) | ||
) { | ||
promise = promise.then(async ({ errors, warnings }) => { | ||
@@ -210,2 +216,3 @@ const result = await validator(options) | ||
return new Promise((resolve, reject) => { | ||
let tmpResult: any | ||
const validate = async (value: any, rules: ValidatePatternRules) => { | ||
@@ -222,7 +229,7 @@ const data = { | ||
payload => { | ||
resolve(payload) | ||
tmpResult = payload | ||
return payload | ||
}, | ||
payload => { | ||
reject(payload) | ||
tmpResult = payload | ||
return Promise.reject(payload) | ||
@@ -232,3 +239,10 @@ } | ||
} | ||
calculator(validate) | ||
Promise.resolve(calculator(validate)).then( | ||
() => { | ||
resolve(tmpResult) | ||
}, | ||
() => { | ||
reject(tmpResult) | ||
} | ||
) | ||
}) | ||
@@ -235,0 +249,0 @@ } |
57579
1453
Updated@uform/shared@^1.0.0-alpha.5