hono 2.3.2 → 2.4.0

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 (141) hide show
  1. package/dist/cjs/compose.js +82 -66
  2. package/dist/cjs/context.js +159 -144
  3. package/dist/cjs/hono.js +173 -150
  4. package/dist/cjs/index.js +31 -11
  5. package/dist/cjs/middleware/basic-auth/index.js +70 -39
  6. package/dist/cjs/middleware/bearer-auth/index.js +69 -51
  7. package/dist/cjs/middleware/cache/index.js +49 -28
  8. package/dist/cjs/middleware/compress/index.js +39 -16
  9. package/dist/cjs/middleware/cors/index.js +89 -71
  10. package/dist/cjs/middleware/etag/index.js +46 -24
  11. package/dist/cjs/middleware/html/index.js +50 -30
  12. package/dist/cjs/middleware/jsx/index.js +178 -166
  13. package/dist/cjs/middleware/jsx/jsx-dev-runtime.js +30 -7
  14. package/dist/cjs/middleware/jsx/jsx-runtime.js +30 -6
  15. package/dist/cjs/middleware/jwt/index.js +74 -54
  16. package/dist/cjs/middleware/logger/index.js +59 -38
  17. package/dist/cjs/middleware/powered-by/index.js +30 -7
  18. package/dist/cjs/middleware/pretty-json/index.js +31 -8
  19. package/dist/cjs/middleware/serve-static/bun.js +56 -38
  20. package/dist/cjs/middleware/serve-static/index.js +27 -4
  21. package/dist/cjs/middleware/serve-static/module.js +42 -0
  22. package/dist/cjs/middleware/serve-static/serve-static.js +58 -39
  23. package/dist/cjs/middleware/validator/index.js +27 -4
  24. package/dist/cjs/middleware/validator/middleware.js +125 -91
  25. package/dist/cjs/package.json +3 -0
  26. package/dist/cjs/request.js +129 -116
  27. package/dist/cjs/router/reg-exp-router/index.js +27 -4
  28. package/dist/cjs/router/reg-exp-router/node.js +108 -97
  29. package/dist/cjs/router/reg-exp-router/router.js +158 -141
  30. package/dist/cjs/router/reg-exp-router/trie.js +55 -39
  31. package/dist/cjs/router/smart-router/index.js +27 -4
  32. package/dist/cjs/router/smart-router/router.js +70 -49
  33. package/dist/cjs/router/static-router/index.js +27 -4
  34. package/dist/cjs/router/static-router/router.js +78 -64
  35. package/dist/cjs/router/trie-router/index.js +27 -4
  36. package/dist/cjs/router/trie-router/node.js +167 -159
  37. package/dist/cjs/router/trie-router/router.js +43 -20
  38. package/dist/cjs/router.js +35 -6
  39. package/dist/cjs/types.js +16 -0
  40. package/dist/cjs/utils/body.js +36 -15
  41. package/dist/cjs/utils/buffer.js +56 -31
  42. package/dist/cjs/utils/cloudflare.js +53 -34
  43. package/dist/cjs/utils/cookie.js +59 -35
  44. package/dist/cjs/utils/crypto.js +69 -43
  45. package/dist/cjs/utils/encode.js +92 -65
  46. package/dist/cjs/utils/filepath.js +39 -22
  47. package/dist/cjs/utils/html.js +54 -33
  48. package/dist/cjs/utils/http-status.js +68 -45
  49. package/dist/cjs/utils/json.js +91 -76
  50. package/dist/cjs/utils/jwt/index.js +32 -25
  51. package/dist/cjs/utils/jwt/jwt.js +124 -93
  52. package/dist/cjs/utils/jwt/types.js +65 -38
  53. package/dist/cjs/utils/mime.js +110 -87
  54. package/dist/cjs/utils/object.js +53 -35
  55. package/dist/cjs/utils/types.js +15 -1
  56. package/dist/cjs/utils/url.js +89 -78
  57. package/dist/cjs/validator/rule.js +78 -64
  58. package/dist/cjs/validator/sanitizer.js +28 -4
  59. package/dist/cjs/validator/schema.js +15 -1
  60. package/dist/cjs/validator/validator.js +405 -386
  61. package/dist/compose.d.ts +1 -1
  62. package/dist/compose.js +61 -64
  63. package/dist/context.d.ts +4 -4
  64. package/dist/context.js +139 -143
  65. package/dist/hono.d.ts +1 -17
  66. package/dist/hono.js +153 -149
  67. package/dist/index.d.ts +3 -1
  68. package/dist/index.js +9 -9
  69. package/dist/middleware/basic-auth/index.d.ts +1 -1
  70. package/dist/middleware/basic-auth/index.js +52 -40
  71. package/dist/middleware/bearer-auth/index.d.ts +1 -1
  72. package/dist/middleware/bearer-auth/index.js +48 -49
  73. package/dist/middleware/cache/index.d.ts +1 -1
  74. package/dist/middleware/cache/index.js +28 -26
  75. package/dist/middleware/compress/index.d.ts +1 -1
  76. package/dist/middleware/compress/index.js +18 -14
  77. package/dist/middleware/cors/index.d.ts +1 -1
  78. package/dist/middleware/cors/index.js +68 -69
  79. package/dist/middleware/etag/index.d.ts +1 -1
  80. package/dist/middleware/etag/index.js +25 -22
  81. package/dist/middleware/html/index.js +29 -28
  82. package/dist/middleware/jsx/index.js +164 -172
  83. package/dist/middleware/jsx/jsx-dev-runtime.js +9 -5
  84. package/dist/middleware/jsx/jsx-runtime.js +7 -2
  85. package/dist/middleware/jwt/index.d.ts +1 -1
  86. package/dist/middleware/jwt/index.js +53 -52
  87. package/dist/middleware/logger/index.d.ts +1 -1
  88. package/dist/middleware/logger/index.js +35 -39
  89. package/dist/middleware/powered-by/index.d.ts +1 -1
  90. package/dist/middleware/powered-by/index.js +9 -5
  91. package/dist/middleware/pretty-json/index.d.ts +1 -1
  92. package/dist/middleware/pretty-json/index.js +10 -6
  93. package/dist/middleware/serve-static/bun.d.ts +1 -1
  94. package/dist/middleware/serve-static/bun.js +35 -36
  95. package/dist/middleware/serve-static/index.js +5 -1
  96. package/dist/middleware/serve-static/{module.d.mts → module.d.ts} +1 -1
  97. package/dist/middleware/serve-static/module.js +13 -0
  98. package/dist/middleware/serve-static/serve-static.d.ts +1 -1
  99. package/dist/middleware/serve-static/serve-static.js +36 -36
  100. package/dist/middleware/validator/index.js +5 -2
  101. package/dist/middleware/validator/middleware.d.ts +1 -1
  102. package/dist/middleware/validator/middleware.js +104 -89
  103. package/dist/request.js +108 -114
  104. package/dist/router/reg-exp-router/index.js +5 -1
  105. package/dist/router/reg-exp-router/node.js +87 -96
  106. package/dist/router/reg-exp-router/router.js +140 -142
  107. package/dist/router/reg-exp-router/trie.js +35 -38
  108. package/dist/router/smart-router/index.js +5 -1
  109. package/dist/router/smart-router/router.js +50 -48
  110. package/dist/router/static-router/index.js +5 -1
  111. package/dist/router/static-router/router.js +58 -63
  112. package/dist/router/trie-router/index.js +5 -1
  113. package/dist/router/trie-router/node.js +147 -158
  114. package/dist/router/trie-router/router.js +23 -19
  115. package/dist/router.js +12 -5
  116. package/dist/types.d.ts +22 -0
  117. package/dist/types.js +0 -0
  118. package/dist/utils/body.js +15 -13
  119. package/dist/utils/buffer.js +35 -29
  120. package/dist/utils/cloudflare.js +32 -32
  121. package/dist/utils/cookie.js +38 -33
  122. package/dist/utils/crypto.js +48 -41
  123. package/dist/utils/encode.js +70 -62
  124. package/dist/utils/filepath.js +18 -20
  125. package/dist/utils/html.js +34 -32
  126. package/dist/utils/http-status.js +48 -44
  127. package/dist/utils/json.js +71 -75
  128. package/dist/utils/jwt/index.js +5 -1
  129. package/dist/utils/jwt/jwt.js +95 -90
  130. package/dist/utils/jwt/types.js +47 -41
  131. package/dist/utils/mime.js +90 -86
  132. package/dist/utils/object.js +31 -32
  133. package/dist/utils/types.js +0 -1
  134. package/dist/utils/url.js +69 -77
  135. package/dist/validator/rule.js +56 -62
  136. package/dist/validator/sanitizer.js +6 -2
  137. package/dist/validator/schema.js +0 -1
  138. package/dist/validator/validator.d.ts +5 -4
  139. package/dist/validator/validator.js +393 -393
  140. package/package.json +14 -8
  141. package/dist/middleware/serve-static/module.mjs +0 -13
@@ -1,406 +1,406 @@
1
- import { JSONPathCopy } from './../utils/json';
2
- import { rule } from './rule';
3
- import { sanitizer } from './sanitizer';
4
- export class VObjectBase {
5
- constructor(container, key) {
6
- this.keys = [];
7
- this._isOptional = false;
8
- this.getValidators = () => {
9
- const validators = [];
10
- const thisKeys = [];
11
- Object.assign(thisKeys, this.keys);
12
- const walk = (container, keys, isOptional) => {
13
- for (const v of Object.values(container)) {
14
- if (v instanceof VArray || v instanceof VObject) {
15
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
16
- // @ts-ignore
17
- isOptional || (isOptional = v._isOptional);
18
- keys.push(...v.keys);
19
- walk(v.container, keys, isOptional);
20
- const tmp = [];
21
- Object.assign(tmp, thisKeys);
22
- keys = tmp;
23
- }
24
- else if (v instanceof VBase) {
25
- if (isOptional)
26
- v.isOptional();
27
- v.baseKeys.push(...keys);
28
- validators.push(v);
29
- }
30
- }
31
- };
32
- walk(this.container, this.keys, this._isOptional);
33
- return validators;
34
- };
35
- this.container = container;
36
- if (this instanceof VArray) {
37
- this.keys.push(key, '[*]');
1
+ // src/validator/validator.ts
2
+ import { JSONPathCopy } from "./../utils/json.js";
3
+ import { rule } from "./rule.js";
4
+ import { sanitizer } from "./sanitizer.js";
5
+ var VObjectBase = class {
6
+ constructor(container, key) {
7
+ this.keys = [];
8
+ this._isOptional = false;
9
+ this.getValidators = () => {
10
+ const validators = [];
11
+ const thisKeys = [];
12
+ Object.assign(thisKeys, this.keys);
13
+ const walk = (container, keys, isOptional) => {
14
+ for (const v of Object.values(container)) {
15
+ if (v instanceof VArray || v instanceof VObject) {
16
+ isOptional || (isOptional = v._isOptional);
17
+ keys.push(...v.keys);
18
+ walk(v.container, keys, isOptional);
19
+ const tmp = [];
20
+ Object.assign(tmp, thisKeys);
21
+ keys = tmp;
22
+ } else if (v instanceof VBase) {
23
+ if (isOptional)
24
+ v.isOptional();
25
+ v.baseKeys.push(...keys);
26
+ validators.push(v);
27
+ }
38
28
  }
39
- else if (this instanceof VObject) {
40
- this.keys.push(key);
41
- }
42
- }
43
- isOptional() {
44
- this._isOptional = true;
45
- return this;
46
- }
47
- }
48
- export class VObject extends VObjectBase {
49
- constructor(container, key) {
50
- super(container, key);
51
- }
52
- }
53
- export class VArray extends VObjectBase {
54
- constructor(container, key) {
55
- super(container, key);
56
- this.type = 'array';
29
+ };
30
+ walk(this.container, this.keys, this._isOptional);
31
+ return validators;
32
+ };
33
+ this.container = container;
34
+ if (this instanceof VArray) {
35
+ this.keys.push(key, "[*]");
36
+ } else if (this instanceof VObject) {
37
+ this.keys.push(key);
57
38
  }
58
- }
59
- export class Validator {
60
- constructor() {
61
- this.isArray = false;
62
- this.query = (key) => new VString({ target: 'query', key: key });
63
- this.header = (key) => new VString({ target: 'header', key: key });
64
- this.body = (key) => new VString({ target: 'body', key: key });
65
- this.json = (key) => {
66
- if (this.isArray) {
67
- return new VStringArray({ target: 'json', key: key });
68
- }
69
- else {
70
- return new VString({ target: 'json', key: key });
71
- }
72
- };
73
- this.array = (path, validator) => {
74
- this.isArray = true;
75
- const res = validator(this);
76
- const arr = new VArray(res, path);
77
- return arr;
78
- };
79
- this.object = (path, validator) => {
80
- this.isArray = false;
81
- const res = validator(this);
82
- const obj = new VObject(res, path);
83
- return obj;
84
- };
85
- }
86
- }
87
- export class VBase {
88
- constructor(options) {
89
- this.baseKeys = [];
90
- this._nested = () => (this.baseKeys.length ? true : false);
91
- this.addSanitizer = (sanitizer) => {
92
- this.sanitizers.push(sanitizer);
93
- return this;
94
- };
95
- this.message = (text) => {
96
- const len = this.rules.length;
97
- if (len >= 1) {
98
- this.rules[len - 1].customMessage = text;
99
- }
100
- return this;
101
- };
102
- this.isRequired = () => {
103
- return this.addRule('isRequired', (value) => {
104
- if (value !== undefined && value !== null && value !== '')
105
- return true;
39
+ }
40
+ isOptional() {
41
+ this._isOptional = true;
42
+ return this;
43
+ }
44
+ };
45
+ var VObject = class extends VObjectBase {
46
+ constructor(container, key) {
47
+ super(container, key);
48
+ }
49
+ };
50
+ var VArray = class extends VObjectBase {
51
+ constructor(container, key) {
52
+ super(container, key);
53
+ this.type = "array";
54
+ }
55
+ };
56
+ var Validator = class {
57
+ constructor() {
58
+ this.isArray = false;
59
+ this.query = (key) => new VString({ target: "query", key });
60
+ this.queries = (key) => new VStringArray({ target: "queries", key });
61
+ this.header = (key) => new VString({ target: "header", key });
62
+ this.body = (key) => new VString({ target: "body", key });
63
+ this.json = (key) => {
64
+ if (this.isArray) {
65
+ return new VStringArray({ target: "json", key });
66
+ } else {
67
+ return new VString({ target: "json", key });
68
+ }
69
+ };
70
+ this.array = (path, validator) => {
71
+ this.isArray = true;
72
+ const res = validator(this);
73
+ const arr = new VArray(res, path);
74
+ return arr;
75
+ };
76
+ this.object = (path, validator) => {
77
+ this.isArray = false;
78
+ const res = validator(this);
79
+ const obj = new VObject(res, path);
80
+ return obj;
81
+ };
82
+ }
83
+ };
84
+ var VBase = class {
85
+ constructor(options) {
86
+ this.baseKeys = [];
87
+ this._nested = () => this.baseKeys.length ? true : false;
88
+ this.addSanitizer = (sanitizer2) => {
89
+ this.sanitizers.push(sanitizer2);
90
+ return this;
91
+ };
92
+ this.message = (text) => {
93
+ const len = this.rules.length;
94
+ if (len >= 1) {
95
+ this.rules[len - 1].customMessage = text;
96
+ }
97
+ return this;
98
+ };
99
+ this.isRequired = () => {
100
+ return this.addRule("isRequired", (value) => {
101
+ if (value !== void 0 && value !== null && value !== "")
102
+ return true;
103
+ return false;
104
+ });
105
+ };
106
+ this.isOptional = () => {
107
+ this._optional = true;
108
+ return this.addRule("isOptional", () => true);
109
+ };
110
+ this.isEqual = (comparison) => {
111
+ return this.addRule("isEqual", (value) => {
112
+ return value === comparison;
113
+ });
114
+ };
115
+ this.asNumber = () => {
116
+ const newVNumber = new VNumber({ ...this, type: "number" });
117
+ if (this.isArray)
118
+ return newVNumber.asArray();
119
+ return newVNumber;
120
+ };
121
+ this.asBoolean = () => {
122
+ const newVBoolean = new VBoolean({ ...this, type: "boolean" });
123
+ if (this.isArray)
124
+ return newVBoolean.asArray();
125
+ return newVBoolean;
126
+ };
127
+ this.validate = async (req) => {
128
+ let value = void 0;
129
+ let jsonData = void 0;
130
+ if (this.target === "query") {
131
+ value = req.query(this.key);
132
+ }
133
+ if (this.target === "queries") {
134
+ value = req.queries(this.key);
135
+ }
136
+ if (this.target === "header") {
137
+ value = req.header(this.key);
138
+ }
139
+ if (this.target === "body") {
140
+ const body = await req.parseBody();
141
+ value = body[this.key];
142
+ }
143
+ if (this.target === "json") {
144
+ if (this._nested()) {
145
+ this.key = `${this.baseKeys.join(".")}.${this.key}`;
146
+ }
147
+ let obj = {};
148
+ try {
149
+ obj = await req.json();
150
+ } catch (e) {
151
+ throw new Error("Malformed JSON in request body");
152
+ }
153
+ const dst = {};
154
+ value = JSONPathCopy(obj, dst, this.key);
155
+ if (this._nested())
156
+ jsonData = dst;
157
+ }
158
+ const results = [];
159
+ let typeRule = this.rules.shift();
160
+ for (const rule2 of this.rules) {
161
+ if (rule2.type === "type") {
162
+ typeRule = rule2;
163
+ } else if (rule2.type === "value") {
164
+ const result = this.validateRule(rule2, value);
165
+ result.jsonData || (result.jsonData = jsonData);
166
+ results.push(result);
167
+ }
168
+ }
169
+ if (typeRule) {
170
+ const typeResult = this.validateRule(typeRule, value);
171
+ typeResult.jsonData || (typeResult.jsonData = jsonData);
172
+ results.unshift(typeResult);
173
+ this.rules.unshift(typeRule);
174
+ }
175
+ return results;
176
+ };
177
+ this.validateType = (value) => {
178
+ if (this.isArray) {
179
+ if (!Array.isArray(value)) {
180
+ return false;
181
+ }
182
+ for (const val of value) {
183
+ if (typeof val === "undefined" && this._nested()) {
184
+ value.pop();
185
+ }
186
+ for (const val2 of value) {
187
+ if (typeof val2 !== this.type) {
188
+ if (!this._optional || typeof val2 !== "undefined")
106
189
  return false;
107
- });
108
- };
109
- this.isOptional = () => {
110
- this._optional = true;
111
- return this.addRule('isOptional', () => true);
112
- };
113
- this.isEqual = (comparison) => {
114
- return this.addRule('isEqual', (value) => {
115
- return value === comparison;
116
- });
117
- };
118
- this.asNumber = () => {
119
- const newVNumber = new VNumber({ ...this, type: 'number' });
120
- if (this.isArray)
121
- return newVNumber.asArray();
122
- return newVNumber;
123
- };
124
- this.asBoolean = () => {
125
- const newVBoolean = new VBoolean({ ...this, type: 'boolean' });
126
- if (this.isArray)
127
- return newVBoolean.asArray();
128
- return newVBoolean;
129
- };
130
- this.validate = async (req) => {
131
- let value = undefined;
132
- let jsonData = undefined;
133
- if (this.target === 'query') {
134
- value = req.query(this.key);
135
- }
136
- if (this.target === 'header') {
137
- value = req.header(this.key);
138
- }
139
- if (this.target === 'body') {
140
- const body = await req.parseBody();
141
- value = body[this.key];
142
- }
143
- if (this.target === 'json') {
144
- if (this._nested()) {
145
- this.key = `${this.baseKeys.join('.')}.${this.key}`;
146
- }
147
- let obj = {};
148
- try {
149
- obj = (await req.json());
150
- }
151
- catch (e) {
152
- throw new Error('Malformed JSON in request body');
153
- }
154
- const dst = {};
155
- value = JSONPathCopy(obj, dst, this.key);
156
- if (this._nested())
157
- jsonData = dst;
158
190
  }
159
- const results = [];
160
- let typeRule = this.rules.shift();
161
- for (const rule of this.rules) {
162
- if (rule.type === 'type') {
163
- typeRule = rule;
164
- }
165
- else if (rule.type === 'value') {
166
- const result = this.validateRule(rule, value);
167
- result.jsonData || (result.jsonData = jsonData);
168
- results.push(result);
169
- }
170
- }
171
- if (typeRule) {
172
- const typeResult = this.validateRule(typeRule, value);
173
- typeResult.jsonData || (typeResult.jsonData = jsonData);
174
- results.unshift(typeResult);
175
- this.rules.unshift(typeRule);
176
- }
177
- return results;
178
- };
179
- this.validateType = (value) => {
180
- if (this.isArray) {
181
- if (!Array.isArray(value)) {
182
- return false;
183
- }
184
- for (const val of value) {
185
- if (typeof val === 'undefined' && this._nested()) {
186
- value.pop();
187
- }
188
- for (const val of value) {
189
- if (typeof val !== this.type) {
190
- // Value is of wrong type here
191
- // If it is not optional and not undefined, return false
192
- if (!this._optional || typeof val !== 'undefined')
193
- return false;
194
- }
195
- }
196
- }
197
- }
198
- else {
199
- if (typeof value !== this.type) {
200
- if (this._optional && (typeof value === 'undefined' || Array.isArray(value))) {
201
- // Do nothing.
202
- // If it is optional it's OK to be `undefined` or Array
203
- }
204
- else {
205
- return false;
206
- }
207
- }
208
- }
209
- return true;
210
- };
211
- this.validateValue = (func, value) => {
212
- if (this._optional && typeof value === 'undefined')
213
- return true;
214
- if (Array.isArray(value)) {
215
- // Sanitize
216
- for (const sanitizer of this.sanitizers) {
217
- value = value.map((innerVal) => sanitizer(innerVal));
218
- }
219
- for (const val of value) {
220
- if (!func(val)) {
221
- return false;
222
- }
223
- }
224
- return true;
225
- }
226
- else {
227
- // Sanitize
228
- for (const sanitizer of this.sanitizers) {
229
- value = sanitizer(value);
230
- }
231
- if (!func(value)) {
232
- return false;
233
- }
234
- return true;
235
- }
236
- };
237
- this.getMessage = (opts) => {
238
- let keyText;
239
- const valueText = Array.isArray(opts.value)
240
- ? `${opts.value
241
- .map((val) => val === undefined ? 'undefined' : typeof val === 'string' ? `"${val}"` : val)
242
- .join(', ')}`
243
- : opts.value;
244
- switch (this.target) {
245
- case 'query':
246
- keyText = `the query parameter "${this.key}"`;
247
- break;
248
- case 'header':
249
- keyText = `the request header "${this.key}"`;
250
- break;
251
- case 'body':
252
- keyText = `the request body "${this.key}"`;
253
- break;
254
- case 'json':
255
- keyText = `the JSON body "${this.key}"`;
256
- break;
257
- }
258
- return `Invalid Value [${valueText}]: ${keyText} is invalid - ${opts.ruleName}`;
259
- };
260
- this.target = options.target;
261
- this.key = options.key;
262
- this.type = options.type || 'string';
263
- this.rules = [
264
- {
265
- name: this.getTypeRuleName(),
266
- type: 'type',
267
- func: this.validateType,
268
- },
269
- ];
270
- this.sanitizers = [];
271
- this._optional = false;
272
- this.isArray = options.isArray || false;
273
- }
274
- addRule(arg, func) {
275
- if (typeof arg === 'string' && func) {
276
- this.rules.push({ name: arg, func, type: 'value' });
191
+ }
277
192
  }
278
- else if (arg instanceof Function) {
279
- this.rules.push({ name: arg.name, func: arg, type: 'value' });
193
+ } else {
194
+ if (typeof value !== this.type) {
195
+ if (this._optional && (typeof value === "undefined" || Array.isArray(value))) {
196
+ } else {
197
+ return false;
198
+ }
280
199
  }
281
- return this;
282
- }
283
- get(value) {
284
- const len = this.rules.length;
285
- if (len > 0) {
286
- this.rules[this.rules.length - 1].customMessage = value;
200
+ }
201
+ return true;
202
+ };
203
+ this.validateValue = (func, value) => {
204
+ if (this._optional && typeof value === "undefined")
205
+ return true;
206
+ if (Array.isArray(value)) {
207
+ if (value.length === 0 && !this._optional)
208
+ return false;
209
+ for (const sanitizer2 of this.sanitizers) {
210
+ value = value.map((innerVal) => sanitizer2(innerVal));
287
211
  }
288
- return this;
289
- }
290
- getTypeRuleName() {
291
- const prefix = 'should be';
292
- return this.isArray ? `${prefix} "${this.type}[]"` : `${prefix} "${this.type}"`;
293
- }
294
- validateRule(rule, value) {
295
- let isValid = false;
296
- if (this._nested() && this.target != 'json') {
297
- isValid = false;
212
+ for (const val of value) {
213
+ if (!func(val)) {
214
+ return false;
215
+ }
298
216
  }
299
- else if (rule.type === 'value') {
300
- isValid = this.validateValue(rule.func, value);
217
+ return true;
218
+ } else {
219
+ for (const sanitizer2 of this.sanitizers) {
220
+ value = sanitizer2(value);
301
221
  }
302
- else if (rule.type === 'type') {
303
- isValid = this.validateType(value);
222
+ if (!func(value)) {
223
+ return false;
304
224
  }
305
- const message = isValid
306
- ? undefined
307
- : rule.customMessage || this.getMessage({ ruleName: rule.name, value });
308
- const result = {
309
- isValid: isValid,
310
- message: message,
311
- target: this.target,
312
- key: this.key,
313
- value,
314
- ruleName: rule.name,
315
- ruleType: rule.type,
316
- };
317
- return result;
318
- }
319
- }
320
- export class VString extends VBase {
321
- constructor(options) {
322
- super(options);
323
- this.asArray = () => {
324
- return new VStringArray(this);
325
- };
326
- this.isEmpty = (options = { ignore_whitespace: false }) => {
327
- return this.addRule('isEmpty', (value) => rule.isEmpty(value, options));
328
- };
329
- this.isLength = (options, arg2) => {
330
- return this.addRule('isLength', (value) => rule.isLength(value, options, arg2));
331
- };
332
- this.isAlpha = () => {
333
- return this.addRule('isAlpha', (value) => rule.isAlpha(value));
334
- };
335
- this.isNumeric = () => {
336
- return this.addRule('isNumeric', (value) => rule.isNumeric(value));
337
- };
338
- this.contains = (elem, options = {
339
- ignoreCase: false,
340
- minOccurrences: 1,
341
- }) => {
342
- return this.addRule('contains', (value) => rule.contains(value, elem, options));
343
- };
344
- this.isIn = (options) => {
345
- return this.addRule('isIn', (value) => rule.isIn(value, options));
346
- };
347
- this.match = (regExp) => {
348
- return this.addRule('match', (value) => rule.match(value, regExp));
349
- };
350
- this.trim = () => {
351
- return this.addSanitizer((value) => sanitizer.trim(value));
352
- };
353
- this.type = 'string';
354
- }
355
- }
356
- export class VNumber extends VBase {
357
- constructor(options) {
358
- super(options);
359
- this.asArray = () => {
360
- return new VNumberArray(this);
361
- };
362
- this.isGte = (min) => {
363
- return this.addRule('isGte', (value) => rule.isGte(value, min));
364
- };
365
- this.isLte = (min) => {
366
- return this.addRule('isLte', (value) => rule.isLte(value, min));
367
- };
368
- this.type = 'number';
369
- }
370
- }
371
- export class VBoolean extends VBase {
372
- constructor(options) {
373
- super(options);
374
- this.asArray = () => {
375
- return new VBooleanArray(this);
376
- };
377
- this.isTrue = () => {
378
- return this.addRule('isTrue', (value) => rule.isTrue(value));
379
- };
380
- this.isFalse = () => {
381
- return this.addRule('isFalse', (value) => rule.isFalse(value));
382
- };
383
- this.type = 'boolean';
384
- }
385
- }
386
- export class VNumberArray extends VNumber {
387
- constructor(options) {
388
- super(options);
389
- this.isArray = true;
390
- this.rules[0].name = this.getTypeRuleName();
225
+ return true;
226
+ }
227
+ };
228
+ this.getMessage = (opts) => {
229
+ let keyText;
230
+ const valueText = Array.isArray(opts.value) ? `${opts.value.map(
231
+ (val) => val === void 0 ? "undefined" : typeof val === "string" ? `"${val}"` : val
232
+ ).join(", ")}` : opts.value;
233
+ switch (this.target) {
234
+ case "query":
235
+ keyText = `the query parameter "${this.key}"`;
236
+ break;
237
+ case "queries":
238
+ keyText = `the query parameters "${this.key}"`;
239
+ break;
240
+ case "header":
241
+ keyText = `the request header "${this.key}"`;
242
+ break;
243
+ case "body":
244
+ keyText = `the request body "${this.key}"`;
245
+ break;
246
+ case "json":
247
+ keyText = `the JSON body "${this.key}"`;
248
+ break;
249
+ }
250
+ return `Invalid Value [${valueText}]: ${keyText} is invalid - ${opts.ruleName}`;
251
+ };
252
+ this.target = options.target;
253
+ this.key = options.key;
254
+ this.type = options.type || "string";
255
+ this.rules = [
256
+ {
257
+ name: this.getTypeRuleName(),
258
+ type: "type",
259
+ func: this.validateType
260
+ }
261
+ ];
262
+ this.sanitizers = [];
263
+ this._optional = false;
264
+ this.isArray = options.isArray || false;
265
+ }
266
+ addRule(arg, func) {
267
+ if (typeof arg === "string" && func) {
268
+ this.rules.push({ name: arg, func, type: "value" });
269
+ } else if (arg instanceof Function) {
270
+ this.rules.push({ name: arg.name, func: arg, type: "value" });
391
271
  }
392
- }
393
- export class VStringArray extends VString {
394
- constructor(options) {
395
- super(options);
396
- this.isArray = true;
397
- this.rules[0].name = this.getTypeRuleName();
272
+ return this;
273
+ }
274
+ get(value) {
275
+ const len = this.rules.length;
276
+ if (len > 0) {
277
+ this.rules[this.rules.length - 1].customMessage = value;
398
278
  }
399
- }
400
- export class VBooleanArray extends VBoolean {
401
- constructor(options) {
402
- super(options);
403
- this.isArray = true;
404
- this.rules[0].name = this.getTypeRuleName();
279
+ return this;
280
+ }
281
+ getTypeRuleName() {
282
+ const prefix = "should be";
283
+ return this.isArray ? `${prefix} "${this.type}[]"` : `${prefix} "${this.type}"`;
284
+ }
285
+ validateRule(rule2, value) {
286
+ let isValid = false;
287
+ if (this._nested() && this.target != "json") {
288
+ isValid = false;
289
+ } else if (rule2.type === "value") {
290
+ isValid = this.validateValue(rule2.func, value);
291
+ } else if (rule2.type === "type") {
292
+ isValid = this.validateType(value);
405
293
  }
406
- }
294
+ const message = isValid ? void 0 : rule2.customMessage || this.getMessage({ ruleName: rule2.name, value });
295
+ const result = {
296
+ isValid,
297
+ message,
298
+ target: this.target,
299
+ key: this.key,
300
+ value,
301
+ ruleName: rule2.name,
302
+ ruleType: rule2.type
303
+ };
304
+ return result;
305
+ }
306
+ };
307
+ var VString = class extends VBase {
308
+ constructor(options) {
309
+ super(options);
310
+ this.asArray = () => {
311
+ return new VStringArray(this);
312
+ };
313
+ this.isEmpty = (options = { ignore_whitespace: false }) => {
314
+ return this.addRule("isEmpty", (value) => rule.isEmpty(value, options));
315
+ };
316
+ this.isLength = (options, arg2) => {
317
+ return this.addRule("isLength", (value) => rule.isLength(value, options, arg2));
318
+ };
319
+ this.isAlpha = () => {
320
+ return this.addRule("isAlpha", (value) => rule.isAlpha(value));
321
+ };
322
+ this.isNumeric = () => {
323
+ return this.addRule("isNumeric", (value) => rule.isNumeric(value));
324
+ };
325
+ this.contains = (elem, options = {
326
+ ignoreCase: false,
327
+ minOccurrences: 1
328
+ }) => {
329
+ return this.addRule("contains", (value) => rule.contains(value, elem, options));
330
+ };
331
+ this.isIn = (options) => {
332
+ return this.addRule("isIn", (value) => rule.isIn(value, options));
333
+ };
334
+ this.match = (regExp) => {
335
+ return this.addRule("match", (value) => rule.match(value, regExp));
336
+ };
337
+ this.trim = () => {
338
+ return this.addSanitizer((value) => sanitizer.trim(value));
339
+ };
340
+ this.type = "string";
341
+ }
342
+ };
343
+ var VNumber = class extends VBase {
344
+ constructor(options) {
345
+ super(options);
346
+ this.asArray = () => {
347
+ return new VNumberArray(this);
348
+ };
349
+ this.isGte = (min) => {
350
+ return this.addRule("isGte", (value) => rule.isGte(value, min));
351
+ };
352
+ this.isLte = (min) => {
353
+ return this.addRule("isLte", (value) => rule.isLte(value, min));
354
+ };
355
+ this.type = "number";
356
+ }
357
+ };
358
+ var VBoolean = class extends VBase {
359
+ constructor(options) {
360
+ super(options);
361
+ this.asArray = () => {
362
+ return new VBooleanArray(this);
363
+ };
364
+ this.isTrue = () => {
365
+ return this.addRule("isTrue", (value) => rule.isTrue(value));
366
+ };
367
+ this.isFalse = () => {
368
+ return this.addRule("isFalse", (value) => rule.isFalse(value));
369
+ };
370
+ this.type = "boolean";
371
+ }
372
+ };
373
+ var VNumberArray = class extends VNumber {
374
+ constructor(options) {
375
+ super(options);
376
+ this.isArray = true;
377
+ this.rules[0].name = this.getTypeRuleName();
378
+ }
379
+ };
380
+ var VStringArray = class extends VString {
381
+ constructor(options) {
382
+ super(options);
383
+ this.isArray = true;
384
+ this.rules[0].name = this.getTypeRuleName();
385
+ }
386
+ };
387
+ var VBooleanArray = class extends VBoolean {
388
+ constructor(options) {
389
+ super(options);
390
+ this.isArray = true;
391
+ this.rules[0].name = this.getTypeRuleName();
392
+ }
393
+ };
394
+ export {
395
+ VArray,
396
+ VBase,
397
+ VBoolean,
398
+ VBooleanArray,
399
+ VNumber,
400
+ VNumberArray,
401
+ VObject,
402
+ VObjectBase,
403
+ VString,
404
+ VStringArray,
405
+ Validator
406
+ };