remult 0.27.23-next.0 → 0.27.23-next.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.
@@ -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,27 @@ 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`);
52
95
  static defaultMessage = 'Invalid value';
53
96
  }
97
+ /**
98
+ * Function to create a validator with a custom validation function.
99
+ */
54
100
  export function createValidator(validate, defaultMessage) {
55
101
  const validation = async (entity, e, message) => {
56
102
  const valid = await validate(entity, e);
@@ -87,9 +133,15 @@ export function createValidator(validate, defaultMessage) {
87
133
  withMessage: (message) => async (entity, e) => result(entity, e, message),
88
134
  });
89
135
  }
136
+ /**
137
+ * Function to create a value validator.
138
+ */
90
139
  export function valueValidator(validate, defaultMessage) {
91
140
  return (entity, e) => validate(e.value) || defaultMessage || false;
92
141
  }
142
+ /**
143
+ * Function to create a value validator with arguments.
144
+ */
93
145
  export function createValueValidator(validate, defaultMessage) {
94
146
  return createValidator((_, e) => {
95
147
  if (e.value === undefined || e.value === null)
@@ -97,6 +149,9 @@ export function createValueValidator(validate, defaultMessage) {
97
149
  return validate(e.value);
98
150
  }, defaultMessage);
99
151
  }
152
+ /**
153
+ * Function to create a value validator with arguments and a custom message.
154
+ */
100
155
  export function createValueValidatorWithArgs(validate, defaultMessage) {
101
156
  const result = createValidatorWithArgsInternal((_, e, args) => {
102
157
  if (e.value === undefined || e.value === null)
@@ -113,6 +168,9 @@ export function createValueValidatorWithArgs(validate, defaultMessage) {
113
168
  },
114
169
  });
115
170
  }
171
+ /**
172
+ * Function to create a validator with arguments and a custom message.
173
+ */
116
174
  export function createValidatorWithArgs(validate, defaultMessage) {
117
175
  return createValidatorWithArgsInternal(validate, defaultMessage);
118
176
  }
@@ -143,6 +201,9 @@ function createValidatorWithArgsInternal(validate, defaultMessage, isValueValida
143
201
  },
144
202
  });
145
203
  }
204
+ /**
205
+ * Function to get the values of an enum.
206
+ */
146
207
  export function getEnumValues(enumObj) {
147
208
  return Object.values(enumObj).filter((x) => typeof enumObj[x] !== 'number');
148
209
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "remult",
3
- "version": "0.27.23-next.0",
3
+ "version": "0.27.23-next.1",
4
4
  "description": "A CRUD framework for full-stack TypeScript",
5
5
  "homepage": "https://remult.dev",
6
6
  "repository": {
@@ -1,56 +1,129 @@
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
  };
35
78
  static defaultMessage: string;
36
79
  }
80
+ /**
81
+ * Type representing a field validator with an optional message.
82
+ */
37
83
  export type Validator<valueType> = FieldValidator<unknown, valueType> & ((message?: ValidationMessage<valueType, undefined>) => FieldValidator<unknown, valueType>) & {
38
84
  defaultMessage: ValidationMessage<valueType, undefined>;
39
85
  /**
40
- * @deprecated use (message:string) instead - for example: Validators.required("Is needed")
86
+ * @deprecated use (message:string) instead - for example: Validators.required("Is needed")
41
87
  */
42
88
  withMessage(message: ValidationMessage<valueType, undefined>): FieldValidator<unknown, valueType>;
43
89
  };
90
+ /**
91
+ * Function to create a validator with a custom validation function.
92
+ */
44
93
  export declare function createValidator<valueType>(validate: (entity: any, e: ValidateFieldEvent<any, valueType>) => Promise<boolean | string> | boolean | string, defaultMessage?: ValidationMessage<valueType, undefined>): Validator<valueType>;
94
+ /**
95
+ * Function to create a value validator.
96
+ */
45
97
  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>;
98
+ /**
99
+ * Function to create a value validator with arguments.
100
+ */
46
101
  export declare function createValueValidator<valueType>(validate: (value: valueType) => boolean | string | Promise<boolean | string>, defaultMessage?: ValidationMessage<valueType, undefined>): Validator<valueType>;
102
+ /**
103
+ * Function to create a value validator with arguments and a custom message.
104
+ */
47
105
  export declare function createValueValidatorWithArgs<valueType, argsType>(validate: (value: valueType, args: argsType) => boolean | string | Promise<boolean | string>, defaultMessage?: ValueValidationMessage<argsType>): ValidatorWithArgs<valueType, argsType> & {
48
106
  defaultMessage: ValueValidationMessage<argsType>;
49
107
  };
108
+ /**
109
+ * Type representing a validation message that can be a string or a function.
110
+ */
50
111
  export type ValueValidationMessage<argsType> = string | ((args: argsType) => string);
112
+ /**
113
+ * Type representing a validation message with additional parameters.
114
+ */
51
115
  export type ValidationMessage<valueType, argsType> = string | ((entity: any, event: ValidateFieldEvent<any, valueType>, args: argsType) => string);
116
+ /**
117
+ * Type representing a validator with arguments.
118
+ */
52
119
  export type ValidatorWithArgs<valueType, argsType> = (args: argsType, message?: ValidationMessage<valueType, argsType>) => FieldValidator<unknown, valueType>;
120
+ /**
121
+ * Function to create a validator with arguments and a custom message.
122
+ */
53
123
  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
124
  defaultMessage: ValidationMessage<valueType, argsType>;
55
125
  };
126
+ /**
127
+ * Function to get the values of an enum.
128
+ */
56
129
  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,29 @@ 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"); });
96
139
  Validators.defaultMessage = 'Invalid value';
97
140
  return Validators;
98
141
  }());
99
142
  exports.Validators = Validators;
143
+ /**
144
+ * Function to create a validator with a custom validation function.
145
+ */
100
146
  function createValidator(validate, defaultMessage) {
101
147
  var _this = this;
102
148
  var validation = function (entity, e, message) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
@@ -151,11 +197,17 @@ function createValidator(validate, defaultMessage) {
151
197
  },
152
198
  });
153
199
  }
200
+ /**
201
+ * Function to create a value validator.
202
+ */
154
203
  function valueValidator(validate, defaultMessage) {
155
204
  return function (entity, e) {
156
205
  return validate(e.value) || defaultMessage || false;
157
206
  };
158
207
  }
208
+ /**
209
+ * Function to create a value validator with arguments.
210
+ */
159
211
  function createValueValidator(validate, defaultMessage) {
160
212
  return createValidator(function (_, e) {
161
213
  if (e.value === undefined || e.value === null)
@@ -163,6 +215,9 @@ function createValueValidator(validate, defaultMessage) {
163
215
  return validate(e.value);
164
216
  }, defaultMessage);
165
217
  }
218
+ /**
219
+ * Function to create a value validator with arguments and a custom message.
220
+ */
166
221
  function createValueValidatorWithArgs(validate, defaultMessage) {
167
222
  var result = createValidatorWithArgsInternal(function (_, e, args) {
168
223
  if (e.value === undefined || e.value === null)
@@ -181,6 +236,9 @@ function createValueValidatorWithArgs(validate, defaultMessage) {
181
236
  },
182
237
  });
183
238
  }
239
+ /**
240
+ * Function to create a validator with arguments and a custom message.
241
+ */
184
242
  function createValidatorWithArgs(validate, defaultMessage) {
185
243
  return createValidatorWithArgsInternal(validate, defaultMessage);
186
244
  }
@@ -223,6 +281,9 @@ function createValidatorWithArgsInternal(validate, defaultMessage, isValueValida
223
281
  },
224
282
  });
225
283
  }
284
+ /**
285
+ * Function to get the values of an enum.
286
+ */
226
287
  function getEnumValues(enumObj) {
227
288
  return Object.values(enumObj).filter(function (x) { return typeof enumObj[x] !== 'number'; });
228
289
  }