ddan-js 2.9.3 → 2.9.5

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.
Files changed (133) hide show
  1. package/bin/ddan-js.browser.js +1 -1
  2. package/bin/ddan-js.esm.js +1 -1
  3. package/bin/ddan-js.js +1 -1
  4. package/bin/types/browser.d.ts +3 -3
  5. package/bin/types/index.d.ts +4 -3
  6. package/bin/types/modules/fetch.d.ts +1 -1
  7. package/bin/types/modules/node/index.d.ts +2 -0
  8. package/package.json +18 -13
  9. package/bin/lib/browser.js +0 -111
  10. package/bin/lib/class/event.js +0 -104
  11. package/bin/lib/class/getset.js +0 -12
  12. package/bin/lib/class/icon.js +0 -94
  13. package/bin/lib/class/joker.js +0 -121
  14. package/bin/lib/class/kvalue.js +0 -29
  15. package/bin/lib/class/mapping.js +0 -82
  16. package/bin/lib/class/persist.js +0 -65
  17. package/bin/lib/class/pipeTask.js +0 -42
  18. package/bin/lib/class/pipeparallel.js +0 -24
  19. package/bin/lib/class/store.js +0 -87
  20. package/bin/lib/class/tracker.js +0 -125
  21. package/bin/lib/class/watermark.js +0 -63
  22. package/bin/lib/common/_Symbol.js +0 -5
  23. package/bin/lib/common/_freeGlobal.js +0 -5
  24. package/bin/lib/common/_object.js +0 -40
  25. package/bin/lib/common/_root.js +0 -10
  26. package/bin/lib/common/internal/asciiToArray.js +0 -13
  27. package/bin/lib/common/internal/castSlice.js +0 -18
  28. package/bin/lib/common/internal/createCaseFirst.js +0 -30
  29. package/bin/lib/common/internal/hasUnicode.js +0 -26
  30. package/bin/lib/common/internal/nodeTypes.js +0 -26
  31. package/bin/lib/common/internal/slice.js +0 -46
  32. package/bin/lib/common/internal/stringToArray.js +0 -18
  33. package/bin/lib/common/internal/unicodeToArray.js +0 -40
  34. package/bin/lib/common/internal/unicodeWords.js +0 -67
  35. package/bin/lib/index.js +0 -114
  36. package/bin/lib/modules/browser/ecdh.js +0 -124
  37. package/bin/lib/modules/browser/index.js +0 -4
  38. package/bin/lib/modules/cdn.js +0 -62
  39. package/bin/lib/modules/convert/base.js +0 -125
  40. package/bin/lib/modules/convert/base64.js +0 -114
  41. package/bin/lib/modules/convert/index.js +0 -99
  42. package/bin/lib/modules/convert/md5.js +0 -222
  43. package/bin/lib/modules/convert/pkcs.js +0 -16
  44. package/bin/lib/modules/convert/utf8.js +0 -106
  45. package/bin/lib/modules/crypto/base64.js +0 -14
  46. package/bin/lib/modules/crypto/const.js +0 -7
  47. package/bin/lib/modules/crypto/index.js +0 -30
  48. package/bin/lib/modules/crypto/rsa.js +0 -143
  49. package/bin/lib/modules/crypto/tea.js +0 -79
  50. package/bin/lib/modules/crypto/uuid.js +0 -34
  51. package/bin/lib/modules/css.js +0 -137
  52. package/bin/lib/modules/decorator.js +0 -14
  53. package/bin/lib/modules/fetch.js +0 -82
  54. package/bin/lib/modules/gbk.js +0 -46
  55. package/bin/lib/modules/hook/base.js +0 -60
  56. package/bin/lib/modules/hook/hooker.js +0 -39
  57. package/bin/lib/modules/hook/index.js +0 -49
  58. package/bin/lib/modules/hook/log.js +0 -54
  59. package/bin/lib/modules/hook/modules/bezier.js +0 -121
  60. package/bin/lib/modules/hook/modules/debounce.js +0 -127
  61. package/bin/lib/modules/hook/modules/mutex.js +0 -22
  62. package/bin/lib/modules/hook/modules/pipeline.js +0 -59
  63. package/bin/lib/modules/hook/modules/polling.js +0 -20
  64. package/bin/lib/modules/hook/modules/safeTask.js +0 -25
  65. package/bin/lib/modules/hook/modules/throttle.js +0 -30
  66. package/bin/lib/modules/html.js +0 -293
  67. package/bin/lib/modules/http/index.js +0 -171
  68. package/bin/lib/modules/http/interceptor.js +0 -22
  69. package/bin/lib/modules/list/base.js +0 -37
  70. package/bin/lib/modules/list/index.js +0 -90
  71. package/bin/lib/modules/logger.js +0 -55
  72. package/bin/lib/modules/math/index.js +0 -118
  73. package/bin/lib/modules/mini/diff.js +0 -97
  74. package/bin/lib/modules/mini/http/index.js +0 -155
  75. package/bin/lib/modules/mini/index.js +0 -71
  76. package/bin/lib/modules/node/aliyun.js +0 -156
  77. package/bin/lib/modules/node/brotli.js +0 -46
  78. package/bin/lib/modules/node/child.js +0 -18
  79. package/bin/lib/modules/node/ecdh-web.js +0 -11
  80. package/bin/lib/modules/node/ecdh.js +0 -115
  81. package/bin/lib/modules/node/file.js +0 -25
  82. package/bin/lib/modules/node/index.js +0 -11
  83. package/bin/lib/modules/node/proxy.js +0 -147
  84. package/bin/lib/modules/node/rsa-web.js +0 -19
  85. package/bin/lib/modules/node/socks5.js +0 -424
  86. package/bin/lib/modules/obj/index.js +0 -184
  87. package/bin/lib/modules/qs/base.js +0 -5
  88. package/bin/lib/modules/qs/decode.js +0 -45
  89. package/bin/lib/modules/qs/encode.js +0 -53
  90. package/bin/lib/modules/qs/index.js +0 -114
  91. package/bin/lib/modules/regex.js +0 -16
  92. package/bin/lib/modules/rsa.js +0 -18
  93. package/bin/lib/modules/rule/async-validator/index.js +0 -306
  94. package/bin/lib/modules/rule/async-validator/interface.js +0 -2
  95. package/bin/lib/modules/rule/async-validator/messages.js +0 -59
  96. package/bin/lib/modules/rule/async-validator/rule/enum.js +0 -13
  97. package/bin/lib/modules/rule/async-validator/rule/index.js +0 -16
  98. package/bin/lib/modules/rule/async-validator/rule/pattern.js +0 -27
  99. package/bin/lib/modules/rule/async-validator/rule/range.js +0 -54
  100. package/bin/lib/modules/rule/async-validator/rule/required.js +0 -12
  101. package/bin/lib/modules/rule/async-validator/rule/type.js +0 -98
  102. package/bin/lib/modules/rule/async-validator/rule/url.js +0 -55
  103. package/bin/lib/modules/rule/async-validator/rule/whitespace.js +0 -20
  104. package/bin/lib/modules/rule/async-validator/util.js +0 -255
  105. package/bin/lib/modules/rule/async-validator/validator/any.js +0 -16
  106. package/bin/lib/modules/rule/async-validator/validator/array.js +0 -19
  107. package/bin/lib/modules/rule/async-validator/validator/boolean.js +0 -20
  108. package/bin/lib/modules/rule/async-validator/validator/date.js +0 -31
  109. package/bin/lib/modules/rule/async-validator/validator/enum.js +0 -20
  110. package/bin/lib/modules/rule/async-validator/validator/float.js +0 -20
  111. package/bin/lib/modules/rule/async-validator/validator/index.js +0 -36
  112. package/bin/lib/modules/rule/async-validator/validator/integer.js +0 -20
  113. package/bin/lib/modules/rule/async-validator/validator/method.js +0 -19
  114. package/bin/lib/modules/rule/async-validator/validator/number.js +0 -23
  115. package/bin/lib/modules/rule/async-validator/validator/object.js +0 -19
  116. package/bin/lib/modules/rule/async-validator/validator/pattern.js +0 -19
  117. package/bin/lib/modules/rule/async-validator/validator/regexp.js +0 -19
  118. package/bin/lib/modules/rule/async-validator/validator/required.js +0 -10
  119. package/bin/lib/modules/rule/async-validator/validator/string.js +0 -24
  120. package/bin/lib/modules/rule/async-validator/validator/type.js +0 -20
  121. package/bin/lib/modules/rule/index.js +0 -67
  122. package/bin/lib/modules/string/index.js +0 -121
  123. package/bin/lib/modules/string/words.js +0 -33
  124. package/bin/lib/modules/time/const.js +0 -11
  125. package/bin/lib/modules/time/dtime.js +0 -132
  126. package/bin/lib/modules/time/frame.js +0 -49
  127. package/bin/lib/modules/time/index.js +0 -84
  128. package/bin/lib/typings/index.js +0 -2
  129. package/bin/lib/util/function.js +0 -33
  130. package/bin/lib/util/includes.js +0 -96
  131. package/bin/lib/util/index.js +0 -26
  132. package/bin/lib/util/is.js +0 -148
  133. package/bin/lib/util/to.js +0 -47
@@ -1,53 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const stringifyPrimitive = function (v) {
4
- switch (typeof v) {
5
- case 'string':
6
- return v;
7
- case 'boolean':
8
- return v ? 'true' : 'false';
9
- case 'number':
10
- return isFinite(v) ? v : '';
11
- default:
12
- return '';
13
- }
14
- };
15
- const _encodeURIComponent = function (v, uri = true) {
16
- return uri ? encodeURIComponent(v) : v;
17
- };
18
- const encode = function (obj, { sep = "&", eq = "=", name = null, uri = true, cleanZero = false, cleanNull = false, cleanWhitespace = false } = {}) {
19
- sep = sep || '&';
20
- eq = eq || '=';
21
- if (obj === null) {
22
- obj = undefined;
23
- }
24
- if (typeof obj === 'object') {
25
- return Object.keys(obj).map(function (k) {
26
- const ks = _encodeURIComponent(stringifyPrimitive(k), uri) + eq;
27
- const _v = obj[k];
28
- if (Array.isArray(_v)) {
29
- return _v.map(function (v) {
30
- return ks + _encodeURIComponent(stringifyPrimitive(v), uri);
31
- }).join(sep);
32
- }
33
- else {
34
- if (isInvalid(_v, { cleanZero, cleanNull, cleanWhitespace }))
35
- return "";
36
- return ks + _encodeURIComponent(stringifyPrimitive(_v), uri);
37
- }
38
- }).filter(Boolean).join(sep);
39
- }
40
- if (!name)
41
- return '';
42
- return _encodeURIComponent(stringifyPrimitive(name), uri) + eq + _encodeURIComponent(stringifyPrimitive(obj), uri);
43
- };
44
- const isInvalid = function (v, { cleanZero = false, cleanNull = false, cleanWhitespace = false } = {}) {
45
- if (cleanZero && v === 0)
46
- return true;
47
- if (cleanNull && (v === null || v === undefined))
48
- return true;
49
- if (cleanWhitespace && (typeof v === 'string' && v.trim() === ''))
50
- return true;
51
- return false;
52
- };
53
- exports.default = encode;
@@ -1,114 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const decode_1 = require("./decode");
4
- const encode_1 = require("./encode");
5
- const https = (url) => {
6
- if (!url)
7
- return url;
8
- return url.replace(/^http[s]?/, 'https');
9
- };
10
- // const RegHost = /^(http[s]?):\/\/([\w.]+)(?::(\d+))?/
11
- const RegHost = /^(https?):\/\/([\w.\_\-]+)(?::(\d+))?/;
12
- const parseUrl = (url) => {
13
- const hostInfo = parseHost(url);
14
- const { origin = '', url: _url = '', query = '' } = hostInfo;
15
- const pathInfo = parsePath(_url);
16
- const params = (0, decode_1.default)(query, { multiple: true, uri: false });
17
- const result = {
18
- ...hostInfo,
19
- ...pathInfo,
20
- pathname: _url.substring(_url.indexOf(origin || '') + origin.length),
21
- params,
22
- };
23
- return result;
24
- };
25
- const parsePath = (url) => {
26
- const _url = (url || '').replace(/\\/g, '/').split('?')[0];
27
- const mat = _url.match(/[\/]?([\w\_\-]+)[.]?[\w]*[?]?$/);
28
- const page = mat ? mat[1] : '';
29
- const idx = _url.lastIndexOf('/') + 1;
30
- const name = _url.substring(idx);
31
- const last = name.lastIndexOf('.');
32
- const withoutExtension = name.substring(0, last) || '';
33
- const extname = last >= 0 ? name.substring(last) : '';
34
- const route = _url.substring(0, idx) || '';
35
- return { route, page, name, withoutExtension, extname };
36
- };
37
- const join = (...args) => {
38
- let arr = [];
39
- args.forEach((e) => {
40
- const _s = e.trim().replace(/^\/*(.*?)\/*$/, '$1');
41
- if (_s.length <= 0)
42
- return;
43
- if (/^(\w+):\/\//.test(_s)) {
44
- arr = [_s];
45
- }
46
- else {
47
- arr.push(_s);
48
- }
49
- });
50
- return arr.join('/');
51
- };
52
- const parseHost = (url) => {
53
- const result = {
54
- href: url,
55
- url: '',
56
- origin: '',
57
- protocol: '',
58
- host: '',
59
- port: '',
60
- query: '',
61
- };
62
- if (!url)
63
- return result;
64
- const matches = url.match(RegHost);
65
- if (!matches || matches.length < 4)
66
- return result;
67
- const port = matches[3] || '';
68
- const arr = url.split('?');
69
- result.url = arr[0];
70
- result.origin = matches[0];
71
- result.protocol = `${matches[1]}:`;
72
- result.host = `${matches[2]}${port ? ':' + port : ''}`;
73
- result.port = port;
74
- result.query = arr.length > 1 ? arr[1] : '';
75
- return result;
76
- };
77
- const shExpMatch = (text, pattern) => {
78
- if (text === pattern)
79
- return true;
80
- if (!text)
81
- return false;
82
- if (!pattern)
83
- return false;
84
- if (pattern === '*')
85
- return true;
86
- // 将模式转换为正则表达式
87
- const regexPattern = pattern
88
- .replace(/\./g, '\\.') // 转义 .
89
- .replace(/\*/g, '.*') // 将 * 转换为 .*
90
- .replace(/\?/g, '.'); // 将 ? 转换为 .
91
- // const regex = new RegExp(regexPattern)
92
- const regex = new RegExp('^' + regexPattern + '$');
93
- return regex.test(text);
94
- };
95
- const isLocalIpAddress = (addr) => {
96
- if (!addr)
97
- return false;
98
- if (shExpMatch(addr, '127.0.0.1') ||
99
- shExpMatch(addr, 'localhost') ||
100
- shExpMatch(addr, '192.168.*.*'))
101
- return true;
102
- return false;
103
- };
104
- exports.default = {
105
- parse: decode_1.default,
106
- stringify: encode_1.default,
107
- https,
108
- parseUrl,
109
- parsePath,
110
- parseHost,
111
- join,
112
- shExpMatch,
113
- isLocalIpAddress,
114
- };
@@ -1,16 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const regex = (patt, modifiers) => new RegExp(patt, modifiers);
4
- const pattern = {
5
- num: "0-9",
6
- zh: "\u4E00-\u9FA5",
7
- en: "A-Za-z",
8
- };
9
- const entire = {
10
- chinese: /^[\u4E00-\u9FA5]+$/,
11
- real: /^(\-|\+)?\d+(\.\d+)?$/,
12
- cssReal: /^(\-|\+)?\d*(\.)?\d+$/,
13
- id: /^[A-Za-z0-9]+$/,
14
- yyyyMMdd: /^(\d{4})-?(\d{2})-?(\d{2})$/,
15
- };
16
- exports.default = { pattern, regex, entire };
@@ -1,18 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const rsa_1 = require("./crypto/rsa");
4
- const __subtle = ((typeof window !== 'undefined') ? window.crypto.subtle : {});
5
- const generateKeys = () => rsa_1.default.generateKeys(__subtle);
6
- // SHA256: 256 - 2 - 2*32 = 190
7
- // SHA1: 256 - 2 - 2*20 = 214
8
- const encrypt = async (data, publicKey) => rsa_1.default.encrypt(__subtle, data, publicKey);
9
- const decrypt = async (encryptedBase64, privateKey) => rsa_1.default.decrypt(__subtle, encryptedBase64, privateKey);
10
- const encode = async (data) => rsa_1.default.encrypt(__subtle, data, rsa_1.default.RSAPublicKey);
11
- const decode = async (encryptedBase64) => rsa_1.default.decrypt(__subtle, encryptedBase64, rsa_1.default.RSAPrivateKey);
12
- exports.default = {
13
- generateKeys,
14
- encrypt,
15
- decrypt,
16
- encode,
17
- decode,
18
- };
@@ -1,306 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- const util_1 = require("./util");
18
- const index_1 = require("./validator/index");
19
- const messages_1 = require("./messages");
20
- __exportStar(require("./interface"), exports);
21
- /**
22
- * Encapsulates a validation schema.
23
- *
24
- * @param descriptor An object declaring validation rules
25
- * for this schema.
26
- */
27
- class Schema {
28
- // ========================= Static =========================
29
- static register = function register(type, validator) {
30
- if (typeof validator !== 'function') {
31
- throw new Error('Cannot register a validator by type, validator is not a function');
32
- }
33
- index_1.default[type] = validator;
34
- };
35
- static warning = util_1.warning;
36
- static messages = messages_1.messages;
37
- static validators = index_1.default;
38
- // ======================== Instance ========================
39
- rules = {};
40
- _messages = messages_1.messages;
41
- constructor(descriptor) {
42
- this.define(descriptor);
43
- }
44
- define(rules) {
45
- if (!rules) {
46
- throw new Error('Cannot configure a schema with no rules');
47
- }
48
- if (typeof rules !== 'object' || Array.isArray(rules)) {
49
- throw new Error('Rules must be an object');
50
- }
51
- this.rules = {};
52
- Object.keys(rules).forEach(name => {
53
- const item = rules[name];
54
- this.rules[name] = Array.isArray(item) ? item : [item];
55
- });
56
- }
57
- messages(messages) {
58
- if (messages) {
59
- this._messages = (0, util_1.deepMerge)((0, messages_1.newMessages)(), messages);
60
- }
61
- return this._messages;
62
- }
63
- validate(source_, o = {}, oc = () => { }) {
64
- let source = source_;
65
- let options = o;
66
- let callback = oc;
67
- if (typeof options === 'function') {
68
- callback = options;
69
- options = {};
70
- }
71
- if (!this.rules || Object.keys(this.rules).length === 0) {
72
- if (callback) {
73
- callback(null, source);
74
- }
75
- return Promise.resolve(source);
76
- }
77
- function complete(results) {
78
- let errors = [];
79
- let fields = {};
80
- function add(e) {
81
- if (Array.isArray(e)) {
82
- errors = errors.concat(...e);
83
- }
84
- else {
85
- errors.push(e);
86
- }
87
- }
88
- for (let i = 0; i < results.length; i++) {
89
- add(results[i]);
90
- }
91
- if (!errors.length) {
92
- callback(null, source);
93
- }
94
- else {
95
- fields = (0, util_1.convertFieldsError)(errors);
96
- callback(errors, fields);
97
- }
98
- }
99
- if (options.messages) {
100
- let messages = this.messages();
101
- if (messages === messages_1.messages) {
102
- messages = (0, messages_1.newMessages)();
103
- }
104
- (0, util_1.deepMerge)(messages, options.messages);
105
- options.messages = messages;
106
- }
107
- else {
108
- options.messages = this.messages();
109
- }
110
- const series = {};
111
- const keys = options.keys || Object.keys(this.rules);
112
- keys.forEach(z => {
113
- const arr = this.rules[z];
114
- let value = source[z];
115
- arr.forEach(r => {
116
- let rule = r;
117
- if (typeof rule.transform === 'function') {
118
- if (source === source_) {
119
- source = { ...source };
120
- }
121
- value = source[z] = rule.transform(value);
122
- }
123
- if (typeof rule === 'function') {
124
- rule = {
125
- validator: rule,
126
- };
127
- }
128
- else {
129
- rule = { ...rule };
130
- }
131
- // Fill validator. Skip if nothing need to validate
132
- rule.validator = this.getValidationMethod(rule);
133
- if (!rule.validator) {
134
- return;
135
- }
136
- rule.field = z;
137
- rule.fullField = rule.fullField || z;
138
- rule.type = this.getType(rule);
139
- series[z] = series[z] || [];
140
- series[z].push({
141
- rule,
142
- value,
143
- source,
144
- field: z,
145
- });
146
- });
147
- });
148
- const errorFields = {};
149
- return (0, util_1.asyncMap)(series, options, (data, doIt) => {
150
- const rule = data.rule;
151
- let deep = (rule.type === 'object' || rule.type === 'array') &&
152
- (typeof rule.fields === 'object' ||
153
- typeof rule.defaultField === 'object');
154
- deep = deep && (rule.required || (!rule.required && data.value));
155
- rule.field = data.field;
156
- function addFullField(key, schema) {
157
- return {
158
- ...schema,
159
- fullField: `${rule.fullField}.${key}`,
160
- fullFields: rule.fullFields ? [...rule.fullFields, key] : [key],
161
- };
162
- }
163
- function cb(e = []) {
164
- let errorList = Array.isArray(e) ? e : [e];
165
- if (options.suppressWarning === true && errorList.length) {
166
- Schema.warning('async-validator:', errorList);
167
- }
168
- if (errorList.length && rule.message !== undefined) {
169
- //@ts-ignore
170
- errorList = [].concat(rule.message);
171
- }
172
- // Fill error info
173
- let filledErrors = errorList.map((0, util_1.complementError)(rule, source));
174
- if (options.first && filledErrors.length) {
175
- errorFields[rule.field] = 1;
176
- return doIt(filledErrors);
177
- }
178
- if (!deep) {
179
- doIt(filledErrors);
180
- }
181
- else {
182
- // if rule is required but the target object
183
- // does not exist fail at the rule level and don't
184
- // go deeper
185
- if (rule.required && !data.value) {
186
- if (rule.message !== undefined) {
187
- filledErrors = []
188
- //@ts-ignore
189
- .concat(rule.message)
190
- .map((0, util_1.complementError)(rule, source));
191
- }
192
- else if (options.error) {
193
- filledErrors = [
194
- options.error(rule, (0, util_1.format)(options.messages.required, rule.field)),
195
- ];
196
- }
197
- return doIt(filledErrors);
198
- }
199
- let fieldsSchema = {};
200
- if (rule.defaultField) {
201
- Object.keys(data.value).map(key => {
202
- //@ts-ignore
203
- fieldsSchema[key] = rule.defaultField;
204
- });
205
- }
206
- fieldsSchema = {
207
- ...fieldsSchema,
208
- ...data.rule.fields,
209
- };
210
- const paredFieldsSchema = {};
211
- Object.keys(fieldsSchema).forEach(field => {
212
- const fieldSchema = fieldsSchema[field];
213
- const fieldSchemaList = Array.isArray(fieldSchema)
214
- ? fieldSchema
215
- : [fieldSchema];
216
- paredFieldsSchema[field] = fieldSchemaList.map(addFullField.bind(null, field));
217
- });
218
- const schema = new Schema(paredFieldsSchema);
219
- schema.messages(options.messages);
220
- if (data.rule.options) {
221
- data.rule.options.messages = options.messages;
222
- data.rule.options.error = options.error;
223
- }
224
- schema.validate(data.value, data.rule.options || options, errs => {
225
- const finalErrors = [];
226
- if (filledErrors && filledErrors.length) {
227
- //@ts-ignore
228
- finalErrors.push(...filledErrors);
229
- }
230
- if (errs && errs.length) {
231
- //@ts-ignore
232
- finalErrors.push(...errs);
233
- }
234
- //@ts-ignore
235
- doIt(finalErrors.length ? finalErrors : null);
236
- });
237
- }
238
- }
239
- let res = undefined;
240
- if (rule.asyncValidator) {
241
- res = rule.asyncValidator(rule, data.value, cb, data.source, options);
242
- }
243
- else if (rule.validator) {
244
- try {
245
- res = rule.validator(rule, data.value, cb, data.source, options);
246
- }
247
- catch (error) {
248
- console.error?.(error);
249
- // rethrow to report error
250
- if (!options.suppressValidatorError) {
251
- setTimeout(() => {
252
- throw error;
253
- }, 0);
254
- }
255
- //@ts-ignore
256
- cb(error.message);
257
- }
258
- if (res === true) {
259
- cb();
260
- }
261
- else if (res === false) {
262
- cb(typeof rule.message === 'function'
263
- ? rule.message(rule.fullField || rule.field)
264
- : rule.message || `${rule.fullField || rule.field} fails`);
265
- }
266
- else if (res instanceof Array) {
267
- cb(res);
268
- }
269
- else if (res instanceof Error) {
270
- cb(res.message);
271
- }
272
- }
273
- if (res && res.then) {
274
- res.then(() => cb(), e => cb(e));
275
- }
276
- }, results => {
277
- complete(results);
278
- }, source);
279
- }
280
- getType(rule) {
281
- if (rule.type === undefined && rule.pattern instanceof RegExp) {
282
- rule.type = 'pattern';
283
- }
284
- if (typeof rule.validator !== 'function' &&
285
- rule.type &&
286
- !index_1.default.hasOwnProperty(rule.type)) {
287
- throw new Error((0, util_1.format)('Unknown rule type %s', rule.type));
288
- }
289
- return rule.type || 'string';
290
- }
291
- getValidationMethod(rule) {
292
- if (typeof rule.validator === 'function') {
293
- return rule.validator;
294
- }
295
- const keys = Object.keys(rule);
296
- const messageIndex = keys.indexOf('message');
297
- if (messageIndex !== -1) {
298
- keys.splice(messageIndex, 1);
299
- }
300
- if (keys.length === 1 && keys[0] === 'required') {
301
- return index_1.default.required;
302
- }
303
- return index_1.default[this.getType(rule)] || undefined;
304
- }
305
- }
306
- exports.default = Schema;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,59 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.messages = exports.newMessages = void 0;
4
- function newMessages() {
5
- return {
6
- default: 'Validation error on field %s',
7
- required: '%s is required',
8
- enum: '%s must be one of %s',
9
- whitespace: '%s cannot be empty',
10
- date: {
11
- format: '%s date %s is invalid for format %s',
12
- parse: '%s date could not be parsed, %s is invalid ',
13
- invalid: '%s date %s is invalid',
14
- },
15
- types: {
16
- string: '%s is not a %s',
17
- method: '%s is not a %s (function)',
18
- array: '%s is not an %s',
19
- object: '%s is not an %s',
20
- number: '%s is not a %s',
21
- date: '%s is not a %s',
22
- boolean: '%s is not a %s',
23
- integer: '%s is not an %s',
24
- float: '%s is not a %s',
25
- regexp: '%s is not a valid %s',
26
- email: '%s is not a valid %s',
27
- url: '%s is not a valid %s',
28
- hex: '%s is not a valid %s',
29
- },
30
- string: {
31
- len: '%s must be exactly %s characters',
32
- min: '%s must be at least %s characters',
33
- max: '%s cannot be longer than %s characters',
34
- range: '%s must be between %s and %s characters',
35
- },
36
- number: {
37
- len: '%s must equal %s',
38
- min: '%s cannot be less than %s',
39
- max: '%s cannot be greater than %s',
40
- range: '%s must be between %s and %s',
41
- },
42
- array: {
43
- len: '%s must be exactly %s in length',
44
- min: '%s cannot be less than %s in length',
45
- max: '%s cannot be greater than %s in length',
46
- range: '%s must be between %s and %s in length',
47
- },
48
- pattern: {
49
- mismatch: '%s value %s does not match pattern %s',
50
- },
51
- clone() {
52
- const cloned = JSON.parse(JSON.stringify(this));
53
- cloned.clone = this.clone;
54
- return cloned;
55
- },
56
- };
57
- }
58
- exports.newMessages = newMessages;
59
- exports.messages = newMessages();
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const util_1 = require("../util");
4
- const ENUM = 'enum';
5
- const enumerable = (rule, value, source, errors, options) => {
6
- rule[ENUM] = Array.isArray(rule[ENUM]) ? rule[ENUM] : [];
7
- if (rule[ENUM].indexOf(value) === -1) {
8
- errors.push(
9
- //@ts-ignore
10
- (0, util_1.format)(options.messages[ENUM], rule.fullField, rule[ENUM].join(', ')));
11
- }
12
- };
13
- exports.default = enumerable;
@@ -1,16 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const required_1 = require("./required");
4
- const whitespace_1 = require("./whitespace");
5
- const type_1 = require("./type");
6
- const range_1 = require("./range");
7
- const enum_1 = require("./enum");
8
- const pattern_1 = require("./pattern");
9
- exports.default = {
10
- required: required_1.default,
11
- whitespace: whitespace_1.default,
12
- type: type_1.default,
13
- range: range_1.default,
14
- enum: enum_1.default,
15
- pattern: pattern_1.default,
16
- };
@@ -1,27 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const util_1 = require("../util");
4
- const pattern = (rule, value, source, errors, options) => {
5
- if (rule.pattern) {
6
- if (rule.pattern instanceof RegExp) {
7
- // if a RegExp instance is passed, reset `lastIndex` in case its `global`
8
- // flag is accidentally set to `true`, which in a validation scenario
9
- // is not necessary and the result might be misleading
10
- rule.pattern.lastIndex = 0;
11
- if (!rule.pattern.test(value)) {
12
- errors.push((0, util_1.format)(
13
- //@ts-ignore
14
- options.messages.pattern.mismatch, rule.fullField, value, rule.pattern));
15
- }
16
- }
17
- else if (typeof rule.pattern === 'string') {
18
- const _pattern = new RegExp(rule.pattern);
19
- if (!_pattern.test(value)) {
20
- errors.push((0, util_1.format)(
21
- //@ts-ignore
22
- options.messages.pattern.mismatch, rule.fullField, value, rule.pattern));
23
- }
24
- }
25
- }
26
- };
27
- exports.default = pattern;
@@ -1,54 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const util_1 = require("../util");
4
- const range = (rule, value, source, errors, options) => {
5
- const len = typeof rule.len === 'number';
6
- const min = typeof rule.min === 'number';
7
- const max = typeof rule.max === 'number';
8
- // 正则匹配码点范围从U+010000一直到U+10FFFF的文字(补充平面Supplementary Plane)
9
- const spRegexp = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
10
- let val = value;
11
- let key = "";
12
- const num = typeof value === 'number';
13
- const str = typeof value === 'string';
14
- const arr = Array.isArray(value);
15
- if (num) {
16
- key = 'number';
17
- }
18
- else if (str) {
19
- key = 'string';
20
- }
21
- else if (arr) {
22
- key = 'array';
23
- }
24
- // if the value is not of a supported type for range validation
25
- // the validation rule rule should use the
26
- // type property to also test for a particular type
27
- if (!key) {
28
- return false;
29
- }
30
- if (arr) {
31
- val = value.length;
32
- }
33
- if (str) {
34
- // 处理码点大于U+010000的文字length属性不准确的bug,如"𠮷𠮷𠮷".length !== 3
35
- val = value.replace(spRegexp, '_').length;
36
- }
37
- if (len) {
38
- if (val !== rule.len) {
39
- //@ts-ignore
40
- errors.push((0, util_1.format)(options.messages[key].len, rule.fullField, rule.len));
41
- }
42
- //@ts-ignore
43
- }
44
- else if (min && !max && val < rule.min) {
45
- errors.push((0, util_1.format)(options.messages[key].min, rule.fullField, rule.min));
46
- }
47
- else if (max && !min && val > rule.max) {
48
- errors.push((0, util_1.format)(options.messages[key].max, rule.fullField, rule.max));
49
- }
50
- else if (min && max && (val < rule.min || val > rule.max)) {
51
- errors.push((0, util_1.format)(options.messages[key].range, rule.fullField, rule.min, rule.max));
52
- }
53
- };
54
- exports.default = range;