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.
- package/dist/cjs/compose.js +82 -66
- package/dist/cjs/context.js +159 -144
- package/dist/cjs/hono.js +173 -150
- package/dist/cjs/index.js +31 -11
- package/dist/cjs/middleware/basic-auth/index.js +70 -39
- package/dist/cjs/middleware/bearer-auth/index.js +69 -51
- package/dist/cjs/middleware/cache/index.js +49 -28
- package/dist/cjs/middleware/compress/index.js +39 -16
- package/dist/cjs/middleware/cors/index.js +89 -71
- package/dist/cjs/middleware/etag/index.js +46 -24
- package/dist/cjs/middleware/html/index.js +50 -30
- package/dist/cjs/middleware/jsx/index.js +178 -166
- package/dist/cjs/middleware/jsx/jsx-dev-runtime.js +30 -7
- package/dist/cjs/middleware/jsx/jsx-runtime.js +30 -6
- package/dist/cjs/middleware/jwt/index.js +74 -54
- package/dist/cjs/middleware/logger/index.js +59 -38
- package/dist/cjs/middleware/powered-by/index.js +30 -7
- package/dist/cjs/middleware/pretty-json/index.js +31 -8
- package/dist/cjs/middleware/serve-static/bun.js +56 -38
- package/dist/cjs/middleware/serve-static/index.js +27 -4
- package/dist/cjs/middleware/serve-static/module.js +42 -0
- package/dist/cjs/middleware/serve-static/serve-static.js +58 -39
- package/dist/cjs/middleware/validator/index.js +27 -4
- package/dist/cjs/middleware/validator/middleware.js +125 -91
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/request.js +129 -116
- package/dist/cjs/router/reg-exp-router/index.js +27 -4
- package/dist/cjs/router/reg-exp-router/node.js +108 -97
- package/dist/cjs/router/reg-exp-router/router.js +158 -141
- package/dist/cjs/router/reg-exp-router/trie.js +55 -39
- package/dist/cjs/router/smart-router/index.js +27 -4
- package/dist/cjs/router/smart-router/router.js +70 -49
- package/dist/cjs/router/static-router/index.js +27 -4
- package/dist/cjs/router/static-router/router.js +78 -64
- package/dist/cjs/router/trie-router/index.js +27 -4
- package/dist/cjs/router/trie-router/node.js +167 -159
- package/dist/cjs/router/trie-router/router.js +43 -20
- package/dist/cjs/router.js +35 -6
- package/dist/cjs/types.js +16 -0
- package/dist/cjs/utils/body.js +36 -15
- package/dist/cjs/utils/buffer.js +56 -31
- package/dist/cjs/utils/cloudflare.js +53 -34
- package/dist/cjs/utils/cookie.js +59 -35
- package/dist/cjs/utils/crypto.js +69 -43
- package/dist/cjs/utils/encode.js +92 -65
- package/dist/cjs/utils/filepath.js +39 -22
- package/dist/cjs/utils/html.js +54 -33
- package/dist/cjs/utils/http-status.js +68 -45
- package/dist/cjs/utils/json.js +91 -76
- package/dist/cjs/utils/jwt/index.js +32 -25
- package/dist/cjs/utils/jwt/jwt.js +124 -93
- package/dist/cjs/utils/jwt/types.js +65 -38
- package/dist/cjs/utils/mime.js +110 -87
- package/dist/cjs/utils/object.js +53 -35
- package/dist/cjs/utils/types.js +15 -1
- package/dist/cjs/utils/url.js +89 -78
- package/dist/cjs/validator/rule.js +78 -64
- package/dist/cjs/validator/sanitizer.js +28 -4
- package/dist/cjs/validator/schema.js +15 -1
- package/dist/cjs/validator/validator.js +405 -386
- package/dist/compose.d.ts +1 -1
- package/dist/compose.js +61 -64
- package/dist/context.d.ts +4 -4
- package/dist/context.js +139 -143
- package/dist/hono.d.ts +1 -17
- package/dist/hono.js +153 -149
- package/dist/index.d.ts +3 -1
- package/dist/index.js +9 -9
- package/dist/middleware/basic-auth/index.d.ts +1 -1
- package/dist/middleware/basic-auth/index.js +52 -40
- package/dist/middleware/bearer-auth/index.d.ts +1 -1
- package/dist/middleware/bearer-auth/index.js +48 -49
- package/dist/middleware/cache/index.d.ts +1 -1
- package/dist/middleware/cache/index.js +28 -26
- package/dist/middleware/compress/index.d.ts +1 -1
- package/dist/middleware/compress/index.js +18 -14
- package/dist/middleware/cors/index.d.ts +1 -1
- package/dist/middleware/cors/index.js +68 -69
- package/dist/middleware/etag/index.d.ts +1 -1
- package/dist/middleware/etag/index.js +25 -22
- package/dist/middleware/html/index.js +29 -28
- package/dist/middleware/jsx/index.js +164 -172
- package/dist/middleware/jsx/jsx-dev-runtime.js +9 -5
- package/dist/middleware/jsx/jsx-runtime.js +7 -2
- package/dist/middleware/jwt/index.d.ts +1 -1
- package/dist/middleware/jwt/index.js +53 -52
- package/dist/middleware/logger/index.d.ts +1 -1
- package/dist/middleware/logger/index.js +35 -39
- package/dist/middleware/powered-by/index.d.ts +1 -1
- package/dist/middleware/powered-by/index.js +9 -5
- package/dist/middleware/pretty-json/index.d.ts +1 -1
- package/dist/middleware/pretty-json/index.js +10 -6
- package/dist/middleware/serve-static/bun.d.ts +1 -1
- package/dist/middleware/serve-static/bun.js +35 -36
- package/dist/middleware/serve-static/index.js +5 -1
- package/dist/middleware/serve-static/{module.d.mts → module.d.ts} +1 -1
- package/dist/middleware/serve-static/module.js +13 -0
- package/dist/middleware/serve-static/serve-static.d.ts +1 -1
- package/dist/middleware/serve-static/serve-static.js +36 -36
- package/dist/middleware/validator/index.js +5 -2
- package/dist/middleware/validator/middleware.d.ts +1 -1
- package/dist/middleware/validator/middleware.js +104 -89
- package/dist/request.js +108 -114
- package/dist/router/reg-exp-router/index.js +5 -1
- package/dist/router/reg-exp-router/node.js +87 -96
- package/dist/router/reg-exp-router/router.js +140 -142
- package/dist/router/reg-exp-router/trie.js +35 -38
- package/dist/router/smart-router/index.js +5 -1
- package/dist/router/smart-router/router.js +50 -48
- package/dist/router/static-router/index.js +5 -1
- package/dist/router/static-router/router.js +58 -63
- package/dist/router/trie-router/index.js +5 -1
- package/dist/router/trie-router/node.js +147 -158
- package/dist/router/trie-router/router.js +23 -19
- package/dist/router.js +12 -5
- package/dist/types.d.ts +22 -0
- package/dist/types.js +0 -0
- package/dist/utils/body.js +15 -13
- package/dist/utils/buffer.js +35 -29
- package/dist/utils/cloudflare.js +32 -32
- package/dist/utils/cookie.js +38 -33
- package/dist/utils/crypto.js +48 -41
- package/dist/utils/encode.js +70 -62
- package/dist/utils/filepath.js +18 -20
- package/dist/utils/html.js +34 -32
- package/dist/utils/http-status.js +48 -44
- package/dist/utils/json.js +71 -75
- package/dist/utils/jwt/index.js +5 -1
- package/dist/utils/jwt/jwt.js +95 -90
- package/dist/utils/jwt/types.js +47 -41
- package/dist/utils/mime.js +90 -86
- package/dist/utils/object.js +31 -32
- package/dist/utils/types.js +0 -1
- package/dist/utils/url.js +69 -77
- package/dist/validator/rule.js +56 -62
- package/dist/validator/sanitizer.js +6 -2
- package/dist/validator/schema.js +0 -1
- package/dist/validator/validator.d.ts +5 -4
- package/dist/validator/validator.js +393 -393
- package/package.json +14 -8
- package/dist/middleware/serve-static/module.mjs +0 -13
|
@@ -1,420 +1,439 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
78
|
+
constructor(container, key) {
|
|
79
|
+
super(container, key);
|
|
80
|
+
}
|
|
56
81
|
}
|
|
57
|
-
exports.VObject = VObject;
|
|
58
82
|
class VArray extends VObjectBase {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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
|
-
|
|
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
|
-
|
|
286
|
-
|
|
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
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
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
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
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
|
-
|
|
307
|
-
|
|
249
|
+
return true;
|
|
250
|
+
} else {
|
|
251
|
+
for (const sanitizer2 of this.sanitizers) {
|
|
252
|
+
value = sanitizer2(value);
|
|
308
253
|
}
|
|
309
|
-
|
|
310
|
-
|
|
254
|
+
if (!func(value)) {
|
|
255
|
+
return false;
|
|
311
256
|
}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
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
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
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
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
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
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
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
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
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
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
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
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
420
|
+
constructor(options) {
|
|
421
|
+
super(options);
|
|
422
|
+
this.isArray = true;
|
|
423
|
+
this.rules[0].name = this.getTypeRuleName();
|
|
424
|
+
}
|
|
419
425
|
}
|
|
420
|
-
|
|
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
|
+
});
|