ddan-js 1.2.3 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/ddan-js.esm.js +1 -1
- package/bin/ddan-js.js +1 -1
- package/bin/lib/modules/rule/async-validator/index.js +300 -0
- package/bin/lib/modules/rule/async-validator/interface.js +2 -0
- package/bin/lib/modules/rule/async-validator/messages.js +59 -0
- package/bin/lib/modules/rule/async-validator/rule/enum.js +13 -0
- package/bin/lib/modules/rule/async-validator/rule/index.js +16 -0
- package/bin/lib/modules/rule/async-validator/rule/pattern.js +27 -0
- package/bin/lib/modules/rule/async-validator/rule/range.js +54 -0
- package/bin/lib/modules/rule/async-validator/rule/required.js +12 -0
- package/bin/lib/modules/rule/async-validator/rule/type.js +98 -0
- package/bin/lib/modules/rule/async-validator/rule/url.js +55 -0
- package/bin/lib/modules/rule/async-validator/rule/whitespace.js +20 -0
- package/bin/lib/modules/rule/async-validator/util.js +250 -0
- package/bin/lib/modules/rule/async-validator/validator/any.js +16 -0
- package/bin/lib/modules/rule/async-validator/validator/array.js +19 -0
- package/bin/lib/modules/rule/async-validator/validator/boolean.js +20 -0
- package/bin/lib/modules/rule/async-validator/validator/date.js +31 -0
- package/bin/lib/modules/rule/async-validator/validator/enum.js +20 -0
- package/bin/lib/modules/rule/async-validator/validator/float.js +20 -0
- package/bin/lib/modules/rule/async-validator/validator/index.js +36 -0
- package/bin/lib/modules/rule/async-validator/validator/integer.js +20 -0
- package/bin/lib/modules/rule/async-validator/validator/method.js +19 -0
- package/bin/lib/modules/rule/async-validator/validator/number.js +23 -0
- package/bin/lib/modules/rule/async-validator/validator/object.js +19 -0
- package/bin/lib/modules/rule/async-validator/validator/pattern.js +19 -0
- package/bin/lib/modules/rule/async-validator/validator/regexp.js +19 -0
- package/bin/lib/modules/rule/async-validator/validator/required.js +10 -0
- package/bin/lib/modules/rule/async-validator/validator/string.js +24 -0
- package/bin/lib/modules/rule/async-validator/validator/type.js +20 -0
- package/bin/lib/modules/rule/index.js +9 -31
- package/bin/lib/modules/url/index.js +12 -1
- package/bin/lib/util/index.js +30 -1
- package/bin/types/index.d.ts +5 -0
- package/bin/types/modules/rule/async-validator/index.d.ts +43 -0
- package/bin/types/modules/rule/async-validator/interface.d.ts +135 -0
- package/bin/types/modules/rule/async-validator/messages.d.ts +3 -0
- package/bin/types/modules/rule/async-validator/rule/enum.d.ts +3 -0
- package/bin/types/modules/rule/async-validator/rule/index.d.ts +9 -0
- package/bin/types/modules/rule/async-validator/rule/pattern.d.ts +3 -0
- package/bin/types/modules/rule/async-validator/rule/range.d.ts +3 -0
- package/bin/types/modules/rule/async-validator/rule/required.d.ts +3 -0
- package/bin/types/modules/rule/async-validator/rule/type.d.ts +3 -0
- package/bin/types/modules/rule/async-validator/rule/url.d.ts +2 -0
- package/bin/types/modules/rule/async-validator/rule/whitespace.d.ts +14 -0
- package/bin/types/modules/rule/async-validator/util.d.ts +16 -0
- package/bin/types/modules/rule/async-validator/validator/any.d.ts +3 -0
- package/bin/types/modules/rule/async-validator/validator/array.d.ts +3 -0
- package/bin/types/modules/rule/async-validator/validator/boolean.d.ts +3 -0
- package/bin/types/modules/rule/async-validator/validator/date.d.ts +3 -0
- package/bin/types/modules/rule/async-validator/validator/enum.d.ts +3 -0
- package/bin/types/modules/rule/async-validator/validator/float.d.ts +3 -0
- package/bin/types/modules/rule/async-validator/validator/index.d.ts +20 -0
- package/bin/types/modules/rule/async-validator/validator/integer.d.ts +3 -0
- package/bin/types/modules/rule/async-validator/validator/method.d.ts +3 -0
- package/bin/types/modules/rule/async-validator/validator/number.d.ts +3 -0
- package/bin/types/modules/rule/async-validator/validator/object.d.ts +3 -0
- package/bin/types/modules/rule/async-validator/validator/pattern.d.ts +3 -0
- package/bin/types/modules/rule/async-validator/validator/regexp.d.ts +3 -0
- package/bin/types/modules/rule/async-validator/validator/required.d.ts +3 -0
- package/bin/types/modules/rule/async-validator/validator/string.d.ts +3 -0
- package/bin/types/modules/rule/async-validator/validator/type.d.ts +3 -0
- package/bin/types/modules/rule/index.d.ts +5 -0
- package/bin/types/modules/url/index.d.ts +2 -0
- package/bin/types/util/index.d.ts +4 -0
- package/package.json +1 -1
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const rule_1 = require("../rule");
|
|
4
|
+
const util_1 = require("../util");
|
|
5
|
+
const method = (rule, value, callback, source, options) => {
|
|
6
|
+
const errors = [];
|
|
7
|
+
const validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field));
|
|
8
|
+
if (validate) {
|
|
9
|
+
if ((0, util_1.isEmptyValue)(value) && !rule.required) {
|
|
10
|
+
return callback();
|
|
11
|
+
}
|
|
12
|
+
rule_1.default.required(rule, value, source, errors, options);
|
|
13
|
+
if (value !== undefined) {
|
|
14
|
+
rule_1.default.type(rule, value, source, errors, options);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
callback(errors);
|
|
18
|
+
};
|
|
19
|
+
exports.default = method;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const rule_1 = require("../rule");
|
|
4
|
+
const util_1 = require("../util");
|
|
5
|
+
const number = (rule, value, callback, source, options) => {
|
|
6
|
+
const errors = [];
|
|
7
|
+
const validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field));
|
|
8
|
+
if (validate) {
|
|
9
|
+
if (value === '') {
|
|
10
|
+
value = undefined;
|
|
11
|
+
}
|
|
12
|
+
if ((0, util_1.isEmptyValue)(value) && !rule.required) {
|
|
13
|
+
return callback();
|
|
14
|
+
}
|
|
15
|
+
rule_1.default.required(rule, value, source, errors, options);
|
|
16
|
+
if (value !== undefined) {
|
|
17
|
+
rule_1.default.type(rule, value, source, errors, options);
|
|
18
|
+
rule_1.default.range(rule, value, source, errors, options);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
callback(errors);
|
|
22
|
+
};
|
|
23
|
+
exports.default = number;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const rule_1 = require("../rule");
|
|
4
|
+
const util_1 = require("../util");
|
|
5
|
+
const object = (rule, value, callback, source, options) => {
|
|
6
|
+
const errors = [];
|
|
7
|
+
const validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field));
|
|
8
|
+
if (validate) {
|
|
9
|
+
if ((0, util_1.isEmptyValue)(value) && !rule.required) {
|
|
10
|
+
return callback();
|
|
11
|
+
}
|
|
12
|
+
rule_1.default.required(rule, value, source, errors, options);
|
|
13
|
+
if (value !== undefined) {
|
|
14
|
+
rule_1.default.type(rule, value, source, errors, options);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
callback(errors);
|
|
18
|
+
};
|
|
19
|
+
exports.default = object;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const rule_1 = require("../rule");
|
|
4
|
+
const util_1 = require("../util");
|
|
5
|
+
const pattern = (rule, value, callback, source, options) => {
|
|
6
|
+
const errors = [];
|
|
7
|
+
const validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field));
|
|
8
|
+
if (validate) {
|
|
9
|
+
if ((0, util_1.isEmptyValue)(value, 'string') && !rule.required) {
|
|
10
|
+
return callback();
|
|
11
|
+
}
|
|
12
|
+
rule_1.default.required(rule, value, source, errors, options);
|
|
13
|
+
if (!(0, util_1.isEmptyValue)(value, 'string')) {
|
|
14
|
+
rule_1.default.pattern(rule, value, source, errors, options);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
callback(errors);
|
|
18
|
+
};
|
|
19
|
+
exports.default = pattern;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const rule_1 = require("../rule");
|
|
4
|
+
const util_1 = require("../util");
|
|
5
|
+
const regexp = (rule, value, callback, source, options) => {
|
|
6
|
+
const errors = [];
|
|
7
|
+
const validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field));
|
|
8
|
+
if (validate) {
|
|
9
|
+
if ((0, util_1.isEmptyValue)(value) && !rule.required) {
|
|
10
|
+
return callback();
|
|
11
|
+
}
|
|
12
|
+
rule_1.default.required(rule, value, source, errors, options);
|
|
13
|
+
if (!(0, util_1.isEmptyValue)(value)) {
|
|
14
|
+
rule_1.default.type(rule, value, source, errors, options);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
callback(errors);
|
|
18
|
+
};
|
|
19
|
+
exports.default = regexp;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const rule_1 = require("../rule");
|
|
4
|
+
const required = (rule, value, callback, source, options) => {
|
|
5
|
+
const errors = [];
|
|
6
|
+
const type = Array.isArray(value) ? 'array' : typeof value;
|
|
7
|
+
rule_1.default.required(rule, value, source, errors, options, type);
|
|
8
|
+
callback(errors);
|
|
9
|
+
};
|
|
10
|
+
exports.default = required;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const rule_1 = require("../rule");
|
|
4
|
+
const util_1 = require("../util");
|
|
5
|
+
const string = (rule, value, callback, source, options) => {
|
|
6
|
+
const errors = [];
|
|
7
|
+
const validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field || ""));
|
|
8
|
+
if (validate) {
|
|
9
|
+
if ((0, util_1.isEmptyValue)(value, 'string') && !rule.required) {
|
|
10
|
+
return callback();
|
|
11
|
+
}
|
|
12
|
+
rule_1.default.required(rule, value, source, errors, options, 'string');
|
|
13
|
+
if (!(0, util_1.isEmptyValue)(value, 'string')) {
|
|
14
|
+
rule_1.default.type(rule, value, source, errors, options);
|
|
15
|
+
rule_1.default.range(rule, value, source, errors, options);
|
|
16
|
+
rule_1.default.pattern(rule, value, source, errors, options);
|
|
17
|
+
if (rule.whitespace === true) {
|
|
18
|
+
rule_1.default.whitespace(rule, value, source, errors, options);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
callback(errors);
|
|
23
|
+
};
|
|
24
|
+
exports.default = string;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const rule_1 = require("../rule");
|
|
4
|
+
const util_1 = require("../util");
|
|
5
|
+
const type = (rule, value, callback, source, options) => {
|
|
6
|
+
const ruleType = rule.type;
|
|
7
|
+
const errors = [];
|
|
8
|
+
const validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field));
|
|
9
|
+
if (validate) {
|
|
10
|
+
if ((0, util_1.isEmptyValue)(value, ruleType) && !rule.required) {
|
|
11
|
+
return callback();
|
|
12
|
+
}
|
|
13
|
+
rule_1.default.required(rule, value, source, errors, options, ruleType);
|
|
14
|
+
if (!(0, util_1.isEmptyValue)(value, ruleType)) {
|
|
15
|
+
rule_1.default.type(rule, value, source, errors, options);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
callback(errors);
|
|
19
|
+
};
|
|
20
|
+
exports.default = type;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const hook_1 = require("../hook");
|
|
4
|
+
const async_validator_1 = require("./async-validator");
|
|
3
5
|
function validateId(content) {
|
|
4
6
|
if (!content)
|
|
5
7
|
return false;
|
|
@@ -20,34 +22,10 @@ function validatePhone(content) {
|
|
|
20
22
|
}
|
|
21
23
|
return true;
|
|
22
24
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
// invalid = val.length <= 0 || !val.every((e) => !!e);
|
|
31
|
-
// }
|
|
32
|
-
// if (Array.isArray(rule.rules)) {
|
|
33
|
-
// for (const item of rule.rules) {
|
|
34
|
-
// if (invalid && item.required) {
|
|
35
|
-
// cb && cb(rule, item);
|
|
36
|
-
// return false;
|
|
37
|
-
// }
|
|
38
|
-
// }
|
|
39
|
-
// } else {
|
|
40
|
-
// if (invalid && rule.rules.required) {
|
|
41
|
-
// cb && cb(rule, rule.rules);
|
|
42
|
-
// return false;
|
|
43
|
-
// } else if (rule.rules.validator) {
|
|
44
|
-
// if (!rule.rules.validator(rule, val)) {
|
|
45
|
-
// cb && cb(rule, rule.rules);
|
|
46
|
-
// return false;
|
|
47
|
-
// }
|
|
48
|
-
// }
|
|
49
|
-
// }
|
|
50
|
-
// }
|
|
51
|
-
// return true;
|
|
52
|
-
// }
|
|
53
|
-
exports.default = { validateId, validatePhone };
|
|
25
|
+
function validator(rules) {
|
|
26
|
+
return new async_validator_1.default(rules);
|
|
27
|
+
}
|
|
28
|
+
function validate(rules, source_, o = {}, oc = () => { }) {
|
|
29
|
+
return hook_1.default.run(validator(rules).validate(source_, o, oc));
|
|
30
|
+
}
|
|
31
|
+
exports.default = { validateId, validatePhone, validator, validate };
|
|
@@ -2,4 +2,15 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const decode_1 = require("./decode");
|
|
4
4
|
const encode_1 = require("./encode");
|
|
5
|
-
|
|
5
|
+
function useHttps(url) {
|
|
6
|
+
if (!url)
|
|
7
|
+
return url;
|
|
8
|
+
const tUrl = url.trim();
|
|
9
|
+
if (typeof url !== "string")
|
|
10
|
+
return url;
|
|
11
|
+
if (tUrl.indexOf("https") < 0) {
|
|
12
|
+
return tUrl.replace("http", "https");
|
|
13
|
+
}
|
|
14
|
+
return tUrl;
|
|
15
|
+
}
|
|
16
|
+
exports.default = { parse: decode_1.default, decode: decode_1.default, stringify: encode_1.default, encode: encode_1.default, useHttps };
|
package/bin/lib/util/index.js
CHANGED
|
@@ -3,4 +3,33 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const is_1 = require("./is");
|
|
4
4
|
const _object_1 = require("../common/_object");
|
|
5
5
|
const includes_1 = require("./includes");
|
|
6
|
-
|
|
6
|
+
function compareArray(arr1, arr2) {
|
|
7
|
+
if (!Array.isArray(arr1) || !Array.isArray(arr2))
|
|
8
|
+
return false;
|
|
9
|
+
if (arr1.length !== arr2.length)
|
|
10
|
+
return false;
|
|
11
|
+
for (let i = 0, len = arr1.length; i < len; i++) {
|
|
12
|
+
if (arr1[i] !== arr2[i])
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
function merge(target, source) {
|
|
18
|
+
if (!source)
|
|
19
|
+
return target;
|
|
20
|
+
if (!target)
|
|
21
|
+
return source;
|
|
22
|
+
Object.keys(source).forEach((key) => {
|
|
23
|
+
if (Array.isArray(target[key]) && Array.isArray(source[key])) {
|
|
24
|
+
target[key] = target[key].concat(source[key]);
|
|
25
|
+
}
|
|
26
|
+
else if (typeof target[key] === "object" && typeof source[key] === "object") {
|
|
27
|
+
target[key] = Object.assign(target[key], source[key]);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
target[key] = source[key];
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
return target;
|
|
34
|
+
}
|
|
35
|
+
exports.default = Object.assign(Object.assign(Object.assign({}, is_1.default), _object_1.default), { includes: includes_1.default, merge, compareArray });
|
package/bin/types/index.d.ts
CHANGED
|
@@ -37,6 +37,8 @@ declare const _default: {
|
|
|
37
37
|
};
|
|
38
38
|
util: {
|
|
39
39
|
includes: typeof import("./util/includes").default;
|
|
40
|
+
merge: (target: any, source: any) => any;
|
|
41
|
+
compareArray: (arr1: any, arr2: any) => boolean;
|
|
40
42
|
getTag: (value: any) => string;
|
|
41
43
|
getType: (value: any) => string;
|
|
42
44
|
toString: () => string;
|
|
@@ -96,10 +98,13 @@ declare const _default: {
|
|
|
96
98
|
name?: null | undefined;
|
|
97
99
|
multiple?: boolean | undefined;
|
|
98
100
|
}) => string;
|
|
101
|
+
useHttps: (url: string) => string;
|
|
99
102
|
};
|
|
100
103
|
rule: {
|
|
101
104
|
validateId: (content: any) => boolean;
|
|
102
105
|
validatePhone: (content: any) => boolean;
|
|
106
|
+
validator: (rules: import("./modules/rule/async-validator").Rules) => import("./modules/rule/async-validator").default;
|
|
107
|
+
validate: (rules: import("./modules/rule/async-validator").Rules, source_: import("./modules/rule/async-validator").Values, o?: any, oc?: any) => Promise<import("./modules/rule/async-validator").Values>;
|
|
103
108
|
};
|
|
104
109
|
DEvent: typeof DEvent;
|
|
105
110
|
store: typeof store;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { InternalRuleItem, InternalValidateMessages, RuleItem, Rules, ValidateCallback, ValidateMessages, ValidateOption, Values, SyncErrorType } from './interface';
|
|
2
|
+
export * from './interface';
|
|
3
|
+
/**
|
|
4
|
+
* Encapsulates a validation schema.
|
|
5
|
+
*
|
|
6
|
+
* @param descriptor An object declaring validation rules
|
|
7
|
+
* for this schema.
|
|
8
|
+
*/
|
|
9
|
+
declare class Schema {
|
|
10
|
+
static register: (type: string, validator: any) => void;
|
|
11
|
+
static warning: (type: string, errors: SyncErrorType[]) => void;
|
|
12
|
+
static messages: InternalValidateMessages;
|
|
13
|
+
static validators: {
|
|
14
|
+
string: import("./interface").ExecuteValidator;
|
|
15
|
+
method: import("./interface").ExecuteValidator;
|
|
16
|
+
number: import("./interface").ExecuteValidator;
|
|
17
|
+
boolean: import("./interface").ExecuteValidator;
|
|
18
|
+
regexp: import("./interface").ExecuteValidator;
|
|
19
|
+
integer: import("./interface").ExecuteValidator;
|
|
20
|
+
float: import("./interface").ExecuteValidator;
|
|
21
|
+
array: import("./interface").ExecuteValidator;
|
|
22
|
+
object: import("./interface").ExecuteValidator;
|
|
23
|
+
enum: import("./interface").ExecuteValidator;
|
|
24
|
+
pattern: import("./interface").ExecuteValidator;
|
|
25
|
+
date: import("./interface").ExecuteValidator;
|
|
26
|
+
url: import("./interface").ExecuteValidator;
|
|
27
|
+
hex: import("./interface").ExecuteValidator;
|
|
28
|
+
email: import("./interface").ExecuteValidator;
|
|
29
|
+
required: import("./interface").ExecuteValidator;
|
|
30
|
+
any: import("./interface").ExecuteValidator;
|
|
31
|
+
};
|
|
32
|
+
rules: Record<string, RuleItem[]>;
|
|
33
|
+
_messages: InternalValidateMessages;
|
|
34
|
+
constructor(descriptor: Rules);
|
|
35
|
+
define(rules: Rules): void;
|
|
36
|
+
messages(messages?: ValidateMessages): InternalValidateMessages;
|
|
37
|
+
validate(source: Values, option?: ValidateOption, callback?: ValidateCallback): Promise<Values>;
|
|
38
|
+
validate(source: Values, callback: ValidateCallback): Promise<Values>;
|
|
39
|
+
validate(source: Values): Promise<Values>;
|
|
40
|
+
getType(rule: InternalRuleItem): import("./interface").RuleType;
|
|
41
|
+
getValidationMethod(rule: InternalRuleItem): import("./interface").ExecuteValidator | ((rule: InternalRuleItem, value: any, callback: (error?: string | Error | undefined) => void, source: Values, options: ValidateOption) => void | import("./interface").SyncValidateResult);
|
|
42
|
+
}
|
|
43
|
+
export default Schema;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
export type RuleType = 'string' | 'number' | 'boolean' | 'method' | 'regexp' | 'integer' | 'float' | 'array' | 'object' | 'enum' | 'date' | 'url' | 'hex' | 'email' | 'pattern' | 'any';
|
|
2
|
+
export interface ValidateOption {
|
|
3
|
+
suppressWarning?: boolean;
|
|
4
|
+
suppressValidatorError?: boolean;
|
|
5
|
+
first?: boolean;
|
|
6
|
+
firstFields?: boolean | string[];
|
|
7
|
+
messages?: Partial<ValidateMessages>;
|
|
8
|
+
/** The name of rules need to be trigger. Will validate all rules if leave empty */
|
|
9
|
+
keys?: string[];
|
|
10
|
+
error?: (rule: InternalRuleItem, message: string) => ValidateError;
|
|
11
|
+
}
|
|
12
|
+
export type SyncErrorType = Error | string;
|
|
13
|
+
export type SyncValidateResult = boolean | SyncErrorType | SyncErrorType[];
|
|
14
|
+
export type ValidateResult = void | Promise<void> | SyncValidateResult;
|
|
15
|
+
export interface RuleItem {
|
|
16
|
+
type?: RuleType;
|
|
17
|
+
required?: boolean;
|
|
18
|
+
pattern?: RegExp | string;
|
|
19
|
+
min?: number;
|
|
20
|
+
max?: number;
|
|
21
|
+
len?: number;
|
|
22
|
+
enum?: Array<string | number | boolean | null | undefined>;
|
|
23
|
+
whitespace?: boolean;
|
|
24
|
+
fields?: Record<string, Rule>;
|
|
25
|
+
options?: ValidateOption;
|
|
26
|
+
defaultField?: Rule;
|
|
27
|
+
transform?: (value: Value) => Value;
|
|
28
|
+
message?: string | ((a?: string) => string);
|
|
29
|
+
asyncValidator?: (rule: InternalRuleItem, value: Value, callback: (error?: string | Error) => void, source: Values, options: ValidateOption) => void | Promise<void>;
|
|
30
|
+
validator?: (rule: InternalRuleItem, value: Value, callback: (error?: string | Error) => void, source: Values, options: ValidateOption) => SyncValidateResult | void;
|
|
31
|
+
}
|
|
32
|
+
export type Rule = RuleItem | RuleItem[];
|
|
33
|
+
export type Rules = Record<string, Rule>;
|
|
34
|
+
/**
|
|
35
|
+
* Rule for validating a value exists in an enumerable list.
|
|
36
|
+
*
|
|
37
|
+
* @param rule The validation rule.
|
|
38
|
+
* @param value The value of the field on the source object.
|
|
39
|
+
* @param source The source object being validated.
|
|
40
|
+
* @param errors An array of errors that this rule may add
|
|
41
|
+
* validation errors to.
|
|
42
|
+
* @param options The validation options.
|
|
43
|
+
* @param options.messages The validation messages.
|
|
44
|
+
* @param type Rule type
|
|
45
|
+
*/
|
|
46
|
+
export type ExecuteRule = (rule: InternalRuleItem, value: Value, source: Values, errors: string[], options: ValidateOption, type?: string) => void;
|
|
47
|
+
/**
|
|
48
|
+
* Performs validation for any type.
|
|
49
|
+
*
|
|
50
|
+
* @param rule The validation rule.
|
|
51
|
+
* @param value The value of the field on the source object.
|
|
52
|
+
* @param callback The callback function.
|
|
53
|
+
* @param source The source object being validated.
|
|
54
|
+
* @param options The validation options.
|
|
55
|
+
* @param options.messages The validation messages.
|
|
56
|
+
*/
|
|
57
|
+
export type ExecuteValidator = (rule: InternalRuleItem, value: Value, callback: (error?: string[]) => void, source: Values, options: ValidateOption) => void;
|
|
58
|
+
type ValidateMessage<T extends any[] = unknown[]> = string | ((...args: T) => string);
|
|
59
|
+
type FullField = string | undefined;
|
|
60
|
+
type EnumString = string | undefined;
|
|
61
|
+
type Pattern = string | RegExp | undefined;
|
|
62
|
+
type Range = number | undefined;
|
|
63
|
+
type Type = string | undefined;
|
|
64
|
+
export interface ValidateMessages {
|
|
65
|
+
default?: ValidateMessage;
|
|
66
|
+
required?: ValidateMessage<[FullField]>;
|
|
67
|
+
enum?: ValidateMessage<[FullField, EnumString]>;
|
|
68
|
+
whitespace?: ValidateMessage<[FullField]>;
|
|
69
|
+
date?: {
|
|
70
|
+
format?: ValidateMessage;
|
|
71
|
+
parse?: ValidateMessage;
|
|
72
|
+
invalid?: ValidateMessage;
|
|
73
|
+
};
|
|
74
|
+
types?: {
|
|
75
|
+
string?: ValidateMessage<[FullField, Type]>;
|
|
76
|
+
method?: ValidateMessage<[FullField, Type]>;
|
|
77
|
+
array?: ValidateMessage<[FullField, Type]>;
|
|
78
|
+
object?: ValidateMessage<[FullField, Type]>;
|
|
79
|
+
number?: ValidateMessage<[FullField, Type]>;
|
|
80
|
+
date?: ValidateMessage<[FullField, Type]>;
|
|
81
|
+
boolean?: ValidateMessage<[FullField, Type]>;
|
|
82
|
+
integer?: ValidateMessage<[FullField, Type]>;
|
|
83
|
+
float?: ValidateMessage<[FullField, Type]>;
|
|
84
|
+
regexp?: ValidateMessage<[FullField, Type]>;
|
|
85
|
+
email?: ValidateMessage<[FullField, Type]>;
|
|
86
|
+
url?: ValidateMessage<[FullField, Type]>;
|
|
87
|
+
hex?: ValidateMessage<[FullField, Type]>;
|
|
88
|
+
};
|
|
89
|
+
string?: {
|
|
90
|
+
len?: ValidateMessage<[FullField, Range]>;
|
|
91
|
+
min?: ValidateMessage<[FullField, Range]>;
|
|
92
|
+
max?: ValidateMessage<[FullField, Range]>;
|
|
93
|
+
range?: ValidateMessage<[FullField, Range, Range]>;
|
|
94
|
+
};
|
|
95
|
+
number?: {
|
|
96
|
+
len?: ValidateMessage<[FullField, Range]>;
|
|
97
|
+
min?: ValidateMessage<[FullField, Range]>;
|
|
98
|
+
max?: ValidateMessage<[FullField, Range]>;
|
|
99
|
+
range?: ValidateMessage<[FullField, Range, Range]>;
|
|
100
|
+
};
|
|
101
|
+
array?: {
|
|
102
|
+
len?: ValidateMessage<[FullField, Range]>;
|
|
103
|
+
min?: ValidateMessage<[FullField, Range]>;
|
|
104
|
+
max?: ValidateMessage<[FullField, Range]>;
|
|
105
|
+
range?: ValidateMessage<[FullField, Range, Range]>;
|
|
106
|
+
};
|
|
107
|
+
pattern?: {
|
|
108
|
+
mismatch?: ValidateMessage<[FullField, Value, Pattern]>;
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
export interface InternalValidateMessages extends ValidateMessages {
|
|
112
|
+
clone: () => InternalValidateMessages;
|
|
113
|
+
}
|
|
114
|
+
export type Value = any;
|
|
115
|
+
export type Values = Record<string, Value>;
|
|
116
|
+
export interface ValidateError {
|
|
117
|
+
message?: string;
|
|
118
|
+
fieldValue?: Value;
|
|
119
|
+
field?: string;
|
|
120
|
+
}
|
|
121
|
+
export type ValidateFieldsError = Record<string, ValidateError[]>;
|
|
122
|
+
export type ValidateCallback = (errors: ValidateError[] | null, fields: ValidateFieldsError | Values) => void;
|
|
123
|
+
export interface RuleValuePackage {
|
|
124
|
+
rule: InternalRuleItem;
|
|
125
|
+
value: Value;
|
|
126
|
+
source: Values;
|
|
127
|
+
field: string;
|
|
128
|
+
}
|
|
129
|
+
export interface InternalRuleItem extends Omit<RuleItem, 'validator'> {
|
|
130
|
+
field?: string;
|
|
131
|
+
fullField?: string;
|
|
132
|
+
fullFields?: string[];
|
|
133
|
+
validator?: RuleItem['validator'] | ExecuteValidator;
|
|
134
|
+
}
|
|
135
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
declare const _default: {
|
|
2
|
+
required: import("..").ExecuteRule;
|
|
3
|
+
whitespace: import("..").ExecuteRule;
|
|
4
|
+
type: import("..").ExecuteRule;
|
|
5
|
+
range: import("..").ExecuteRule;
|
|
6
|
+
enum: import("..").ExecuteRule;
|
|
7
|
+
pattern: import("..").ExecuteRule;
|
|
8
|
+
};
|
|
9
|
+
export default _default;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ExecuteRule } from '../interface';
|
|
2
|
+
/**
|
|
3
|
+
* Rule for validating whitespace.
|
|
4
|
+
*
|
|
5
|
+
* @param rule The validation rule.
|
|
6
|
+
* @param value The value of the field on the source object.
|
|
7
|
+
* @param source The source object being validated.
|
|
8
|
+
* @param errors An array of errors that this rule may add
|
|
9
|
+
* validation errors to.
|
|
10
|
+
* @param options The validation options.
|
|
11
|
+
* @param options.messages The validation messages.
|
|
12
|
+
*/
|
|
13
|
+
declare const whitespace: ExecuteRule;
|
|
14
|
+
export default whitespace;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ValidateError, ValidateOption, RuleValuePackage, InternalRuleItem, SyncErrorType, Value, Values } from './interface';
|
|
2
|
+
export declare let warning: (type: string, errors: SyncErrorType[]) => void;
|
|
3
|
+
export declare function convertFieldsError(errors: ValidateError[]): Record<string, ValidateError[]> | null;
|
|
4
|
+
export declare function format(template: ((...args: any[]) => string) | string, ...args: any[]): string;
|
|
5
|
+
export declare function isEmptyValue(value: Value, type?: string): boolean;
|
|
6
|
+
export declare function isEmptyObject(obj: object): boolean;
|
|
7
|
+
export declare class AsyncValidationError extends Error {
|
|
8
|
+
errors: ValidateError[];
|
|
9
|
+
fields: Record<string, ValidateError[]>;
|
|
10
|
+
constructor(errors: ValidateError[], fields: Record<string, ValidateError[]>);
|
|
11
|
+
}
|
|
12
|
+
type ValidateFunc = (data: RuleValuePackage, doIt: (errors: ValidateError[]) => void) => void;
|
|
13
|
+
export declare function asyncMap(objArr: Record<string, RuleValuePackage[]>, option: ValidateOption, func: ValidateFunc, callback: (errors: ValidateError[]) => void, source: Values): Promise<Values>;
|
|
14
|
+
export declare function complementError(rule: InternalRuleItem, source: Values): (oe: ValidateError | (() => string) | string) => ValidateError;
|
|
15
|
+
export declare function deepMerge<T extends object>(target: T, source: Partial<T>): T;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
declare const _default: {
|
|
2
|
+
string: import("..").ExecuteValidator;
|
|
3
|
+
method: import("..").ExecuteValidator;
|
|
4
|
+
number: import("..").ExecuteValidator;
|
|
5
|
+
boolean: import("..").ExecuteValidator;
|
|
6
|
+
regexp: import("..").ExecuteValidator;
|
|
7
|
+
integer: import("..").ExecuteValidator;
|
|
8
|
+
float: import("..").ExecuteValidator;
|
|
9
|
+
array: import("..").ExecuteValidator;
|
|
10
|
+
object: import("..").ExecuteValidator;
|
|
11
|
+
enum: import("..").ExecuteValidator;
|
|
12
|
+
pattern: import("..").ExecuteValidator;
|
|
13
|
+
date: import("..").ExecuteValidator;
|
|
14
|
+
url: import("..").ExecuteValidator;
|
|
15
|
+
hex: import("..").ExecuteValidator;
|
|
16
|
+
email: import("..").ExecuteValidator;
|
|
17
|
+
required: import("..").ExecuteValidator;
|
|
18
|
+
any: import("..").ExecuteValidator;
|
|
19
|
+
};
|
|
20
|
+
export default _default;
|