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