@vsaas/loopback-datasource-juggler 10.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/LICENSE +25 -0
- package/NOTICE +23 -0
- package/README.md +74 -0
- package/dist/_virtual/_rolldown/runtime.js +4 -0
- package/dist/index.js +26 -0
- package/dist/lib/browser.depd.js +13 -0
- package/dist/lib/case-utils.js +21 -0
- package/dist/lib/connectors/kv-memory.js +158 -0
- package/dist/lib/connectors/memory.js +810 -0
- package/dist/lib/connectors/transient.js +126 -0
- package/dist/lib/dao.js +2445 -0
- package/dist/lib/datasource.js +2215 -0
- package/dist/lib/date-string.js +87 -0
- package/dist/lib/geo.js +244 -0
- package/dist/lib/globalize.js +33 -0
- package/dist/lib/hooks.js +79 -0
- package/dist/lib/id-utils.js +66 -0
- package/dist/lib/include.js +795 -0
- package/dist/lib/include_utils.js +104 -0
- package/dist/lib/introspection.js +37 -0
- package/dist/lib/jutil.js +65 -0
- package/dist/lib/kvao/delete-all.js +57 -0
- package/dist/lib/kvao/delete.js +43 -0
- package/dist/lib/kvao/expire.js +35 -0
- package/dist/lib/kvao/get.js +34 -0
- package/dist/lib/kvao/index.js +28 -0
- package/dist/lib/kvao/iterate-keys.js +38 -0
- package/dist/lib/kvao/keys.js +55 -0
- package/dist/lib/kvao/set.js +39 -0
- package/dist/lib/kvao/ttl.js +35 -0
- package/dist/lib/list.js +101 -0
- package/dist/lib/mixins.js +58 -0
- package/dist/lib/model-builder.js +608 -0
- package/dist/lib/model-definition.js +231 -0
- package/dist/lib/model-utils.js +368 -0
- package/dist/lib/model.js +586 -0
- package/dist/lib/observer.js +235 -0
- package/dist/lib/relation-definition.js +2604 -0
- package/dist/lib/relations.js +587 -0
- package/dist/lib/scope.js +392 -0
- package/dist/lib/transaction.js +183 -0
- package/dist/lib/types.js +58 -0
- package/dist/lib/utils.js +625 -0
- package/dist/lib/validations.js +742 -0
- package/dist/package.js +93 -0
- package/package.json +85 -0
- package/types/common.d.ts +28 -0
- package/types/connector.d.ts +52 -0
- package/types/datasource.d.ts +324 -0
- package/types/date-string.d.ts +21 -0
- package/types/inclusion-mixin.d.ts +44 -0
- package/types/index.d.ts +36 -0
- package/types/kv-model.d.ts +201 -0
- package/types/model.d.ts +368 -0
- package/types/observer-mixin.d.ts +174 -0
- package/types/persisted-model.d.ts +505 -0
- package/types/query.d.ts +108 -0
- package/types/relation-mixin.d.ts +577 -0
- package/types/relation.d.ts +301 -0
- package/types/scope.d.ts +92 -0
- package/types/transaction-mixin.d.ts +47 -0
- package/types/types.d.ts +65 -0
- package/types/validation-mixin.d.ts +287 -0
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
// Copyright IBM Corp. 2018. All Rights Reserved.
|
|
2
|
+
// Node module: loopback-datasource-juggler
|
|
3
|
+
// This file is licensed under the MIT License.
|
|
4
|
+
// License text available at https://opensource.org/licenses/MIT
|
|
5
|
+
|
|
6
|
+
import { Callback, Options, PromiseOrVoid } from './common';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* This class provides methods that add validation cababilities to models.
|
|
10
|
+
* Each of the validations runs when the `obj.isValid()` method is called.
|
|
11
|
+
*
|
|
12
|
+
* All of the methods have an options object parameter that has a
|
|
13
|
+
* `message` property. When there is only a single error message, this
|
|
14
|
+
* property is just a string;
|
|
15
|
+
* for example: `Post.validatesPresenceOf('title', { message: 'can not be blank' });`
|
|
16
|
+
*
|
|
17
|
+
* In more complicated cases it can be a set of messages, for each possible
|
|
18
|
+
* error condition; for example:
|
|
19
|
+
* `User.validatesLengthOf('password', { min: 6, max: 20, message: {min: 'too short', max: 'too long'}});`
|
|
20
|
+
*
|
|
21
|
+
*/
|
|
22
|
+
export interface Validatable {
|
|
23
|
+
/**
|
|
24
|
+
* Validate presence of one or more specified properties.
|
|
25
|
+
*
|
|
26
|
+
* Requires a model to include a property to be considered valid; fails when validated field is blank.
|
|
27
|
+
*
|
|
28
|
+
* For example, validate presence of title
|
|
29
|
+
* ```
|
|
30
|
+
* Post.validatesPresenceOf('title');
|
|
31
|
+
* ```
|
|
32
|
+
* Validate that model has first, last, and age properties:
|
|
33
|
+
* ```
|
|
34
|
+
* User.validatesPresenceOf('first', 'last', 'age');
|
|
35
|
+
* ```
|
|
36
|
+
* Example with custom message
|
|
37
|
+
* ```
|
|
38
|
+
* Post.validatesPresenceOf('title', {message: 'Cannot be blank'});
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @param {String} propertyName One or more property names.
|
|
42
|
+
* @options {Object} options Configuration parameters; see below.
|
|
43
|
+
* @property {String} message Error message to use instead of default.
|
|
44
|
+
* @property {String} if Validate only if `if` exists.
|
|
45
|
+
* @property {String} unless Validate only if `unless` exists.
|
|
46
|
+
*/
|
|
47
|
+
validatesPresenceOf(...propertyNames: string[]): void;
|
|
48
|
+
validatesPresenceOf(propertyName: string, options?: Options): void;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Validate absence of one or more specified properties.
|
|
52
|
+
*
|
|
53
|
+
* A model should not include a property to be considered valid; fails when validated field is not blank.
|
|
54
|
+
*
|
|
55
|
+
* For example, validate absence of reserved
|
|
56
|
+
* ```
|
|
57
|
+
* Post.validatesAbsenceOf('reserved', { unless: 'special' });
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* @param {String} propertyName One or more property names.
|
|
61
|
+
* @options {Object} options Configuration parameters; see below.
|
|
62
|
+
* @property {String} message Error message to use instead of default.
|
|
63
|
+
* @property {String} if Validate only if `if` exists.
|
|
64
|
+
* @property {String} unless Validate only if `unless` exists.
|
|
65
|
+
*/
|
|
66
|
+
validatesAbsenceOf(...propertyNames: string[]): void;
|
|
67
|
+
validatesAbsenceOf(propertyName: string, options?: Options): void;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Validate length.
|
|
71
|
+
*
|
|
72
|
+
* Require a property length to be within a specified range.
|
|
73
|
+
*
|
|
74
|
+
* There are three kinds of validations: min, max, is.
|
|
75
|
+
*
|
|
76
|
+
* Default error messages:
|
|
77
|
+
*
|
|
78
|
+
* - min: too short
|
|
79
|
+
* - max: too long
|
|
80
|
+
* - is: length is wrong
|
|
81
|
+
*
|
|
82
|
+
* Example: length validations
|
|
83
|
+
* ```
|
|
84
|
+
* User.validatesLengthOf('password', {min: 7});
|
|
85
|
+
* User.validatesLengthOf('email', {max: 100});
|
|
86
|
+
* User.validatesLengthOf('state', {is: 2});
|
|
87
|
+
* User.validatesLengthOf('nick', {min: 3, max: 15});
|
|
88
|
+
* ```
|
|
89
|
+
* Example: length validations with custom error messages
|
|
90
|
+
* ```
|
|
91
|
+
* User.validatesLengthOf('password', {min: 7, message: {min: 'too weak'}});
|
|
92
|
+
* User.validatesLengthOf('state', {is: 2, message: {is: 'is not valid state name'}});
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* @param {String} propertyName Property name to validate.
|
|
96
|
+
* @options {Object} options Configuration parameters; see below.
|
|
97
|
+
* @property {Number} is Value that property must equal to validate.
|
|
98
|
+
* @property {Number} min Value that property must be less than to be valid.
|
|
99
|
+
* @property {Number} max Value that property must be less than to be valid.
|
|
100
|
+
* @property {Object} message Optional object with string properties for custom error message for each validation: is, min, or max.
|
|
101
|
+
*/
|
|
102
|
+
validatesLengthOf(propertyName: string, options?: Options): void;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Validate numericality.
|
|
106
|
+
*
|
|
107
|
+
* Requires a value for property to be either an integer or number.
|
|
108
|
+
*
|
|
109
|
+
* Example
|
|
110
|
+
* ```
|
|
111
|
+
* User.validatesNumericalityOf('age', { message: { number: 'is not a number' }});
|
|
112
|
+
* User.validatesNumericalityOf('age', {int: true, message: { int: 'is not an integer' }});
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* @param {String} propertyName Property name to validate.
|
|
116
|
+
* @options {Object} options Configuration parameters; see below.
|
|
117
|
+
* @property {Boolean} int If true, then property must be an integer to be valid.
|
|
118
|
+
* @property {Boolean} allowBlank Allow property to be blank.
|
|
119
|
+
* @property {Boolean} allowNull Allow property to be null.
|
|
120
|
+
* @property {Object} message Optional object with string properties for 'int' for integer validation. Default error messages:
|
|
121
|
+
* - number: is not a number
|
|
122
|
+
* - int: is not an integer
|
|
123
|
+
*/
|
|
124
|
+
validatesNumericalityOf(propertyName: string, options?: Options): void;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Validate inclusion in set.
|
|
128
|
+
*
|
|
129
|
+
* Require a value for property to be in the specified array.
|
|
130
|
+
*
|
|
131
|
+
* Example:
|
|
132
|
+
* ```
|
|
133
|
+
* User.validatesInclusionOf('gender', {in: ['male', 'female']});
|
|
134
|
+
* User.validatesInclusionOf('role', {
|
|
135
|
+
* in: ['admin', 'moderator', 'user'], message: 'is not allowed'
|
|
136
|
+
* });
|
|
137
|
+
* ```
|
|
138
|
+
*
|
|
139
|
+
* @param {String} propertyName Property name to validate.
|
|
140
|
+
* @options {Object} options Configuration parameters; see below.
|
|
141
|
+
* @property {Array} in Property must match one of the values in the array to be valid.
|
|
142
|
+
* @property {String} message Optional error message if property is not valid.
|
|
143
|
+
* Default error message: "is not included in the list".
|
|
144
|
+
* @property {Boolean} allowNull Whether null values are allowed.
|
|
145
|
+
*/
|
|
146
|
+
validatesInclusionOf(propertyName: string, options?: Options): void;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Validate exclusion in a set.
|
|
150
|
+
*
|
|
151
|
+
* Require a property value not be in the specified array.
|
|
152
|
+
*
|
|
153
|
+
* Example: `Company.validatesExclusionOf('domain', {in: ['www', 'admin']});`
|
|
154
|
+
*
|
|
155
|
+
* @param {String} propertyName Property name to validate.
|
|
156
|
+
* @options {Object} options Configuration parameters; see below.
|
|
157
|
+
* @property {Array} in Property must not match any of the values in the array to be valid.
|
|
158
|
+
* @property {String} message Optional error message if property is not valid. Default error message: "is reserved".
|
|
159
|
+
* @property {Boolean} allowNull Whether null values are allowed.
|
|
160
|
+
*/
|
|
161
|
+
validatesExclusionOf(propertyName: string, options?: Options): void;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Validate format.
|
|
165
|
+
*
|
|
166
|
+
* Require a model to include a property that matches the given format.
|
|
167
|
+
*
|
|
168
|
+
* Example: `User.validatesFormatOf('name', {with: /\w+/});`
|
|
169
|
+
*
|
|
170
|
+
* @param {String} propertyName Property name to validate.
|
|
171
|
+
* @options {Object} options Configuration parameters; see below.
|
|
172
|
+
* @property {RegExp} with Regular expression to validate format.
|
|
173
|
+
* @property {String} message Optional error message if property is not valid. Default error message: " is invalid".
|
|
174
|
+
* @property {Boolean} allowNull Whether null values are allowed.
|
|
175
|
+
*/
|
|
176
|
+
validatesFormatOf(propertyName: string, options?: Options): void;
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Validate using custom validation function.
|
|
180
|
+
*
|
|
181
|
+
* Example:
|
|
182
|
+
*```javascript
|
|
183
|
+
* User.validate('name', customValidator, {message: 'Bad name'});
|
|
184
|
+
* function customValidator(err) {
|
|
185
|
+
* if (this.name === 'bad') err();
|
|
186
|
+
* });
|
|
187
|
+
* var user = new User({name: 'Peter'});
|
|
188
|
+
* user.isValid(); // true
|
|
189
|
+
* user.name = 'bad';
|
|
190
|
+
* user.isValid(); // false
|
|
191
|
+
* ```
|
|
192
|
+
*
|
|
193
|
+
* @param {String} propertyName Property name to validate.
|
|
194
|
+
* @param {Function} validatorFn Custom validation function.
|
|
195
|
+
* @options {Object} options Configuration parameters; see below.
|
|
196
|
+
* @property {String} message Optional error message if property is not valid. Default error message: " is invalid".
|
|
197
|
+
* @property {Boolean} allowNull Whether null values are allowed.
|
|
198
|
+
*/
|
|
199
|
+
validate(propertyName: string, validatorFn: Function, options?: Options): void;
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Validate using custom asynchronous validation function.
|
|
203
|
+
*
|
|
204
|
+
* Example:
|
|
205
|
+
*```js
|
|
206
|
+
* User.validateAsync('name', customValidator, {message: 'Bad name'});
|
|
207
|
+
* function customValidator(err, done) {
|
|
208
|
+
* process.nextTick(function () {
|
|
209
|
+
* if (this.name === 'bad') err();
|
|
210
|
+
* done();
|
|
211
|
+
* });
|
|
212
|
+
* });
|
|
213
|
+
* var user = new User({name: 'Peter'});
|
|
214
|
+
* user.isValid(); // false (because async validation setup)
|
|
215
|
+
* user.isValid(function (isValid) {
|
|
216
|
+
* isValid; // true
|
|
217
|
+
* })
|
|
218
|
+
* user.name = 'bad';
|
|
219
|
+
* user.isValid(); // false
|
|
220
|
+
* user.isValid(function (isValid) {
|
|
221
|
+
* isValid; // false
|
|
222
|
+
* })
|
|
223
|
+
* ```
|
|
224
|
+
*
|
|
225
|
+
* @param {String} propertyName Property name to validate.
|
|
226
|
+
* @param {Function} validatorFn Custom validation function.
|
|
227
|
+
* @options {Object} options Configuration parameters; see below.
|
|
228
|
+
* @property {String} message Optional error message if property is not valid. Default error message: " is invalid".
|
|
229
|
+
* @property {Boolean} allowNull Whether null values are allowed.
|
|
230
|
+
*/
|
|
231
|
+
validateAsync(propertyName: string, validatorFn: Function, options?: Options): void;
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Validate uniqueness of the value for a property in the collection of models.
|
|
235
|
+
*
|
|
236
|
+
* Not available for all connectors. Currently supported with these connectors:
|
|
237
|
+
* - In Memory
|
|
238
|
+
* - Oracle
|
|
239
|
+
* - MongoDB
|
|
240
|
+
*
|
|
241
|
+
* ```
|
|
242
|
+
* // The login must be unique across all User instances.
|
|
243
|
+
* User.validatesUniquenessOf('login');
|
|
244
|
+
*
|
|
245
|
+
* // Assuming SiteUser.belongsTo(Site)
|
|
246
|
+
* // The login must be unique within each Site.
|
|
247
|
+
* SiteUser.validateUniquenessOf('login', { scopedTo: ['siteId'] });
|
|
248
|
+
* ```
|
|
249
|
+
*
|
|
250
|
+
* @param {String} propertyName Property name to validate.
|
|
251
|
+
* @options {Object} options Configuration parameters; see below.
|
|
252
|
+
* @property {RegExp} with Regular expression to validate format.
|
|
253
|
+
* @property {Array.<String>} scopedTo List of properties defining the scope.
|
|
254
|
+
* @property {String} message Optional error message if property is not valid. Default error message: "is not unique".
|
|
255
|
+
* @property {Boolean} allowNull Whether null values are allowed.
|
|
256
|
+
* @property {String} ignoreCase Make the validation case insensitive.
|
|
257
|
+
* @property {String} if Validate only if `if` exists.
|
|
258
|
+
* @property {String} unless Validate only if `unless` exists.
|
|
259
|
+
*/
|
|
260
|
+
validatesUniquenessOf(propertyName: string, validatorFn: Function, options?: Options): void;
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Validate if a value for a property is a Date.
|
|
264
|
+
*
|
|
265
|
+
* Example
|
|
266
|
+
* ```
|
|
267
|
+
* User.validatesDateOf('today', {message: 'today is not a date!'});
|
|
268
|
+
* ```
|
|
269
|
+
*
|
|
270
|
+
* @param {String} propertyName Property name to validate.
|
|
271
|
+
* @options {Object} options Configuration parameters; see below.
|
|
272
|
+
* @property {String} message Error message to use instead of default.
|
|
273
|
+
*/
|
|
274
|
+
validatesDateOf(propertyName: string, validatorFn: Function, options?: Options): void;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* ValidationError
|
|
279
|
+
*/
|
|
280
|
+
export declare class ValidationError extends Error {
|
|
281
|
+
statusCode?: number;
|
|
282
|
+
details: {
|
|
283
|
+
context: any;
|
|
284
|
+
codes: string[];
|
|
285
|
+
messages: string[];
|
|
286
|
+
};
|
|
287
|
+
}
|