remult 0.27.23-next.0 → 0.27.23-next.2
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/esm/src/data-api.js +1 -1
- package/esm/src/validators.js +65 -0
- package/package.json +1 -1
- package/src/data-api.js +1 -1
- package/src/remult3/remult3.d.ts +1 -1
- package/src/validators.d.ts +80 -1
- package/src/validators.js +71 -0
package/esm/src/data-api.js
CHANGED
|
@@ -115,7 +115,7 @@ export class DataApi {
|
|
|
115
115
|
let { aggregate, ...rest } = body;
|
|
116
116
|
let [{ r }, [aggregates]] = await Promise.all([
|
|
117
117
|
this.getArrayImpl(request, rest),
|
|
118
|
-
this.groupBy(request, aggregate),
|
|
118
|
+
this.groupBy(request, { ...aggregate, where: body.where }),
|
|
119
119
|
]);
|
|
120
120
|
return {
|
|
121
121
|
items: r,
|
package/esm/src/validators.js
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
1
|
import { getRelationFieldInfo } from '../internals.js';
|
|
2
|
+
/**
|
|
3
|
+
* Class containing various field validators.
|
|
4
|
+
*/
|
|
2
5
|
export class Validators {
|
|
6
|
+
/**
|
|
7
|
+
* Validator to check if a value is required (not null or empty).
|
|
8
|
+
*/
|
|
3
9
|
static required = createValidator(async (_, e) => !e.valueIsNull() &&
|
|
4
10
|
e.value !== '' &&
|
|
5
11
|
(e.value !== undefined || getRelationFieldInfo(e.metadata) !== undefined), 'Should not be empty');
|
|
12
|
+
/**
|
|
13
|
+
* Validator to ensure a value is unique in the database.
|
|
14
|
+
*/
|
|
6
15
|
static unique = createValidator(async (_, e) => {
|
|
7
16
|
if (!e.entityRef)
|
|
8
17
|
throw 'unique validation may only work on columns that are attached to an entity';
|
|
@@ -16,6 +25,7 @@ export class Validators {
|
|
|
16
25
|
}, 'already exists');
|
|
17
26
|
/**
|
|
18
27
|
* @deprecated use `unique` instead - it also runs only on the backend
|
|
28
|
+
* Validator to ensure a value is unique on the backend.
|
|
19
29
|
*/
|
|
20
30
|
static uniqueOnBackend = createValidator(async (_, e) => {
|
|
21
31
|
if (e.isBackend() && (e.isNew || e.valueChanged())) {
|
|
@@ -26,9 +36,21 @@ export class Validators {
|
|
|
26
36
|
else
|
|
27
37
|
return true;
|
|
28
38
|
}, Validators.unique.defaultMessage);
|
|
39
|
+
/**
|
|
40
|
+
* Validator to check if a value matches a given regular expression.
|
|
41
|
+
*/
|
|
29
42
|
static regex = createValueValidatorWithArgs((val, regex) => regex.test(val));
|
|
43
|
+
/**
|
|
44
|
+
* Validator to check if a value is a valid email address.
|
|
45
|
+
*/
|
|
30
46
|
static email = createValueValidator((val) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(val), 'Invalid Email');
|
|
47
|
+
/**
|
|
48
|
+
* Validator to check if a value is a valid URL.
|
|
49
|
+
*/
|
|
31
50
|
static url = createValueValidator((val) => !!new URL(val), 'Invalid Url');
|
|
51
|
+
/**
|
|
52
|
+
* Validator to check if a value is one of the specified values.
|
|
53
|
+
*/
|
|
32
54
|
static in = createValueValidatorWithArgs((val, values) => values.includes(val), (values) => `Value must be one of: ${values
|
|
33
55
|
.map((y) => typeof y === 'object'
|
|
34
56
|
? y?.['id'] !== undefined
|
|
@@ -36,8 +58,17 @@ export class Validators {
|
|
|
36
58
|
: y?.toString()
|
|
37
59
|
: y)
|
|
38
60
|
.join(', ')}`);
|
|
61
|
+
/**
|
|
62
|
+
* Validator to check if a value is not null.
|
|
63
|
+
*/
|
|
39
64
|
static notNull = createValueValidator((val) => val != null, 'Should not be null');
|
|
65
|
+
/**
|
|
66
|
+
* Validator to check if a value exists in a given enum.
|
|
67
|
+
*/
|
|
40
68
|
static enum = createValueValidatorWithArgs((value, enumObj) => Object.values(enumObj).includes(value), (enumObj) => `Value must be one of ${getEnumValues(enumObj).join(', ')}`);
|
|
69
|
+
/**
|
|
70
|
+
* Validator to check if a related value exists in the database.
|
|
71
|
+
*/
|
|
41
72
|
static relationExists = createValidator(async (_, e) => {
|
|
42
73
|
if (e.valueIsNull())
|
|
43
74
|
return true;
|
|
@@ -45,12 +76,31 @@ export class Validators {
|
|
|
45
76
|
return true;
|
|
46
77
|
return Boolean(await e.load());
|
|
47
78
|
}, 'Relation value does not exist');
|
|
79
|
+
/**
|
|
80
|
+
* Validator to check if a value is greater than or equal to a minimum value.
|
|
81
|
+
*/
|
|
48
82
|
static min = createValueValidatorWithArgs((val, minValue) => val >= minValue, (minValue) => `Value must be bigger than or equal to ${minValue}`);
|
|
83
|
+
/**
|
|
84
|
+
* Validator to check if a value is less than or equal to a maximum value.
|
|
85
|
+
*/
|
|
49
86
|
static max = createValueValidatorWithArgs((val, maxValue) => val <= maxValue, (maxValue) => `Value must be smaller than or equal to ${maxValue}`);
|
|
87
|
+
/**
|
|
88
|
+
* Validator to check if a string's length is less than or equal to a maximum length.
|
|
89
|
+
*/
|
|
50
90
|
static maxLength = createValueValidatorWithArgs((val, maxLength) => val.length <= maxLength, (maxLength) => `Value must be at most ${maxLength} characters`);
|
|
91
|
+
/**
|
|
92
|
+
* Validator to check if a string's length is greater than or equal to a minimum length.
|
|
93
|
+
*/
|
|
51
94
|
static minLength = createValueValidatorWithArgs((val, minLength) => val.length >= minLength, (maxLength) => `Value must be at least ${maxLength} characters`);
|
|
95
|
+
/**
|
|
96
|
+
* Validator to check if a value is within a specified range.
|
|
97
|
+
*/
|
|
98
|
+
static range = createValueValidatorWithArgs((val, [minValue, maxValue]) => val >= minValue && val <= maxValue, ([minValue, maxValue]) => `Value must be between ${minValue} and ${maxValue}`);
|
|
52
99
|
static defaultMessage = 'Invalid value';
|
|
53
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Function to create a validator with a custom validation function.
|
|
103
|
+
*/
|
|
54
104
|
export function createValidator(validate, defaultMessage) {
|
|
55
105
|
const validation = async (entity, e, message) => {
|
|
56
106
|
const valid = await validate(entity, e);
|
|
@@ -87,9 +137,15 @@ export function createValidator(validate, defaultMessage) {
|
|
|
87
137
|
withMessage: (message) => async (entity, e) => result(entity, e, message),
|
|
88
138
|
});
|
|
89
139
|
}
|
|
140
|
+
/**
|
|
141
|
+
* Function to create a value validator.
|
|
142
|
+
*/
|
|
90
143
|
export function valueValidator(validate, defaultMessage) {
|
|
91
144
|
return (entity, e) => validate(e.value) || defaultMessage || false;
|
|
92
145
|
}
|
|
146
|
+
/**
|
|
147
|
+
* Function to create a value validator with arguments.
|
|
148
|
+
*/
|
|
93
149
|
export function createValueValidator(validate, defaultMessage) {
|
|
94
150
|
return createValidator((_, e) => {
|
|
95
151
|
if (e.value === undefined || e.value === null)
|
|
@@ -97,6 +153,9 @@ export function createValueValidator(validate, defaultMessage) {
|
|
|
97
153
|
return validate(e.value);
|
|
98
154
|
}, defaultMessage);
|
|
99
155
|
}
|
|
156
|
+
/**
|
|
157
|
+
* Function to create a value validator with arguments and a custom message.
|
|
158
|
+
*/
|
|
100
159
|
export function createValueValidatorWithArgs(validate, defaultMessage) {
|
|
101
160
|
const result = createValidatorWithArgsInternal((_, e, args) => {
|
|
102
161
|
if (e.value === undefined || e.value === null)
|
|
@@ -113,6 +172,9 @@ export function createValueValidatorWithArgs(validate, defaultMessage) {
|
|
|
113
172
|
},
|
|
114
173
|
});
|
|
115
174
|
}
|
|
175
|
+
/**
|
|
176
|
+
* Function to create a validator with arguments and a custom message.
|
|
177
|
+
*/
|
|
116
178
|
export function createValidatorWithArgs(validate, defaultMessage) {
|
|
117
179
|
return createValidatorWithArgsInternal(validate, defaultMessage);
|
|
118
180
|
}
|
|
@@ -143,6 +205,9 @@ function createValidatorWithArgsInternal(validate, defaultMessage, isValueValida
|
|
|
143
205
|
},
|
|
144
206
|
});
|
|
145
207
|
}
|
|
208
|
+
/**
|
|
209
|
+
* Function to get the values of an enum.
|
|
210
|
+
*/
|
|
146
211
|
export function getEnumValues(enumObj) {
|
|
147
212
|
return Object.values(enumObj).filter((x) => typeof enumObj[x] !== 'number');
|
|
148
213
|
}
|
package/package.json
CHANGED
package/src/data-api.js
CHANGED
|
@@ -201,7 +201,7 @@ var DataApi = /** @class */ (function () {
|
|
|
201
201
|
aggregate = body.aggregate, rest = tslib_1.__rest(body, ["aggregate"]);
|
|
202
202
|
return [4 /*yield*/, Promise.all([
|
|
203
203
|
this.getArrayImpl(request, rest),
|
|
204
|
-
this.groupBy(request, aggregate),
|
|
204
|
+
this.groupBy(request, tslib_1.__assign(tslib_1.__assign({}, aggregate), { where: body.where })),
|
|
205
205
|
])];
|
|
206
206
|
case 2:
|
|
207
207
|
_a = tslib_1.__read.apply(void 0, [_c.sent(), 2]), r = _a[0].r, _b = tslib_1.__read(_a[1], 1), aggregates = _b[0];
|
package/src/remult3/remult3.d.ts
CHANGED
|
@@ -1161,7 +1161,7 @@ export declare type EntityIdFields<entityType> = {
|
|
|
1161
1161
|
};
|
|
1162
1162
|
export interface ClassFieldDecoratorContextStub<entityType, valueType> {
|
|
1163
1163
|
readonly access: {
|
|
1164
|
-
set(object: entityType, value: valueType): void;
|
|
1164
|
+
set(object: entityType, value: valueType | null): void;
|
|
1165
1165
|
};
|
|
1166
1166
|
readonly name: string;
|
|
1167
1167
|
}
|
package/src/validators.d.ts
CHANGED
|
@@ -1,56 +1,135 @@
|
|
|
1
1
|
import type { FieldValidator } from './column-interfaces.js';
|
|
2
2
|
import type { ValidateFieldEvent } from './remult3/remult3.js';
|
|
3
|
+
/**
|
|
4
|
+
* Class containing various field validators.
|
|
5
|
+
*/
|
|
3
6
|
export declare class Validators {
|
|
7
|
+
/**
|
|
8
|
+
* Validator to check if a value is required (not null or empty).
|
|
9
|
+
*/
|
|
4
10
|
static required: Validator<unknown>;
|
|
11
|
+
/**
|
|
12
|
+
* Validator to ensure a value is unique in the database.
|
|
13
|
+
*/
|
|
5
14
|
static unique: Validator<unknown>;
|
|
6
15
|
/**
|
|
7
16
|
* @deprecated use `unique` instead - it also runs only on the backend
|
|
17
|
+
* Validator to ensure a value is unique on the backend.
|
|
8
18
|
*/
|
|
9
19
|
static uniqueOnBackend: Validator<unknown>;
|
|
20
|
+
/**
|
|
21
|
+
* Validator to check if a value matches a given regular expression.
|
|
22
|
+
*/
|
|
10
23
|
static regex: ValidatorWithArgs<string, RegExp> & {
|
|
11
24
|
defaultMessage: ValueValidationMessage<RegExp>;
|
|
12
25
|
};
|
|
26
|
+
/**
|
|
27
|
+
* Validator to check if a value is a valid email address.
|
|
28
|
+
*/
|
|
13
29
|
static email: Validator<string>;
|
|
30
|
+
/**
|
|
31
|
+
* Validator to check if a value is a valid URL.
|
|
32
|
+
*/
|
|
14
33
|
static url: Validator<string>;
|
|
34
|
+
/**
|
|
35
|
+
* Validator to check if a value is one of the specified values.
|
|
36
|
+
*/
|
|
15
37
|
static in: <T>(value: readonly T[], withMessage?: ValueValidationMessage<T[]>) => FieldValidator<unknown, T> & {
|
|
16
38
|
withMessage: ValueValidationMessage<T[]>;
|
|
17
39
|
};
|
|
40
|
+
/**
|
|
41
|
+
* Validator to check if a value is not null.
|
|
42
|
+
*/
|
|
18
43
|
static notNull: Validator<unknown>;
|
|
44
|
+
/**
|
|
45
|
+
* Validator to check if a value exists in a given enum.
|
|
46
|
+
*/
|
|
19
47
|
static enum: ValidatorWithArgs<unknown, unknown> & {
|
|
20
48
|
defaultMessage: ValueValidationMessage<unknown>;
|
|
21
49
|
};
|
|
50
|
+
/**
|
|
51
|
+
* Validator to check if a related value exists in the database.
|
|
52
|
+
*/
|
|
22
53
|
static relationExists: Validator<unknown>;
|
|
54
|
+
/**
|
|
55
|
+
* Validator to check if a value is greater than or equal to a minimum value.
|
|
56
|
+
*/
|
|
23
57
|
static min: ValidatorWithArgs<number, number> & {
|
|
24
58
|
defaultMessage: ValueValidationMessage<number>;
|
|
25
59
|
};
|
|
60
|
+
/**
|
|
61
|
+
* Validator to check if a value is less than or equal to a maximum value.
|
|
62
|
+
*/
|
|
26
63
|
static max: ValidatorWithArgs<number, number> & {
|
|
27
64
|
defaultMessage: ValueValidationMessage<number>;
|
|
28
65
|
};
|
|
66
|
+
/**
|
|
67
|
+
* Validator to check if a string's length is less than or equal to a maximum length.
|
|
68
|
+
*/
|
|
29
69
|
static maxLength: ValidatorWithArgs<string, number> & {
|
|
30
70
|
defaultMessage: ValueValidationMessage<number>;
|
|
31
71
|
};
|
|
72
|
+
/**
|
|
73
|
+
* Validator to check if a string's length is greater than or equal to a minimum length.
|
|
74
|
+
*/
|
|
32
75
|
static minLength: ValidatorWithArgs<string, number> & {
|
|
33
76
|
defaultMessage: ValueValidationMessage<number>;
|
|
34
77
|
};
|
|
78
|
+
/**
|
|
79
|
+
* Validator to check if a value is within a specified range.
|
|
80
|
+
*/
|
|
81
|
+
static range: ValidatorWithArgs<number, [number, number]> & {
|
|
82
|
+
defaultMessage: ValueValidationMessage<[number, number]>;
|
|
83
|
+
};
|
|
35
84
|
static defaultMessage: string;
|
|
36
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* Type representing a field validator with an optional message.
|
|
88
|
+
*/
|
|
37
89
|
export type Validator<valueType> = FieldValidator<unknown, valueType> & ((message?: ValidationMessage<valueType, undefined>) => FieldValidator<unknown, valueType>) & {
|
|
38
90
|
defaultMessage: ValidationMessage<valueType, undefined>;
|
|
39
91
|
/**
|
|
40
|
-
* @deprecated
|
|
92
|
+
* @deprecated use (message:string) instead - for example: Validators.required("Is needed")
|
|
41
93
|
*/
|
|
42
94
|
withMessage(message: ValidationMessage<valueType, undefined>): FieldValidator<unknown, valueType>;
|
|
43
95
|
};
|
|
96
|
+
/**
|
|
97
|
+
* Function to create a validator with a custom validation function.
|
|
98
|
+
*/
|
|
44
99
|
export declare function createValidator<valueType>(validate: (entity: any, e: ValidateFieldEvent<any, valueType>) => Promise<boolean | string> | boolean | string, defaultMessage?: ValidationMessage<valueType, undefined>): Validator<valueType>;
|
|
100
|
+
/**
|
|
101
|
+
* Function to create a value validator.
|
|
102
|
+
*/
|
|
45
103
|
export declare function valueValidator<valueType>(validate: (value: valueType) => boolean | string | Promise<boolean | string>, defaultMessage?: string): (entity: any, e: ValidateFieldEvent<any, valueType>) => string | boolean | Promise<string | boolean>;
|
|
104
|
+
/**
|
|
105
|
+
* Function to create a value validator with arguments.
|
|
106
|
+
*/
|
|
46
107
|
export declare function createValueValidator<valueType>(validate: (value: valueType) => boolean | string | Promise<boolean | string>, defaultMessage?: ValidationMessage<valueType, undefined>): Validator<valueType>;
|
|
108
|
+
/**
|
|
109
|
+
* Function to create a value validator with arguments and a custom message.
|
|
110
|
+
*/
|
|
47
111
|
export declare function createValueValidatorWithArgs<valueType, argsType>(validate: (value: valueType, args: argsType) => boolean | string | Promise<boolean | string>, defaultMessage?: ValueValidationMessage<argsType>): ValidatorWithArgs<valueType, argsType> & {
|
|
48
112
|
defaultMessage: ValueValidationMessage<argsType>;
|
|
49
113
|
};
|
|
114
|
+
/**
|
|
115
|
+
* Type representing a validation message that can be a string or a function.
|
|
116
|
+
*/
|
|
50
117
|
export type ValueValidationMessage<argsType> = string | ((args: argsType) => string);
|
|
118
|
+
/**
|
|
119
|
+
* Type representing a validation message with additional parameters.
|
|
120
|
+
*/
|
|
51
121
|
export type ValidationMessage<valueType, argsType> = string | ((entity: any, event: ValidateFieldEvent<any, valueType>, args: argsType) => string);
|
|
122
|
+
/**
|
|
123
|
+
* Type representing a validator with arguments.
|
|
124
|
+
*/
|
|
52
125
|
export type ValidatorWithArgs<valueType, argsType> = (args: argsType, message?: ValidationMessage<valueType, argsType>) => FieldValidator<unknown, valueType>;
|
|
126
|
+
/**
|
|
127
|
+
* Function to create a validator with arguments and a custom message.
|
|
128
|
+
*/
|
|
53
129
|
export declare function createValidatorWithArgs<valueType, argsType>(validate: (entity: any, e: ValidateFieldEvent<any, valueType>, args: argsType) => Promise<boolean | string> | boolean | string, defaultMessage: ValidationMessage<valueType, argsType>): ValidatorWithArgs<valueType, argsType> & {
|
|
54
130
|
defaultMessage: ValidationMessage<valueType, argsType>;
|
|
55
131
|
};
|
|
132
|
+
/**
|
|
133
|
+
* Function to get the values of an enum.
|
|
134
|
+
*/
|
|
56
135
|
export declare function getEnumValues<theEnum>(enumObj: theEnum): any[];
|
package/src/validators.js
CHANGED
|
@@ -9,11 +9,17 @@ exports.createValidatorWithArgs = createValidatorWithArgs;
|
|
|
9
9
|
exports.getEnumValues = getEnumValues;
|
|
10
10
|
var tslib_1 = require("tslib");
|
|
11
11
|
var internals_js_1 = require("../internals.js");
|
|
12
|
+
/**
|
|
13
|
+
* Class containing various field validators.
|
|
14
|
+
*/
|
|
12
15
|
var Validators = /** @class */ (function () {
|
|
13
16
|
function Validators() {
|
|
14
17
|
}
|
|
15
18
|
var _a;
|
|
16
19
|
_a = Validators;
|
|
20
|
+
/**
|
|
21
|
+
* Validator to check if a value is required (not null or empty).
|
|
22
|
+
*/
|
|
17
23
|
Validators.required = createValidator(function (_, e) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
18
24
|
return tslib_1.__generator(_a, function (_b) {
|
|
19
25
|
return [2 /*return*/, !e.valueIsNull() &&
|
|
@@ -21,6 +27,9 @@ var Validators = /** @class */ (function () {
|
|
|
21
27
|
(e.value !== undefined || (0, internals_js_1.getRelationFieldInfo)(e.metadata) !== undefined)];
|
|
22
28
|
});
|
|
23
29
|
}); }, 'Should not be empty');
|
|
30
|
+
/**
|
|
31
|
+
* Validator to ensure a value is unique in the database.
|
|
32
|
+
*/
|
|
24
33
|
Validators.unique = createValidator(function (_, e) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
25
34
|
var _b;
|
|
26
35
|
return tslib_1.__generator(_a, function (_c) {
|
|
@@ -39,6 +48,7 @@ var Validators = /** @class */ (function () {
|
|
|
39
48
|
}); }, 'already exists');
|
|
40
49
|
/**
|
|
41
50
|
* @deprecated use `unique` instead - it also runs only on the backend
|
|
51
|
+
* Validator to ensure a value is unique on the backend.
|
|
42
52
|
*/
|
|
43
53
|
Validators.uniqueOnBackend = createValidator(function (_, e) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
44
54
|
var _b;
|
|
@@ -54,11 +64,23 @@ var Validators = /** @class */ (function () {
|
|
|
54
64
|
}
|
|
55
65
|
});
|
|
56
66
|
}); }, _a.unique.defaultMessage);
|
|
67
|
+
/**
|
|
68
|
+
* Validator to check if a value matches a given regular expression.
|
|
69
|
+
*/
|
|
57
70
|
Validators.regex = createValueValidatorWithArgs(function (val, regex) {
|
|
58
71
|
return regex.test(val);
|
|
59
72
|
});
|
|
73
|
+
/**
|
|
74
|
+
* Validator to check if a value is a valid email address.
|
|
75
|
+
*/
|
|
60
76
|
Validators.email = createValueValidator(function (val) { return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(val); }, 'Invalid Email');
|
|
77
|
+
/**
|
|
78
|
+
* Validator to check if a value is a valid URL.
|
|
79
|
+
*/
|
|
61
80
|
Validators.url = createValueValidator(function (val) { return !!new URL(val); }, 'Invalid Url');
|
|
81
|
+
/**
|
|
82
|
+
* Validator to check if a value is one of the specified values.
|
|
83
|
+
*/
|
|
62
84
|
Validators.in = createValueValidatorWithArgs(function (val, values) { return values.includes(val); }, function (values) {
|
|
63
85
|
return "Value must be one of: ".concat(values
|
|
64
86
|
.map(function (y) {
|
|
@@ -70,10 +92,19 @@ var Validators = /** @class */ (function () {
|
|
|
70
92
|
})
|
|
71
93
|
.join(', '));
|
|
72
94
|
});
|
|
95
|
+
/**
|
|
96
|
+
* Validator to check if a value is not null.
|
|
97
|
+
*/
|
|
73
98
|
Validators.notNull = createValueValidator(function (val) { return val != null; }, 'Should not be null');
|
|
99
|
+
/**
|
|
100
|
+
* Validator to check if a value exists in a given enum.
|
|
101
|
+
*/
|
|
74
102
|
Validators.enum = createValueValidatorWithArgs(function (value, enumObj) { return Object.values(enumObj).includes(value); }, function (enumObj) {
|
|
75
103
|
return "Value must be one of ".concat(getEnumValues(enumObj).join(', '));
|
|
76
104
|
});
|
|
105
|
+
/**
|
|
106
|
+
* Validator to check if a related value exists in the database.
|
|
107
|
+
*/
|
|
77
108
|
Validators.relationExists = createValidator(function (_, e) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
78
109
|
var _b;
|
|
79
110
|
return tslib_1.__generator(_a, function (_c) {
|
|
@@ -89,14 +120,39 @@ var Validators = /** @class */ (function () {
|
|
|
89
120
|
}
|
|
90
121
|
});
|
|
91
122
|
}); }, 'Relation value does not exist');
|
|
123
|
+
/**
|
|
124
|
+
* Validator to check if a value is greater than or equal to a minimum value.
|
|
125
|
+
*/
|
|
92
126
|
Validators.min = createValueValidatorWithArgs(function (val, minValue) { return val >= minValue; }, function (minValue) { return "Value must be bigger than or equal to ".concat(minValue); });
|
|
127
|
+
/**
|
|
128
|
+
* Validator to check if a value is less than or equal to a maximum value.
|
|
129
|
+
*/
|
|
93
130
|
Validators.max = createValueValidatorWithArgs(function (val, maxValue) { return val <= maxValue; }, function (maxValue) { return "Value must be smaller than or equal to ".concat(maxValue); });
|
|
131
|
+
/**
|
|
132
|
+
* Validator to check if a string's length is less than or equal to a maximum length.
|
|
133
|
+
*/
|
|
94
134
|
Validators.maxLength = createValueValidatorWithArgs(function (val, maxLength) { return val.length <= maxLength; }, function (maxLength) { return "Value must be at most ".concat(maxLength, " characters"); });
|
|
135
|
+
/**
|
|
136
|
+
* Validator to check if a string's length is greater than or equal to a minimum length.
|
|
137
|
+
*/
|
|
95
138
|
Validators.minLength = createValueValidatorWithArgs(function (val, minLength) { return val.length >= minLength; }, function (maxLength) { return "Value must be at least ".concat(maxLength, " characters"); });
|
|
139
|
+
/**
|
|
140
|
+
* Validator to check if a value is within a specified range.
|
|
141
|
+
*/
|
|
142
|
+
Validators.range = createValueValidatorWithArgs(function (val, _b) {
|
|
143
|
+
var _c = tslib_1.__read(_b, 2), minValue = _c[0], maxValue = _c[1];
|
|
144
|
+
return val >= minValue && val <= maxValue;
|
|
145
|
+
}, function (_b) {
|
|
146
|
+
var _c = tslib_1.__read(_b, 2), minValue = _c[0], maxValue = _c[1];
|
|
147
|
+
return "Value must be between ".concat(minValue, " and ").concat(maxValue);
|
|
148
|
+
});
|
|
96
149
|
Validators.defaultMessage = 'Invalid value';
|
|
97
150
|
return Validators;
|
|
98
151
|
}());
|
|
99
152
|
exports.Validators = Validators;
|
|
153
|
+
/**
|
|
154
|
+
* Function to create a validator with a custom validation function.
|
|
155
|
+
*/
|
|
100
156
|
function createValidator(validate, defaultMessage) {
|
|
101
157
|
var _this = this;
|
|
102
158
|
var validation = function (entity, e, message) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
@@ -151,11 +207,17 @@ function createValidator(validate, defaultMessage) {
|
|
|
151
207
|
},
|
|
152
208
|
});
|
|
153
209
|
}
|
|
210
|
+
/**
|
|
211
|
+
* Function to create a value validator.
|
|
212
|
+
*/
|
|
154
213
|
function valueValidator(validate, defaultMessage) {
|
|
155
214
|
return function (entity, e) {
|
|
156
215
|
return validate(e.value) || defaultMessage || false;
|
|
157
216
|
};
|
|
158
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* Function to create a value validator with arguments.
|
|
220
|
+
*/
|
|
159
221
|
function createValueValidator(validate, defaultMessage) {
|
|
160
222
|
return createValidator(function (_, e) {
|
|
161
223
|
if (e.value === undefined || e.value === null)
|
|
@@ -163,6 +225,9 @@ function createValueValidator(validate, defaultMessage) {
|
|
|
163
225
|
return validate(e.value);
|
|
164
226
|
}, defaultMessage);
|
|
165
227
|
}
|
|
228
|
+
/**
|
|
229
|
+
* Function to create a value validator with arguments and a custom message.
|
|
230
|
+
*/
|
|
166
231
|
function createValueValidatorWithArgs(validate, defaultMessage) {
|
|
167
232
|
var result = createValidatorWithArgsInternal(function (_, e, args) {
|
|
168
233
|
if (e.value === undefined || e.value === null)
|
|
@@ -181,6 +246,9 @@ function createValueValidatorWithArgs(validate, defaultMessage) {
|
|
|
181
246
|
},
|
|
182
247
|
});
|
|
183
248
|
}
|
|
249
|
+
/**
|
|
250
|
+
* Function to create a validator with arguments and a custom message.
|
|
251
|
+
*/
|
|
184
252
|
function createValidatorWithArgs(validate, defaultMessage) {
|
|
185
253
|
return createValidatorWithArgsInternal(validate, defaultMessage);
|
|
186
254
|
}
|
|
@@ -223,6 +291,9 @@ function createValidatorWithArgsInternal(validate, defaultMessage, isValueValida
|
|
|
223
291
|
},
|
|
224
292
|
});
|
|
225
293
|
}
|
|
294
|
+
/**
|
|
295
|
+
* Function to get the values of an enum.
|
|
296
|
+
*/
|
|
226
297
|
function getEnumValues(enumObj) {
|
|
227
298
|
return Object.values(enumObj).filter(function (x) { return typeof enumObj[x] !== 'number'; });
|
|
228
299
|
}
|