@warlock.js/core 2.0.5 → 2.1.1
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/cjs/application.js +1 -1
- package/cjs/application.js.map +1 -1
- package/cjs/config/types.d.ts +8 -1
- package/cjs/config/types.d.ts.map +1 -1
- package/cjs/console/commander.js +1 -1
- package/cjs/console/commander.js.map +1 -1
- package/cjs/http/request.d.ts +4 -0
- package/cjs/http/request.d.ts.map +1 -1
- package/cjs/http/request.js +7 -1
- package/cjs/http/request.js.map +1 -1
- package/cjs/http/server.d.ts.map +1 -1
- package/cjs/http/server.js +3 -1
- package/cjs/http/server.js.map +1 -1
- package/cjs/index.d.ts +1 -2
- package/cjs/index.d.ts.map +1 -1
- package/cjs/index.js +1 -1
- package/cjs/mail/sendMail.d.ts.map +1 -1
- package/cjs/mail/sendMail.js +0 -8
- package/cjs/mail/sendMail.js.map +1 -1
- package/cjs/modules/uploads/controllers/upload-files.d.ts +2 -12
- package/cjs/modules/uploads/controllers/upload-files.d.ts.map +1 -1
- package/cjs/modules/uploads/controllers/upload-files.js +13 -18
- package/cjs/modules/uploads/controllers/upload-files.js.map +1 -1
- package/cjs/modules/uploads/events/compress-uploaded-file.d.ts.map +1 -1
- package/cjs/modules/uploads/events/compress-uploaded-file.js +2 -0
- package/cjs/modules/uploads/events/compress-uploaded-file.js.map +1 -1
- package/cjs/modules/uploads/utils/get-watermark-options.js +1 -1
- package/cjs/modules/uploads/utils/get-watermark-options.js.map +1 -1
- package/cjs/output/output.d.ts +11 -6
- package/cjs/output/output.d.ts.map +1 -1
- package/cjs/output/output.js +35 -12
- package/cjs/output/output.js.map +1 -1
- package/cjs/output/types.d.ts +7 -8
- package/cjs/output/types.d.ts.map +1 -1
- package/cjs/repositories/repository-filler-manager.d.ts.map +1 -1
- package/cjs/repositories/repository-filler-manager.js +1 -7
- package/cjs/repositories/repository-filler-manager.js.map +1 -1
- package/cjs/repositories/repository-list-manager.d.ts.map +1 -1
- package/cjs/repositories/repository-list-manager.js +0 -2
- package/cjs/repositories/repository-list-manager.js.map +1 -1
- package/cjs/restful/utils.js +5 -5
- package/cjs/restful/utils.js.map +1 -1
- package/cjs/router/router.d.ts +31 -2
- package/cjs/router/router.d.ts.map +1 -1
- package/cjs/router/router.js +66 -3
- package/cjs/router/router.js.map +1 -1
- package/cjs/swagger/postman-to-swagger.d.ts +23 -1
- package/cjs/swagger/postman-to-swagger.d.ts.map +1 -1
- package/cjs/validator/locales.js +12 -4
- package/cjs/validator/locales.js.map +1 -1
- package/cjs/validator/rules/file.d.ts.map +1 -1
- package/cjs/validator/rules/file.js +0 -1
- package/cjs/validator/rules/file.js.map +1 -1
- package/cjs/validator/v/mutators.d.ts +1 -0
- package/cjs/validator/v/mutators.d.ts.map +1 -1
- package/cjs/validator/v/mutators.js +4 -1
- package/cjs/validator/v/mutators.js.map +1 -1
- package/cjs/validator/v/rules.d.ts +205 -49
- package/cjs/validator/v/rules.d.ts.map +1 -1
- package/cjs/validator/v/rules.js +510 -15
- package/cjs/validator/v/rules.js.map +1 -1
- package/cjs/validator/v/schema.d.ts +414 -28
- package/cjs/validator/v/schema.d.ts.map +1 -1
- package/cjs/validator/v/schema.js +586 -53
- package/cjs/validator/v/schema.js.map +1 -1
- package/cjs/validator/v/types.d.ts +28 -4
- package/cjs/validator/v/types.d.ts.map +1 -1
- package/cjs/validator/validateAll.d.ts.map +1 -1
- package/cjs/validator/validateAll.js +1 -2
- package/cjs/validator/validateAll.js.map +1 -1
- package/esm/application.js +1 -1
- package/esm/application.js.map +1 -1
- package/esm/config/types.d.ts +8 -1
- package/esm/config/types.d.ts.map +1 -1
- package/esm/console/commander.js +1 -1
- package/esm/console/commander.js.map +1 -1
- package/esm/http/request.d.ts +4 -0
- package/esm/http/request.d.ts.map +1 -1
- package/esm/http/request.js +7 -1
- package/esm/http/request.js.map +1 -1
- package/esm/http/server.d.ts.map +1 -1
- package/esm/http/server.js +3 -1
- package/esm/http/server.js.map +1 -1
- package/esm/index.d.ts +1 -2
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +1 -1
- package/esm/mail/sendMail.d.ts.map +1 -1
- package/esm/mail/sendMail.js +0 -8
- package/esm/mail/sendMail.js.map +1 -1
- package/esm/modules/uploads/controllers/upload-files.d.ts +2 -12
- package/esm/modules/uploads/controllers/upload-files.d.ts.map +1 -1
- package/esm/modules/uploads/controllers/upload-files.js +13 -18
- package/esm/modules/uploads/controllers/upload-files.js.map +1 -1
- package/esm/modules/uploads/events/compress-uploaded-file.d.ts.map +1 -1
- package/esm/modules/uploads/events/compress-uploaded-file.js +2 -0
- package/esm/modules/uploads/events/compress-uploaded-file.js.map +1 -1
- package/esm/modules/uploads/utils/get-watermark-options.js +1 -1
- package/esm/modules/uploads/utils/get-watermark-options.js.map +1 -1
- package/esm/output/output.d.ts +11 -6
- package/esm/output/output.d.ts.map +1 -1
- package/esm/output/output.js +35 -12
- package/esm/output/output.js.map +1 -1
- package/esm/output/types.d.ts +7 -8
- package/esm/output/types.d.ts.map +1 -1
- package/esm/repositories/repository-filler-manager.d.ts.map +1 -1
- package/esm/repositories/repository-filler-manager.js +1 -7
- package/esm/repositories/repository-filler-manager.js.map +1 -1
- package/esm/repositories/repository-list-manager.d.ts.map +1 -1
- package/esm/repositories/repository-list-manager.js +0 -2
- package/esm/repositories/repository-list-manager.js.map +1 -1
- package/esm/restful/utils.js +5 -5
- package/esm/restful/utils.js.map +1 -1
- package/esm/router/router.d.ts +31 -2
- package/esm/router/router.d.ts.map +1 -1
- package/esm/router/router.js +66 -3
- package/esm/router/router.js.map +1 -1
- package/esm/swagger/postman-to-swagger.d.ts +23 -1
- package/esm/swagger/postman-to-swagger.d.ts.map +1 -1
- package/esm/validator/locales.js +12 -4
- package/esm/validator/locales.js.map +1 -1
- package/esm/validator/rules/file.d.ts.map +1 -1
- package/esm/validator/rules/file.js +0 -1
- package/esm/validator/rules/file.js.map +1 -1
- package/esm/validator/v/mutators.d.ts +1 -0
- package/esm/validator/v/mutators.d.ts.map +1 -1
- package/esm/validator/v/mutators.js +5 -2
- package/esm/validator/v/mutators.js.map +1 -1
- package/esm/validator/v/rules.d.ts +205 -49
- package/esm/validator/v/rules.d.ts.map +1 -1
- package/esm/validator/v/rules.js +510 -15
- package/esm/validator/v/rules.js.map +1 -1
- package/esm/validator/v/schema.d.ts +414 -28
- package/esm/validator/v/schema.d.ts.map +1 -1
- package/esm/validator/v/schema.js +586 -53
- package/esm/validator/v/schema.js.map +1 -1
- package/esm/validator/v/types.d.ts +28 -4
- package/esm/validator/v/types.d.ts.map +1 -1
- package/esm/validator/validateAll.d.ts.map +1 -1
- package/esm/validator/validateAll.js +1 -2
- package/esm/validator/validateAll.js.map +1 -1
- package/package.json +3 -3
|
@@ -1,7 +1,28 @@
|
|
|
1
|
-
import {trans}from'@mongez/localization';import {clone}from'@mongez/reinforcements';import {isPlainObject}from'@mongez/supportive-is';import {stripUnknownMutator,objectTrimMutator,flipArrayMutator,uniqueArrayMutator,sortArrayMutator,stringMutator,lowercaseMutator,uppercaseMutator,dateMutator,numberMutator}from'./mutators.js';import {requiredRule,requiredWithRule,requiredIfAbsentRule,requiredIfEmptyRule,requiredIfFieldRule,objectRule,unknownKeyRule,arrayRule,minLengthRule,maxLengthRule,lengthRule,uniqueArrayRule,stringRule,emailRule,urlRule,matchesRule,patternRule,
|
|
1
|
+
import {trans}from'@mongez/localization';import {clone}from'@mongez/reinforcements';import {isPlainObject}from'@mongez/supportive-is';import {stripUnknownMutator,objectTrimMutator,flipArrayMutator,uniqueArrayMutator,sortArrayMutator,stringMutator,lowercaseMutator,uppercaseMutator,capitalizeMutator,dateMutator,numberMutator}from'./mutators.js';import {equalRule,requiredRule,requiredWithRule,requiredIfAbsentRule,requiredIfSiblingFieldAllAbsentRule,requiredIfEmptyRule,requiredIfSiblingFieldEmptyRule,requiredIfFieldRule,requiredIfSiblingFieldRule,requiredUnlessSiblingFieldRule,forbiddenRule,whenRule,objectRule,unknownKeyRule,arrayRule,minLengthRule,maxLengthRule,lengthRule,uniqueArrayRule,stringRule,emailRule,urlRule,matchesRule,withoutWhitespaceRule,patternRule,uploadableRule,wordsRule,minWordsRule,maxWordsRule,alphaRule,alphaNumericRule,isNumericRule,startsWithRule,endsWithRule,containsRule,notContainsRule,ipRule,ip4Rule,ip6Rule,isCreditCardRule,colorRule,hexColorRule,hslColorRule,rgbColorRule,rgbaColorRule,lightColorRule,darkColorRule,dateRule,maxDateRule,minDateRule,intRule,floatRule,booleanRule,scalarRule,uniqueRule,existsRule,enumRule,inRule,allowedValuesRule,notAllowedValuesRule,fileRule,imageRule,fileExtensionRule,fileTypeRule,minFileSizeRule,maxFileSizeRule,minWidthRule,maxWidthRule,minHeightRule,maxHeightRule,numberRule,minRule,maxRule,moduloRule,positiveRule}from'./rules.js';import {setKeyPath}from'./utils.js';/* eslint-disable @typescript-eslint/no-this-alias */
|
|
2
|
+
// TODO: Allow developer to extend the validator with custom rules and custom validators
|
|
3
|
+
class BaseValidator {
|
|
2
4
|
rules = [];
|
|
3
5
|
mutators = [];
|
|
4
6
|
defaultValue;
|
|
7
|
+
description;
|
|
8
|
+
/**
|
|
9
|
+
* Add description to the validator
|
|
10
|
+
*/
|
|
11
|
+
describe(description) {
|
|
12
|
+
this.description = description;
|
|
13
|
+
return this;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Value must be equal to the given value
|
|
17
|
+
*/
|
|
18
|
+
equal(value, errorMessage) {
|
|
19
|
+
const rule = this.addRule(equalRule, errorMessage);
|
|
20
|
+
rule.context.options.value = value;
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Add rule to the schema
|
|
25
|
+
*/
|
|
5
26
|
addRule(rule, errorMessage) {
|
|
6
27
|
const newRule = {
|
|
7
28
|
...clone(rule),
|
|
@@ -19,6 +40,18 @@ import {trans}from'@mongez/localization';import {clone}from'@mongez/reinforcemen
|
|
|
19
40
|
this.rules.push(newRule);
|
|
20
41
|
return newRule;
|
|
21
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Define custom rule
|
|
45
|
+
*/
|
|
46
|
+
refine(rule) {
|
|
47
|
+
this.addRule(rule);
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Add mutator to the schema
|
|
52
|
+
*
|
|
53
|
+
* A mutator is a function that mutates the value of the field before validation
|
|
54
|
+
*/
|
|
22
55
|
addMutator(mutator, options = {}) {
|
|
23
56
|
this.mutators.push({
|
|
24
57
|
mutate: mutator,
|
|
@@ -29,39 +62,104 @@ import {trans}from'@mongez/localization';import {clone}from'@mongez/reinforcemen
|
|
|
29
62
|
});
|
|
30
63
|
return this;
|
|
31
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Set default value for the field
|
|
67
|
+
*/
|
|
32
68
|
default(value) {
|
|
33
69
|
this.defaultValue = value;
|
|
34
70
|
return this;
|
|
35
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* Value must be present but not necessarily has a value
|
|
74
|
+
*/
|
|
36
75
|
present(errorMessage) {
|
|
37
76
|
this.addRule(requiredRule, errorMessage);
|
|
38
77
|
return this;
|
|
39
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* This value must be present and has a value
|
|
81
|
+
*/
|
|
40
82
|
required(errorMessage) {
|
|
41
83
|
this.addRule(requiredRule, errorMessage);
|
|
42
84
|
return this;
|
|
43
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* This value must be present if the given input is present
|
|
88
|
+
*/
|
|
44
89
|
requiredWith(input, errorMessage) {
|
|
45
90
|
const rule = this.addRule(requiredWithRule, errorMessage);
|
|
46
|
-
rule.context.options.
|
|
91
|
+
rule.context.options.field = input;
|
|
47
92
|
return this;
|
|
48
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* Mark this field as required if the given input field is absent
|
|
96
|
+
*/
|
|
49
97
|
requiredIfAbsent(input, errorMessage) {
|
|
50
98
|
const rule = this.addRule(requiredIfAbsentRule, errorMessage);
|
|
51
|
-
rule.context.options.
|
|
99
|
+
rule.context.options.field = input;
|
|
100
|
+
return this;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Value is required if all given input fields in same parent context are absent
|
|
104
|
+
*/
|
|
105
|
+
requiredIfSiblingFieldAllAbsent(fields, errorMessage) {
|
|
106
|
+
const rule = this.addRule(requiredIfSiblingFieldAllAbsentRule, errorMessage);
|
|
107
|
+
rule.context.options.fields = fields;
|
|
52
108
|
return this;
|
|
53
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* @alias requiredIfAbsent
|
|
112
|
+
*/
|
|
113
|
+
requiredIfMissing(input, errorMessage) {
|
|
114
|
+
return this.requiredIfAbsent(input, errorMessage);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Value is required if and only if the given input field is empty
|
|
118
|
+
*/
|
|
54
119
|
requiredIfEmpty(input, errorMessage) {
|
|
55
120
|
const rule = this.addRule(requiredIfEmptyRule, errorMessage);
|
|
56
|
-
rule.context.options.
|
|
121
|
+
rule.context.options.field = input;
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Value is required if and only if the given input field in same parent context is empty
|
|
126
|
+
*/
|
|
127
|
+
requiredIfSiblingFieldEmpty(input, errorMessage) {
|
|
128
|
+
const rule = this.addRule(requiredIfSiblingFieldEmptyRule, errorMessage);
|
|
129
|
+
rule.context.options.field = input;
|
|
57
130
|
return this;
|
|
58
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* Value is required if and only if the given input field has the given value
|
|
134
|
+
*/
|
|
59
135
|
requiredIfField(input, value, errorMessage) {
|
|
60
136
|
const rule = this.addRule(requiredIfFieldRule, errorMessage);
|
|
61
137
|
rule.context.options.field = input;
|
|
62
138
|
rule.context.options.value = value;
|
|
63
139
|
return this;
|
|
64
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* Value is required if and only if the given input field in same parent context has the given value
|
|
143
|
+
*/
|
|
144
|
+
requiredIfSiblingField(input, value, errorMessage) {
|
|
145
|
+
const rule = this.addRule(requiredIfSiblingFieldRule, errorMessage);
|
|
146
|
+
rule.context.options.field = input;
|
|
147
|
+
rule.context.options.value = value;
|
|
148
|
+
return this;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Value is required unless the given input field in same parent context has the given value
|
|
152
|
+
*/
|
|
153
|
+
requiredUnlessSiblingField(input, value, errorMessage) {
|
|
154
|
+
const rule = this.addRule(requiredUnlessSiblingFieldRule, errorMessage);
|
|
155
|
+
rule.context.options.field = input;
|
|
156
|
+
rule.context.options.value = value;
|
|
157
|
+
return this;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Mutate the data
|
|
161
|
+
* Please note this method should not be called directly, as it is used internally by the `validate` method
|
|
162
|
+
*/
|
|
65
163
|
async mutate(data, context) {
|
|
66
164
|
let mutatedData = data;
|
|
67
165
|
for (const mutator of this.mutators) {
|
|
@@ -70,6 +168,39 @@ import {trans}from'@mongez/localization';import {clone}from'@mongez/reinforcemen
|
|
|
70
168
|
}
|
|
71
169
|
return mutatedData;
|
|
72
170
|
}
|
|
171
|
+
/**
|
|
172
|
+
* Value is forbidden to be present
|
|
173
|
+
*/
|
|
174
|
+
forbidden(errorMessage) {
|
|
175
|
+
this.addRule(forbiddenRule, errorMessage);
|
|
176
|
+
return this;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Apply conditional validation rules based on another field value
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```ts
|
|
183
|
+
* v.object({
|
|
184
|
+
* status: v.when("type", {
|
|
185
|
+
* is: {
|
|
186
|
+
* post: v.string().required().in(["active", "inactive"]),
|
|
187
|
+
* news: v.string().required().in(["published", "draft"]),
|
|
188
|
+
* },
|
|
189
|
+
* otherwise: v.forbidden(),
|
|
190
|
+
* }),
|
|
191
|
+
* })
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
when(field, options) {
|
|
195
|
+
const rule = this.addRule(whenRule);
|
|
196
|
+
rule.context.options.field = field;
|
|
197
|
+
rule.context.options.is = options.is;
|
|
198
|
+
rule.context.options.otherwise = options.otherwise;
|
|
199
|
+
return this;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Validate the data
|
|
203
|
+
*/
|
|
73
204
|
async validate(data, context) {
|
|
74
205
|
const mutatedData = await this.mutate(data ?? this.defaultValue, context);
|
|
75
206
|
const errors = [];
|
|
@@ -117,10 +248,16 @@ class ObjectValidator extends BaseValidator {
|
|
|
117
248
|
this.schema = schema;
|
|
118
249
|
this.addRule(objectRule, errorMessage);
|
|
119
250
|
}
|
|
251
|
+
/**
|
|
252
|
+
* Strip unknown keys from the data
|
|
253
|
+
*
|
|
254
|
+
* @mutate
|
|
255
|
+
*/
|
|
120
256
|
stripUnknown() {
|
|
257
|
+
const validator = this;
|
|
121
258
|
this.addMutator(stripUnknownMutator, {
|
|
122
259
|
get allowedKeys() {
|
|
123
|
-
return
|
|
260
|
+
return validator.allowedKeys;
|
|
124
261
|
},
|
|
125
262
|
});
|
|
126
263
|
return this;
|
|
@@ -139,15 +276,28 @@ class ObjectValidator extends BaseValidator {
|
|
|
139
276
|
this.addMutator(objectTrimMutator, { recursive });
|
|
140
277
|
return this;
|
|
141
278
|
}
|
|
279
|
+
/**
|
|
280
|
+
* Whether to allow unknown properties
|
|
281
|
+
*
|
|
282
|
+
* @default false
|
|
283
|
+
*/
|
|
142
284
|
allowUnknown(allow = true) {
|
|
143
285
|
this.shouldAllowUnknown = allow;
|
|
144
286
|
return this;
|
|
145
287
|
}
|
|
288
|
+
/**
|
|
289
|
+
* Mutate the data
|
|
290
|
+
*
|
|
291
|
+
* Please note this method should not be called directly, as it is used internally by the `validate` method
|
|
292
|
+
*/
|
|
146
293
|
mutate(data, context) {
|
|
147
294
|
if (!isPlainObject(data))
|
|
148
295
|
return data;
|
|
149
296
|
return super.mutate({ ...data }, context);
|
|
150
297
|
}
|
|
298
|
+
/**
|
|
299
|
+
* Validate the data
|
|
300
|
+
*/
|
|
151
301
|
async validate(data, context) {
|
|
152
302
|
context.schema = this.schema;
|
|
153
303
|
const mutatedData = await this.mutate(data, context);
|
|
@@ -186,29 +336,33 @@ class ObjectValidator extends BaseValidator {
|
|
|
186
336
|
};
|
|
187
337
|
}
|
|
188
338
|
}
|
|
189
|
-
class ArrayValidator extends
|
|
339
|
+
class ArrayValidator extends BaseValidator {
|
|
190
340
|
validator;
|
|
191
341
|
constructor(validator, errorMessage) {
|
|
192
342
|
super();
|
|
193
343
|
this.validator = validator;
|
|
194
344
|
this.addRule(arrayRule, errorMessage);
|
|
195
345
|
}
|
|
196
|
-
// Start of mutators
|
|
197
|
-
// Mutators methods should start with `m` prefix
|
|
198
346
|
/**
|
|
199
347
|
* Reverse array order
|
|
348
|
+
*
|
|
349
|
+
* @mutate
|
|
200
350
|
*/
|
|
201
351
|
flip() {
|
|
202
352
|
return this.addMutator(flipArrayMutator);
|
|
203
353
|
}
|
|
204
354
|
/**
|
|
205
355
|
* Reverse array order
|
|
356
|
+
*
|
|
357
|
+
* @mutate
|
|
206
358
|
*/
|
|
207
359
|
reverse() {
|
|
208
360
|
return this.addMutator(flipArrayMutator);
|
|
209
361
|
}
|
|
210
362
|
/**
|
|
211
363
|
* Make it has only unique values
|
|
364
|
+
*
|
|
365
|
+
* @mutate
|
|
212
366
|
*/
|
|
213
367
|
onlyUnique() {
|
|
214
368
|
return this.addMutator(uniqueArrayMutator);
|
|
@@ -217,6 +371,8 @@ class ArrayValidator extends AnyValidator {
|
|
|
217
371
|
* Sort array
|
|
218
372
|
*
|
|
219
373
|
* If key is passed, it will sort by the key value
|
|
374
|
+
*
|
|
375
|
+
* @mutate
|
|
220
376
|
* @supports dot notation
|
|
221
377
|
*/
|
|
222
378
|
sort(direction = "asc", key) {
|
|
@@ -225,16 +381,25 @@ class ArrayValidator extends AnyValidator {
|
|
|
225
381
|
}
|
|
226
382
|
// End of mutators
|
|
227
383
|
// Start of rules
|
|
384
|
+
/**
|
|
385
|
+
* Array length must be greater than the given length
|
|
386
|
+
*/
|
|
228
387
|
minLength(length, errorMessage) {
|
|
229
388
|
const rule = this.addRule(minLengthRule, errorMessage);
|
|
230
389
|
rule.context.options.minLength = length;
|
|
231
390
|
return this;
|
|
232
391
|
}
|
|
392
|
+
/**
|
|
393
|
+
* Array length must be less than the given length
|
|
394
|
+
*/
|
|
233
395
|
maxLength(length, errorMessage) {
|
|
234
396
|
const rule = this.addRule(maxLengthRule, errorMessage);
|
|
235
397
|
rule.context.options.maxLength = length;
|
|
236
398
|
return this;
|
|
237
399
|
}
|
|
400
|
+
/**
|
|
401
|
+
* Array length must be of the given length
|
|
402
|
+
*/
|
|
238
403
|
length(length, errorMessage) {
|
|
239
404
|
const rule = this.addRule(lengthRule, errorMessage);
|
|
240
405
|
rule.context.options.length = length;
|
|
@@ -247,13 +412,21 @@ class ArrayValidator extends AnyValidator {
|
|
|
247
412
|
this.addRule(uniqueArrayRule);
|
|
248
413
|
return this;
|
|
249
414
|
}
|
|
415
|
+
/**
|
|
416
|
+
* Mutate the data
|
|
417
|
+
*
|
|
418
|
+
* Please note this method should not be called directly, as it is used internally by the `validate` method
|
|
419
|
+
*/
|
|
250
420
|
mutate(data, context) {
|
|
251
421
|
if (!Array.isArray(data))
|
|
252
422
|
return data;
|
|
253
423
|
return super.mutate([...data], context);
|
|
254
424
|
}
|
|
425
|
+
/**
|
|
426
|
+
* Validate array
|
|
427
|
+
*/
|
|
255
428
|
async validate(data, context) {
|
|
256
|
-
const mutatedData = await this.mutate(data, context);
|
|
429
|
+
const mutatedData = (await this.mutate(data, context)) || [];
|
|
257
430
|
const result = await super.validate(data, context);
|
|
258
431
|
if (result.isValid === false)
|
|
259
432
|
return result;
|
|
@@ -289,83 +462,326 @@ class StringValidator extends BaseValidator {
|
|
|
289
462
|
this.addRule(stringRule, errorMessage);
|
|
290
463
|
this.addMutator(stringMutator);
|
|
291
464
|
}
|
|
465
|
+
/**
|
|
466
|
+
* Convert string to lowercase
|
|
467
|
+
*
|
|
468
|
+
* @mutate
|
|
469
|
+
*/
|
|
292
470
|
lowercase() {
|
|
293
471
|
this.addMutator(lowercaseMutator);
|
|
294
472
|
return this;
|
|
295
473
|
}
|
|
474
|
+
/**
|
|
475
|
+
* Convert string to uppercase
|
|
476
|
+
*
|
|
477
|
+
* @mutate
|
|
478
|
+
*/
|
|
296
479
|
uppercase() {
|
|
297
480
|
this.addMutator(uppercaseMutator);
|
|
298
481
|
return this;
|
|
299
482
|
}
|
|
483
|
+
/**
|
|
484
|
+
* Capitalize the first letter of the string
|
|
485
|
+
*
|
|
486
|
+
* @mutate
|
|
487
|
+
*/
|
|
488
|
+
capitalize() {
|
|
489
|
+
this.addMutator(capitalizeMutator);
|
|
490
|
+
return this;
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* Value must be a valid email
|
|
494
|
+
*/
|
|
300
495
|
email(errorMessage) {
|
|
301
496
|
this.addRule(emailRule, errorMessage);
|
|
302
497
|
return this;
|
|
303
498
|
}
|
|
499
|
+
/**
|
|
500
|
+
* Value must be a valid URL
|
|
501
|
+
*/
|
|
304
502
|
url(errorMessage) {
|
|
305
503
|
this.addRule(urlRule, errorMessage);
|
|
306
504
|
return this;
|
|
307
505
|
}
|
|
506
|
+
/**
|
|
507
|
+
* Value must match the value of the given field
|
|
508
|
+
*/
|
|
308
509
|
matches(field, errorMessage) {
|
|
309
510
|
const rule = this.addRule(matchesRule, errorMessage);
|
|
310
511
|
rule.context.options.field = field;
|
|
311
512
|
return this;
|
|
312
513
|
}
|
|
514
|
+
/**
|
|
515
|
+
* Value can not have whitespace
|
|
516
|
+
*/
|
|
517
|
+
withoutWhitespace(errorMessage) {
|
|
518
|
+
this.addRule(withoutWhitespaceRule, errorMessage);
|
|
519
|
+
return this;
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Value must match the given pattern
|
|
523
|
+
*/
|
|
313
524
|
pattern(pattern, errorMessage) {
|
|
314
525
|
const rule = this.addRule(patternRule, errorMessage);
|
|
315
526
|
rule.context.options.pattern = pattern;
|
|
316
527
|
return this;
|
|
317
528
|
}
|
|
529
|
+
/**
|
|
530
|
+
* Validate the current string as an uploadable hash id
|
|
531
|
+
*/
|
|
532
|
+
uploadable(errorMessage) {
|
|
533
|
+
this.addRule(uploadableRule, errorMessage);
|
|
534
|
+
return this;
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* Value must be exactly the given number of words
|
|
538
|
+
*/
|
|
539
|
+
words(words, errorMessage) {
|
|
540
|
+
const rule = this.addRule(wordsRule, errorMessage);
|
|
541
|
+
rule.context.options.words = words;
|
|
542
|
+
return this;
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Value must be at least the given number of words
|
|
546
|
+
*/
|
|
547
|
+
minWords(words, errorMessage) {
|
|
548
|
+
const rule = this.addRule(minWordsRule, errorMessage);
|
|
549
|
+
rule.context.options.minWords = words;
|
|
550
|
+
return this;
|
|
551
|
+
}
|
|
552
|
+
/**
|
|
553
|
+
* Value must be at most the given number of words
|
|
554
|
+
*/
|
|
555
|
+
maxWords(words, errorMessage) {
|
|
556
|
+
const rule = this.addRule(maxWordsRule, errorMessage);
|
|
557
|
+
rule.context.options.maxWords = words;
|
|
558
|
+
return this;
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* Value length must be greater than the given length
|
|
562
|
+
*/
|
|
318
563
|
minLength(length, errorMessage) {
|
|
319
564
|
const rule = this.addRule(minLengthRule, errorMessage);
|
|
320
565
|
rule.context.options.minLength = length;
|
|
321
566
|
return this;
|
|
322
567
|
}
|
|
568
|
+
/**
|
|
569
|
+
* @alias minLength
|
|
570
|
+
*/
|
|
571
|
+
min(min, errorMessage) {
|
|
572
|
+
return this.minLength(min, errorMessage);
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* Value length must be less than the given length
|
|
576
|
+
*/
|
|
323
577
|
maxLength(length, errorMessage) {
|
|
324
578
|
const rule = this.addRule(maxLengthRule, errorMessage);
|
|
325
579
|
rule.context.options.maxLength = length;
|
|
326
580
|
return this;
|
|
327
581
|
}
|
|
582
|
+
/**
|
|
583
|
+
* @alias maxLength
|
|
584
|
+
*/
|
|
585
|
+
max(max, errorMessage) {
|
|
586
|
+
return this.maxLength(max, errorMessage);
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Value must be of the given length
|
|
590
|
+
*/
|
|
328
591
|
length(length, errorMessage) {
|
|
329
592
|
const rule = this.addRule(lengthRule, errorMessage);
|
|
330
593
|
rule.context.options.length = length;
|
|
331
594
|
return this;
|
|
332
595
|
}
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
596
|
+
/**
|
|
597
|
+
* Allow only alphabetic characters
|
|
598
|
+
*/
|
|
599
|
+
alpha(errorMessage) {
|
|
600
|
+
this.addRule(alphaRule, errorMessage);
|
|
336
601
|
return this;
|
|
337
602
|
}
|
|
338
|
-
|
|
339
|
-
|
|
603
|
+
/**
|
|
604
|
+
* Allow only alphanumeric characters
|
|
605
|
+
*/
|
|
606
|
+
alphanumeric(errorMessage) {
|
|
607
|
+
this.addRule(alphaNumericRule, errorMessage);
|
|
608
|
+
return this;
|
|
340
609
|
}
|
|
341
|
-
|
|
342
|
-
|
|
610
|
+
/**
|
|
611
|
+
* Allow only numeric characters
|
|
612
|
+
*/
|
|
613
|
+
numeric(errorMessage) {
|
|
614
|
+
this.addRule(isNumericRule, errorMessage);
|
|
615
|
+
return this;
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Value must starts with the given string
|
|
619
|
+
*/
|
|
620
|
+
startsWith(value, errorMessage) {
|
|
621
|
+
const rule = this.addRule(startsWithRule, errorMessage);
|
|
343
622
|
rule.context.options.value = value;
|
|
344
623
|
return this;
|
|
345
624
|
}
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
625
|
+
/**
|
|
626
|
+
* Value must ends with the given string
|
|
627
|
+
*/
|
|
628
|
+
endsWith(value, errorMessage) {
|
|
629
|
+
const rule = this.addRule(endsWithRule, errorMessage);
|
|
630
|
+
rule.context.options.value = value;
|
|
352
631
|
return this;
|
|
353
632
|
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
633
|
+
/**
|
|
634
|
+
* Value must contain the given string
|
|
635
|
+
*/
|
|
636
|
+
contains(value, errorMessage) {
|
|
637
|
+
const rule = this.addRule(containsRule, errorMessage);
|
|
638
|
+
rule.context.options.value = value;
|
|
639
|
+
return this;
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* Value must not contain the given string
|
|
643
|
+
*/
|
|
644
|
+
notContains(value, errorMessage) {
|
|
645
|
+
const rule = this.addRule(notContainsRule, errorMessage);
|
|
646
|
+
rule.context.options.value = value;
|
|
647
|
+
return this;
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* Value must be a valid IP address
|
|
651
|
+
*/
|
|
652
|
+
ip(errorMessage) {
|
|
653
|
+
this.addRule(ipRule, errorMessage);
|
|
654
|
+
return this;
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
* Value must be a valid IPv4 address
|
|
658
|
+
*/
|
|
659
|
+
ip4(errorMessage) {
|
|
660
|
+
this.addRule(ip4Rule, errorMessage);
|
|
661
|
+
return this;
|
|
662
|
+
}
|
|
663
|
+
/**
|
|
664
|
+
* Value must be a valid IPv6 address
|
|
665
|
+
*/
|
|
666
|
+
ip6(errorMessage) {
|
|
667
|
+
this.addRule(ip6Rule, errorMessage);
|
|
668
|
+
return this;
|
|
669
|
+
}
|
|
670
|
+
/**
|
|
671
|
+
* Check if the string matches a credit card number
|
|
672
|
+
*/
|
|
673
|
+
creditCard(errorMessage) {
|
|
674
|
+
this.addRule(isCreditCardRule, errorMessage);
|
|
675
|
+
return this;
|
|
676
|
+
}
|
|
677
|
+
/**
|
|
678
|
+
* Determine if the value is a valid color
|
|
679
|
+
* This validation rule will check for hex, rgb, rgba, hsl colors
|
|
680
|
+
*/
|
|
681
|
+
color(errorMessage) {
|
|
682
|
+
this.addRule(colorRule, errorMessage);
|
|
683
|
+
return this;
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* Determine if the value is a valid hex color
|
|
687
|
+
*/
|
|
688
|
+
hexColor(errorMessage) {
|
|
689
|
+
this.addRule(hexColorRule, errorMessage);
|
|
690
|
+
return this;
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* Determine if the value is a valid HSL color
|
|
694
|
+
*/
|
|
695
|
+
hslColor(errorMessage) {
|
|
696
|
+
this.addRule(hslColorRule, errorMessage);
|
|
697
|
+
return this;
|
|
698
|
+
}
|
|
699
|
+
/**
|
|
700
|
+
* Determine if the value is a valid RGB color
|
|
701
|
+
*/
|
|
702
|
+
rgbColor(errorMessage) {
|
|
703
|
+
this.addRule(rgbColorRule, errorMessage);
|
|
704
|
+
return this;
|
|
705
|
+
}
|
|
706
|
+
/**
|
|
707
|
+
* Determine if the value is a valid RGBA color
|
|
708
|
+
*/
|
|
709
|
+
rgbaColor(errorMessage) {
|
|
710
|
+
this.addRule(rgbaColorRule, errorMessage);
|
|
711
|
+
return this;
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Determine if the value is a valid light color
|
|
715
|
+
*/
|
|
716
|
+
lightColor(errorMessage) {
|
|
717
|
+
this.addRule(lightColorRule, errorMessage);
|
|
360
718
|
return this;
|
|
361
719
|
}
|
|
720
|
+
/**
|
|
721
|
+
* Determine if the value is a valid dark color
|
|
722
|
+
*/
|
|
723
|
+
darkColor(errorMessage) {
|
|
724
|
+
this.addRule(darkColorRule, errorMessage);
|
|
725
|
+
return this;
|
|
726
|
+
}
|
|
727
|
+
/**
|
|
728
|
+
* Value must be one of the given values
|
|
729
|
+
*/
|
|
730
|
+
enum = ScalarValidator.prototype.enum;
|
|
731
|
+
/**
|
|
732
|
+
* Value must be one of the given values
|
|
733
|
+
*/
|
|
734
|
+
in = ScalarValidator.prototype.in;
|
|
735
|
+
/**
|
|
736
|
+
* @alias in
|
|
737
|
+
*/
|
|
738
|
+
oneOf = ScalarValidator.prototype.in;
|
|
739
|
+
/**
|
|
740
|
+
* Value must be unique
|
|
741
|
+
*/
|
|
742
|
+
unique = ScalarValidator.prototype.unique;
|
|
743
|
+
/**
|
|
744
|
+
* Value must exist
|
|
745
|
+
*/
|
|
746
|
+
exists = ScalarValidator.prototype.exists;
|
|
747
|
+
/**
|
|
748
|
+
* Add rule to check if the value is one of the allowed values
|
|
749
|
+
*/
|
|
750
|
+
allowsOnly = ScalarValidator.prototype.allowsOnly;
|
|
751
|
+
/**
|
|
752
|
+
* Add rule to forbid the value from being one of the given values
|
|
753
|
+
*/
|
|
754
|
+
forbids = ScalarValidator.prototype.forbids;
|
|
755
|
+
/**
|
|
756
|
+
* @alias forbids
|
|
757
|
+
*/
|
|
758
|
+
notIn = ScalarValidator.prototype.forbids;
|
|
362
759
|
}
|
|
363
760
|
class DateValidator extends BaseValidator {
|
|
364
|
-
constructor(errorMessage) {
|
|
761
|
+
constructor(format, errorMessage) {
|
|
365
762
|
super();
|
|
366
|
-
this.addRule(dateRule, errorMessage);
|
|
763
|
+
const rule = this.addRule(dateRule, errorMessage);
|
|
764
|
+
if (format) {
|
|
765
|
+
rule.context.options.format = format;
|
|
766
|
+
}
|
|
367
767
|
this.addMutator(dateMutator);
|
|
368
768
|
}
|
|
769
|
+
/**
|
|
770
|
+
* Date must be before the given date
|
|
771
|
+
*/
|
|
772
|
+
before(date, errorMessage) {
|
|
773
|
+
const rule = this.addRule(maxDateRule, errorMessage);
|
|
774
|
+
rule.context.options.maxDate = date;
|
|
775
|
+
return this;
|
|
776
|
+
}
|
|
777
|
+
/**
|
|
778
|
+
* Date must be after the given date
|
|
779
|
+
*/
|
|
780
|
+
after(date, errorMessage) {
|
|
781
|
+
const rule = this.addRule(minDateRule, errorMessage);
|
|
782
|
+
rule.context.options.minDate = date;
|
|
783
|
+
return this;
|
|
784
|
+
}
|
|
369
785
|
}
|
|
370
786
|
class NumberValidator extends BaseValidator {
|
|
371
787
|
constructor(errorMessage) {
|
|
@@ -373,41 +789,69 @@ class NumberValidator extends BaseValidator {
|
|
|
373
789
|
this.addRule(numberRule, errorMessage);
|
|
374
790
|
this.addMutator(numberMutator);
|
|
375
791
|
}
|
|
792
|
+
/**
|
|
793
|
+
* Value must be equal or higher than the given number
|
|
794
|
+
*/
|
|
376
795
|
min(min, errorMessage) {
|
|
377
796
|
const rule = this.addRule(minRule, errorMessage);
|
|
378
797
|
rule.context.options.min = min;
|
|
379
798
|
return this;
|
|
380
799
|
}
|
|
800
|
+
/**
|
|
801
|
+
* Value must be equal or less than the given number
|
|
802
|
+
*/
|
|
381
803
|
max(max, errorMessage) {
|
|
382
804
|
const rule = this.addRule(maxRule, errorMessage);
|
|
383
805
|
rule.context.options.max = max;
|
|
384
806
|
return this;
|
|
385
807
|
}
|
|
386
|
-
|
|
387
|
-
|
|
808
|
+
/**
|
|
809
|
+
* Value must be a modulo of the given number
|
|
810
|
+
*/
|
|
811
|
+
modulo(value, errorMessage) {
|
|
812
|
+
const rule = this.addRule(moduloRule, errorMessage);
|
|
388
813
|
rule.context.options.value = value;
|
|
389
814
|
return this;
|
|
390
815
|
}
|
|
816
|
+
/**
|
|
817
|
+
* Accept only numbers higher than 0
|
|
818
|
+
*/
|
|
391
819
|
positive(errorMessage) {
|
|
392
820
|
this.addRule(positiveRule, errorMessage);
|
|
393
821
|
return this;
|
|
394
822
|
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
823
|
+
/**
|
|
824
|
+
* Value must be unique and not exist in database
|
|
825
|
+
*/
|
|
826
|
+
unique = ScalarValidator.prototype.unique;
|
|
827
|
+
/**
|
|
828
|
+
* Value must exist in database
|
|
829
|
+
*/
|
|
830
|
+
exists = ScalarValidator.prototype.exists;
|
|
831
|
+
/**
|
|
832
|
+
* Value must be one of the given values
|
|
833
|
+
*/
|
|
834
|
+
enum = ScalarValidator.prototype.enum;
|
|
835
|
+
/**
|
|
836
|
+
* Value must be one of the given values
|
|
837
|
+
*/
|
|
838
|
+
in = ScalarValidator.prototype.in;
|
|
839
|
+
/**
|
|
840
|
+
* @alias in
|
|
841
|
+
*/
|
|
842
|
+
oneOf = ScalarValidator.prototype.in;
|
|
843
|
+
/**
|
|
844
|
+
* Add rule to check if the value is one of the allowed values
|
|
845
|
+
*/
|
|
846
|
+
allowsOnly = ScalarValidator.prototype.allowsOnly;
|
|
847
|
+
/**
|
|
848
|
+
* Add rule to forbid the value from being one of the given values
|
|
849
|
+
*/
|
|
850
|
+
forbids = ScalarValidator.prototype.forbids;
|
|
851
|
+
/**
|
|
852
|
+
* @alias forbids
|
|
853
|
+
*/
|
|
854
|
+
notIn = ScalarValidator.prototype.forbids;
|
|
411
855
|
}
|
|
412
856
|
class IntValidator extends NumberValidator {
|
|
413
857
|
constructor(errorMessage) {
|
|
@@ -432,6 +876,9 @@ class ScalarValidator extends BaseValidator {
|
|
|
432
876
|
super();
|
|
433
877
|
this.addRule(scalarRule, errorMessage);
|
|
434
878
|
}
|
|
879
|
+
/**
|
|
880
|
+
* Value must be unique
|
|
881
|
+
*/
|
|
435
882
|
unique(model, { errorMessage, ...options } = {}) {
|
|
436
883
|
const rule = this.addRule(uniqueRule, errorMessage);
|
|
437
884
|
rule.context.options = {
|
|
@@ -440,34 +887,113 @@ class ScalarValidator extends BaseValidator {
|
|
|
440
887
|
};
|
|
441
888
|
return this;
|
|
442
889
|
}
|
|
890
|
+
/**
|
|
891
|
+
* Value must exist in database
|
|
892
|
+
*/
|
|
443
893
|
exists(model, { errorMessage, ...options } = {}) {
|
|
444
894
|
const rule = this.addRule(existsRule, errorMessage);
|
|
445
895
|
rule.context.options = {
|
|
446
896
|
...options,
|
|
447
897
|
Model: model,
|
|
448
898
|
};
|
|
449
|
-
return;
|
|
899
|
+
return this;
|
|
900
|
+
}
|
|
901
|
+
/**
|
|
902
|
+
* Value must be one of the given values
|
|
903
|
+
*/
|
|
904
|
+
enum(values, errorMessage) {
|
|
905
|
+
const rule = this.addRule(enumRule, errorMessage);
|
|
906
|
+
rule.context.options.enum = values;
|
|
907
|
+
return this;
|
|
908
|
+
}
|
|
909
|
+
/**
|
|
910
|
+
* Value must be one of the given values
|
|
911
|
+
*/
|
|
912
|
+
in(values, errorMessage) {
|
|
913
|
+
const rule = this.addRule(inRule, errorMessage);
|
|
914
|
+
rule.context.options.values = values;
|
|
915
|
+
return this;
|
|
916
|
+
}
|
|
917
|
+
/**
|
|
918
|
+
* @alias in
|
|
919
|
+
*/
|
|
920
|
+
oneOf = ScalarValidator.prototype.in;
|
|
921
|
+
/**
|
|
922
|
+
* Add rule to check if the value is one of the allowed values
|
|
923
|
+
*/
|
|
924
|
+
allowsOnly(values, errorMessage) {
|
|
925
|
+
const rule = this.addRule(allowedValuesRule, errorMessage);
|
|
926
|
+
rule.context.options.allowedValues = values;
|
|
927
|
+
return this;
|
|
928
|
+
}
|
|
929
|
+
/**
|
|
930
|
+
* Forbid the value from being one of the given values
|
|
931
|
+
*/
|
|
932
|
+
forbids(values, errorMessage) {
|
|
933
|
+
const rule = this.addRule(notAllowedValuesRule, errorMessage);
|
|
934
|
+
rule.context.options.notAllowedValues = values;
|
|
935
|
+
return this;
|
|
450
936
|
}
|
|
451
937
|
}
|
|
452
938
|
class FileValidator extends BaseValidator {
|
|
453
939
|
constructor(errorMessage) {
|
|
454
940
|
super();
|
|
455
|
-
this.addRule(
|
|
941
|
+
this.addRule(fileRule, errorMessage);
|
|
456
942
|
}
|
|
457
943
|
image(errorMessage) {
|
|
458
944
|
this.addRule(imageRule, errorMessage);
|
|
459
945
|
return this;
|
|
460
946
|
}
|
|
461
|
-
|
|
947
|
+
accept(extensions, errorMessage) {
|
|
948
|
+
const rule = this.addRule(fileExtensionRule, errorMessage);
|
|
949
|
+
rule.context.options.extensions = extensions;
|
|
950
|
+
return this;
|
|
951
|
+
}
|
|
952
|
+
mimeType(mimeTypes, errorMessage) {
|
|
953
|
+
const rule = this.addRule(fileTypeRule, errorMessage);
|
|
954
|
+
rule.context.options.mimeTypes = mimeTypes;
|
|
955
|
+
return this;
|
|
956
|
+
}
|
|
957
|
+
/**
|
|
958
|
+
* Allow only pdf files
|
|
959
|
+
*/
|
|
960
|
+
pdf(errorMessage) {
|
|
961
|
+
return this.mimeType("application/pdf", errorMessage);
|
|
962
|
+
}
|
|
963
|
+
/**
|
|
964
|
+
* Allow only excel files
|
|
965
|
+
*/
|
|
966
|
+
excel(errorMessage) {
|
|
967
|
+
return this.mimeType([
|
|
968
|
+
"application/vnd.ms-excel",
|
|
969
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
970
|
+
], errorMessage);
|
|
971
|
+
}
|
|
972
|
+
/**
|
|
973
|
+
* Allow only word files
|
|
974
|
+
*/
|
|
975
|
+
word(errorMessage) {
|
|
976
|
+
return this.mimeType([
|
|
977
|
+
"application/msword",
|
|
978
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
979
|
+
], errorMessage);
|
|
980
|
+
}
|
|
981
|
+
minSize(size, errorMessage) {
|
|
462
982
|
const rule = this.addRule(minFileSizeRule, errorMessage);
|
|
463
983
|
rule.context.options.minFileSize = size;
|
|
464
984
|
return this;
|
|
465
985
|
}
|
|
466
|
-
|
|
986
|
+
min(size, errorMessage) {
|
|
987
|
+
return this.minSize(size, errorMessage);
|
|
988
|
+
}
|
|
989
|
+
maxSize(size, errorMessage) {
|
|
467
990
|
const rule = this.addRule(maxFileSizeRule, errorMessage);
|
|
468
991
|
rule.context.options.maxFileSize = size;
|
|
469
992
|
return this;
|
|
470
993
|
}
|
|
994
|
+
max(size, errorMessage) {
|
|
995
|
+
return this.maxSize(size, errorMessage);
|
|
996
|
+
}
|
|
471
997
|
minWidth(width, errorMessage) {
|
|
472
998
|
const rule = this.addRule(minWidthRule, errorMessage);
|
|
473
999
|
rule.context.options.minWidth = width;
|
|
@@ -484,7 +1010,7 @@ class FileValidator extends BaseValidator {
|
|
|
484
1010
|
return this;
|
|
485
1011
|
}
|
|
486
1012
|
maxHeight(height, errorMessage) {
|
|
487
|
-
const rule = this.addRule(
|
|
1013
|
+
const rule = this.addRule(maxHeightRule, errorMessage);
|
|
488
1014
|
rule.context.options.maxHeight = height;
|
|
489
1015
|
return this;
|
|
490
1016
|
}
|
|
@@ -504,7 +1030,10 @@ const validate = async (schema, data) => {
|
|
|
504
1030
|
};
|
|
505
1031
|
const v = {
|
|
506
1032
|
object: (schema, errorMessage) => new ObjectValidator(schema, errorMessage),
|
|
1033
|
+
any: () => new AnyValidator(),
|
|
1034
|
+
forbidden: () => v.any().forbidden(),
|
|
507
1035
|
array: (validator, errorMessage) => new ArrayValidator(validator, errorMessage),
|
|
1036
|
+
date: (format, errorMessage) => new DateValidator(format, errorMessage),
|
|
508
1037
|
string: (errorMessage) => new StringValidator(errorMessage),
|
|
509
1038
|
number: (errorMessage) => new NumberValidator(errorMessage),
|
|
510
1039
|
int: (errorMessage) => new IntValidator(errorMessage),
|
|
@@ -512,5 +1041,9 @@ const v = {
|
|
|
512
1041
|
boolean: (errorMessage) => new BooleanValidator(errorMessage),
|
|
513
1042
|
scalar: (errorMessage) => new ScalarValidator(errorMessage),
|
|
514
1043
|
file: (errorMessage) => new FileValidator(errorMessage),
|
|
1044
|
+
localized: (valueValidator, errorMessage) => v.array(v.object({
|
|
1045
|
+
localeCode: v.string().required(),
|
|
1046
|
+
value: (valueValidator || v.string()).required(),
|
|
1047
|
+
}), errorMessage),
|
|
515
1048
|
validate,
|
|
516
1049
|
};export{AnyValidator,ArrayValidator,BaseValidator,BooleanValidator,DateValidator,FileValidator,FloatValidator,IntValidator,ObjectValidator,ScalarValidator,StringValidator,v,validate};//# sourceMappingURL=schema.js.map
|