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