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,406 +1,406 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
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
|
-
|
|
279
|
-
|
|
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
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
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
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
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
|
-
|
|
300
|
-
|
|
217
|
+
return true;
|
|
218
|
+
} else {
|
|
219
|
+
for (const sanitizer2 of this.sanitizers) {
|
|
220
|
+
value = sanitizer2(value);
|
|
301
221
|
}
|
|
302
|
-
|
|
303
|
-
|
|
222
|
+
if (!func(value)) {
|
|
223
|
+
return false;
|
|
304
224
|
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
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
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
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
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
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
|
+
};
|