@e22m4u/js-repository 0.8.4 → 0.8.6
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/README.md +37 -49
- package/dist/cjs/index.cjs +762 -353
- package/eslint.config.js +1 -0
- package/package.json +14 -14
- package/src/adapter/adapter-loader.js +9 -4
- package/src/adapter/adapter-registry.js +3 -1
- package/src/adapter/builtin/memory-adapter.js +29 -13
- package/src/adapter/decorator/data-sanitizing-decorator.js +2 -1
- package/src/adapter/decorator/default-values-decorator.js +2 -1
- package/src/adapter/decorator/fields-filtering-decorator.js +14 -7
- package/src/adapter/decorator/inclusion-decorator.js +14 -7
- package/src/adapter/decorator/property-uniqueness-decorator.js +2 -1
- package/src/adapter/decorator/required-property-decorator.js +2 -1
- package/src/definition/datasource/datasource-definition-validator.js +6 -3
- package/src/definition/definition-registry.js +8 -4
- package/src/definition/model/model-data-sanitizer.js +4 -2
- package/src/definition/model/model-definition-utils.js +74 -35
- package/src/definition/model/model-definition-utils.spec.js +2 -6
- package/src/definition/model/model-definition-validator.js +10 -5
- package/src/definition/model/properties/primary-keys-definition-validator.js +4 -2
- package/src/definition/model/properties/properties-definition-validator.js +36 -18
- package/src/definition/model/properties/property-uniqueness-validator.js +30 -18
- package/src/definition/model/properties/property-uniqueness-validator.spec.js +734 -74
- package/src/definition/model/properties/required-property-validator.js +7 -12
- package/src/definition/model/properties/required-property-validator.spec.js +7 -46
- package/src/definition/model/relations/relations-definition-validator.js +70 -33
- package/src/filter/fields-clause-tool.js +31 -12
- package/src/filter/include-clause-tool.js +38 -15
- package/src/filter/operator-clause-tool.js +55 -23
- package/src/filter/order-clause-tool.js +36 -13
- package/src/filter/slice-clause-tool.js +16 -7
- package/src/filter/where-clause-tool.js +24 -10
- package/src/relations/belongs-to-resolver.js +44 -20
- package/src/relations/has-many-resolver.js +52 -25
- package/src/relations/has-one-resolver.js +58 -27
- package/src/relations/references-many-resolver.js +24 -11
- package/src/repository/repository-registry.js +3 -1
- package/src/repository/repository.js +2 -1
- package/src/utils/capitalize.js +3 -1
- package/src/utils/clone-deep.js +6 -2
- package/src/utils/exclude-object-keys.js +2 -1
- package/src/utils/get-value-by-path.js +6 -2
- package/src/utils/is-deep-equal.js +21 -7
- package/src/utils/is-promise.js +6 -2
- package/src/utils/model-name-to-model-key.js +2 -1
- package/src/utils/select-object-keys.js +9 -4
- package/src/utils/singularize.js +3 -1
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import {DataType} from './data-type.js';
|
|
2
2
|
import {Service} from '@e22m4u/js-service';
|
|
3
|
-
import {BlankValuesService} from '@e22m4u/js-empty-values';
|
|
4
3
|
import {InvalidArgumentError} from '../../../errors/index.js';
|
|
5
4
|
import {ModelDefinitionUtils} from '../model-definition-utils.js';
|
|
6
5
|
|
|
@@ -44,7 +43,6 @@ export class RequiredPropertyValidator extends Service {
|
|
|
44
43
|
ModelDefinitionUtils,
|
|
45
44
|
).getPropertiesDefinitionInBaseModelHierarchy(modelName);
|
|
46
45
|
const propNames = Object.keys(isPartial ? modelData : propDefs);
|
|
47
|
-
const blankValuesService = this.getService(BlankValuesService);
|
|
48
46
|
for (const propName of propNames) {
|
|
49
47
|
const propDef = propDefs[propName];
|
|
50
48
|
if (!propDef || typeof propDef !== 'object') {
|
|
@@ -52,16 +50,13 @@ export class RequiredPropertyValidator extends Service {
|
|
|
52
50
|
}
|
|
53
51
|
// проверка основного значения
|
|
54
52
|
const propValue = modelData[propName];
|
|
55
|
-
if (propDef.required) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
propValue,
|
|
63
|
-
);
|
|
64
|
-
}
|
|
53
|
+
if (propDef.required && propValue == null) {
|
|
54
|
+
throw new InvalidArgumentError(
|
|
55
|
+
'Property %v of the model %v is required, but %v was given.',
|
|
56
|
+
propName,
|
|
57
|
+
modelName,
|
|
58
|
+
propValue,
|
|
59
|
+
);
|
|
65
60
|
}
|
|
66
61
|
// проверка вложенного объекта
|
|
67
62
|
if (
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import {expect} from 'chai';
|
|
2
|
+
import {DataType} from './data-type.js';
|
|
2
3
|
import {format} from '@e22m4u/js-format';
|
|
3
4
|
import {DatabaseSchema} from '../../../database-schema.js';
|
|
4
5
|
import {RequiredPropertyValidator} from './required-property-validator.js';
|
|
5
|
-
import {DataType} from './data-type.js';
|
|
6
|
-
import {BlankValuesService} from '@e22m4u/js-empty-values';
|
|
7
6
|
|
|
8
7
|
describe('RequiredPropertyValidator', function () {
|
|
9
8
|
describe('validate', function () {
|
|
@@ -84,11 +83,9 @@ describe('RequiredPropertyValidator', function () {
|
|
|
84
83
|
S.validate('model', {foo: 'bar'});
|
|
85
84
|
});
|
|
86
85
|
|
|
87
|
-
it('should not throw an error if a required property is
|
|
86
|
+
it('should not throw an error if a required property is defined', function () {
|
|
88
87
|
const dbs = new DatabaseSchema();
|
|
89
88
|
const S = dbs.getService(RequiredPropertyValidator);
|
|
90
|
-
const blankValues = S.getService(BlankValuesService);
|
|
91
|
-
blankValues.setBlankValues([undefined]);
|
|
92
89
|
dbs.defineModel({
|
|
93
90
|
name: 'model',
|
|
94
91
|
properties: {
|
|
@@ -101,11 +98,9 @@ describe('RequiredPropertyValidator', function () {
|
|
|
101
98
|
S.validate('model', {foo: 'bar'});
|
|
102
99
|
});
|
|
103
100
|
|
|
104
|
-
it('should throw an error if a required property is
|
|
101
|
+
it('should throw an error if a required property is undefined', function () {
|
|
105
102
|
const dbs = new DatabaseSchema();
|
|
106
103
|
const S = dbs.getService(RequiredPropertyValidator);
|
|
107
|
-
const blankValues = S.getService(BlankValuesService);
|
|
108
|
-
blankValues.setBlankValues([undefined]);
|
|
109
104
|
dbs.defineModel({
|
|
110
105
|
name: 'model',
|
|
111
106
|
properties: {
|
|
@@ -126,8 +121,6 @@ describe('RequiredPropertyValidator', function () {
|
|
|
126
121
|
it('should not throw an error if no data is provided for an embedded model', function () {
|
|
127
122
|
const dbs = new DatabaseSchema();
|
|
128
123
|
const S = dbs.getService(RequiredPropertyValidator);
|
|
129
|
-
const blankValues = S.getService(BlankValuesService);
|
|
130
|
-
blankValues.setBlankValues([undefined]);
|
|
131
124
|
dbs.defineModel({
|
|
132
125
|
name: 'modelA',
|
|
133
126
|
properties: {
|
|
@@ -151,8 +144,6 @@ describe('RequiredPropertyValidator', function () {
|
|
|
151
144
|
it('should throw an error if an embedded model is required but not provided', function () {
|
|
152
145
|
const dbs = new DatabaseSchema();
|
|
153
146
|
const S = dbs.getService(RequiredPropertyValidator);
|
|
154
|
-
const blankValues = S.getService(BlankValuesService);
|
|
155
|
-
blankValues.setBlankValues([undefined]);
|
|
156
147
|
dbs.defineModel({
|
|
157
148
|
name: 'modelA',
|
|
158
149
|
properties: {
|
|
@@ -181,8 +172,6 @@ describe('RequiredPropertyValidator', function () {
|
|
|
181
172
|
it('should allow a model data to have properties without a specified schema', function () {
|
|
182
173
|
const dbs = new DatabaseSchema();
|
|
183
174
|
const S = dbs.getService(RequiredPropertyValidator);
|
|
184
|
-
const blankValues = S.getService(BlankValuesService);
|
|
185
|
-
blankValues.setBlankValues([undefined]);
|
|
186
175
|
dbs.defineModel({
|
|
187
176
|
name: 'modelA',
|
|
188
177
|
properties: {
|
|
@@ -206,8 +195,6 @@ describe('RequiredPropertyValidator', function () {
|
|
|
206
195
|
it('should allow omit a model data when its model has a required property', function () {
|
|
207
196
|
const dbs = new DatabaseSchema();
|
|
208
197
|
const S = dbs.getService(RequiredPropertyValidator);
|
|
209
|
-
const blankValues = S.getService(BlankValuesService);
|
|
210
|
-
blankValues.setBlankValues([undefined]);
|
|
211
198
|
dbs.defineModel({
|
|
212
199
|
name: 'modelA',
|
|
213
200
|
properties: {
|
|
@@ -232,8 +219,6 @@ describe('RequiredPropertyValidator', function () {
|
|
|
232
219
|
it('should allow omit an optional property for an embedded model', function () {
|
|
233
220
|
const dbs = new DatabaseSchema();
|
|
234
221
|
const S = dbs.getService(RequiredPropertyValidator);
|
|
235
|
-
const blankValues = S.getService(BlankValuesService);
|
|
236
|
-
blankValues.setBlankValues([undefined]);
|
|
237
222
|
dbs.defineModel({
|
|
238
223
|
name: 'modelA',
|
|
239
224
|
properties: {
|
|
@@ -257,8 +242,6 @@ describe('RequiredPropertyValidator', function () {
|
|
|
257
242
|
it('should throw an error if a required property is not provided', function () {
|
|
258
243
|
const dbs = new DatabaseSchema();
|
|
259
244
|
const S = dbs.getService(RequiredPropertyValidator);
|
|
260
|
-
const blankValues = S.getService(BlankValuesService);
|
|
261
|
-
blankValues.setBlankValues([undefined]);
|
|
262
245
|
dbs.defineModel({
|
|
263
246
|
name: 'modelA',
|
|
264
247
|
properties: {
|
|
@@ -289,8 +272,6 @@ describe('RequiredPropertyValidator', function () {
|
|
|
289
272
|
it('should allow omit an optional array', function () {
|
|
290
273
|
const dbs = new DatabaseSchema();
|
|
291
274
|
const S = dbs.getService(RequiredPropertyValidator);
|
|
292
|
-
const blankValues = S.getService(BlankValuesService);
|
|
293
|
-
blankValues.setBlankValues([undefined]);
|
|
294
275
|
dbs.defineModel({
|
|
295
276
|
name: 'modelA',
|
|
296
277
|
properties: {
|
|
@@ -315,8 +296,6 @@ describe('RequiredPropertyValidator', function () {
|
|
|
315
296
|
it('should allow a required array to be empty', function () {
|
|
316
297
|
const dbs = new DatabaseSchema();
|
|
317
298
|
const S = dbs.getService(RequiredPropertyValidator);
|
|
318
|
-
const blankValues = S.getService(BlankValuesService);
|
|
319
|
-
blankValues.setBlankValues([undefined]);
|
|
320
299
|
dbs.defineModel({
|
|
321
300
|
name: 'modelA',
|
|
322
301
|
properties: {
|
|
@@ -339,11 +318,9 @@ describe('RequiredPropertyValidator', function () {
|
|
|
339
318
|
S.validate('modelA', {array: []});
|
|
340
319
|
});
|
|
341
320
|
|
|
342
|
-
it('should allow omit an optional array even if
|
|
321
|
+
it('should allow omit an optional array even if an item model has a required property', function () {
|
|
343
322
|
const dbs = new DatabaseSchema();
|
|
344
323
|
const S = dbs.getService(RequiredPropertyValidator);
|
|
345
|
-
const blankValues = S.getService(BlankValuesService);
|
|
346
|
-
blankValues.setBlankValues([undefined]);
|
|
347
324
|
dbs.defineModel({
|
|
348
325
|
name: 'modelA',
|
|
349
326
|
properties: {
|
|
@@ -366,11 +343,9 @@ describe('RequiredPropertyValidator', function () {
|
|
|
366
343
|
S.validate('modelA', {});
|
|
367
344
|
});
|
|
368
345
|
|
|
369
|
-
it('should allow an empty array even if
|
|
346
|
+
it('should allow an empty array even if an item model has a required property', function () {
|
|
370
347
|
const dbs = new DatabaseSchema();
|
|
371
348
|
const S = dbs.getService(RequiredPropertyValidator);
|
|
372
|
-
const blankValues = S.getService(BlankValuesService);
|
|
373
|
-
blankValues.setBlankValues([undefined]);
|
|
374
349
|
dbs.defineModel({
|
|
375
350
|
name: 'modelA',
|
|
376
351
|
properties: {
|
|
@@ -396,8 +371,6 @@ describe('RequiredPropertyValidator', function () {
|
|
|
396
371
|
it('should throw an error when a required array is not provided', function () {
|
|
397
372
|
const dbs = new DatabaseSchema();
|
|
398
373
|
const S = dbs.getService(RequiredPropertyValidator);
|
|
399
|
-
const blankValues = S.getService(BlankValuesService);
|
|
400
|
-
blankValues.setBlankValues([undefined]);
|
|
401
374
|
dbs.defineModel({
|
|
402
375
|
name: 'modelA',
|
|
403
376
|
properties: {
|
|
@@ -427,8 +400,6 @@ describe('RequiredPropertyValidator', function () {
|
|
|
427
400
|
it('should allow omit an optional property of the item model', function () {
|
|
428
401
|
const dbs = new DatabaseSchema();
|
|
429
402
|
const S = dbs.getService(RequiredPropertyValidator);
|
|
430
|
-
const blankValues = S.getService(BlankValuesService);
|
|
431
|
-
blankValues.setBlankValues([undefined]);
|
|
432
403
|
dbs.defineModel({
|
|
433
404
|
name: 'modelA',
|
|
434
405
|
properties: {
|
|
@@ -454,8 +425,6 @@ describe('RequiredPropertyValidator', function () {
|
|
|
454
425
|
it('should allow an item date to have properties without a specified schema', function () {
|
|
455
426
|
const dbs = new DatabaseSchema();
|
|
456
427
|
const S = dbs.getService(RequiredPropertyValidator);
|
|
457
|
-
const blankValues = S.getService(BlankValuesService);
|
|
458
|
-
blankValues.setBlankValues([undefined]);
|
|
459
428
|
dbs.defineModel({
|
|
460
429
|
name: 'modelA',
|
|
461
430
|
properties: {
|
|
@@ -480,11 +449,9 @@ describe('RequiredPropertyValidator', function () {
|
|
|
480
449
|
});
|
|
481
450
|
|
|
482
451
|
describe('isPartial', function () {
|
|
483
|
-
it('should throw an error if a required property is
|
|
452
|
+
it('should throw an error if a required property is undefined', function () {
|
|
484
453
|
const dbs = new DatabaseSchema();
|
|
485
454
|
const S = dbs.getService(RequiredPropertyValidator);
|
|
486
|
-
const blankValues = S.getService(BlankValuesService);
|
|
487
|
-
blankValues.setBlankValues([undefined]);
|
|
488
455
|
dbs.defineModel({
|
|
489
456
|
name: 'model',
|
|
490
457
|
properties: {
|
|
@@ -501,11 +468,9 @@ describe('RequiredPropertyValidator', function () {
|
|
|
501
468
|
);
|
|
502
469
|
});
|
|
503
470
|
|
|
504
|
-
it('should not validate
|
|
471
|
+
it('should not validate required but not provided properties', function () {
|
|
505
472
|
const dbs = new DatabaseSchema();
|
|
506
473
|
const S = dbs.getService(RequiredPropertyValidator);
|
|
507
|
-
const blankValues = S.getService(BlankValuesService);
|
|
508
|
-
blankValues.setBlankValues([undefined]);
|
|
509
474
|
dbs.defineModel({
|
|
510
475
|
name: 'model',
|
|
511
476
|
properties: {
|
|
@@ -521,8 +486,6 @@ describe('RequiredPropertyValidator', function () {
|
|
|
521
486
|
it('should validate not provided properties of an embedded model', function () {
|
|
522
487
|
const dbs = new DatabaseSchema();
|
|
523
488
|
const S = dbs.getService(RequiredPropertyValidator);
|
|
524
|
-
const blankValues = S.getService(BlankValuesService);
|
|
525
|
-
blankValues.setBlankValues([undefined]);
|
|
526
489
|
dbs.defineModel({
|
|
527
490
|
name: 'modelA',
|
|
528
491
|
properties: {
|
|
@@ -551,8 +514,6 @@ describe('RequiredPropertyValidator', function () {
|
|
|
551
514
|
it('should validate not provided properties of an item model', function () {
|
|
552
515
|
const dbs = new DatabaseSchema();
|
|
553
516
|
const S = dbs.getService(RequiredPropertyValidator);
|
|
554
|
-
const blankValues = S.getService(BlankValuesService);
|
|
555
|
-
blankValues.setBlankValues([undefined]);
|
|
556
517
|
dbs.defineModel({
|
|
557
518
|
name: 'modelA',
|
|
558
519
|
properties: {
|
|
@@ -13,19 +13,21 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
13
13
|
* @param {object} relDefs
|
|
14
14
|
*/
|
|
15
15
|
validate(modelName, relDefs) {
|
|
16
|
-
if (!modelName || typeof modelName !== 'string')
|
|
16
|
+
if (!modelName || typeof modelName !== 'string') {
|
|
17
17
|
throw new InvalidArgumentError(
|
|
18
18
|
'The first argument of RelationsDefinitionValidator.validate ' +
|
|
19
19
|
'should be a non-empty String, but %v was given.',
|
|
20
20
|
modelName,
|
|
21
21
|
);
|
|
22
|
-
|
|
22
|
+
}
|
|
23
|
+
if (!relDefs || typeof relDefs !== 'object' || Array.isArray(relDefs)) {
|
|
23
24
|
throw new InvalidArgumentError(
|
|
24
25
|
'The provided option "relations" of the model %v ' +
|
|
25
26
|
'should be an Object, but %v was given.',
|
|
26
27
|
modelName,
|
|
27
28
|
relDefs,
|
|
28
29
|
);
|
|
30
|
+
}
|
|
29
31
|
const relNames = Object.keys(relDefs);
|
|
30
32
|
relNames.forEach(relName => {
|
|
31
33
|
const relDef = relDefs[relName];
|
|
@@ -41,27 +43,30 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
41
43
|
* @param {object} relDef
|
|
42
44
|
*/
|
|
43
45
|
_validateRelation(modelName, relName, relDef) {
|
|
44
|
-
if (!modelName || typeof modelName !== 'string')
|
|
46
|
+
if (!modelName || typeof modelName !== 'string') {
|
|
45
47
|
throw new InvalidArgumentError(
|
|
46
48
|
'The first argument of RelationsDefinitionValidator._validateRelation ' +
|
|
47
49
|
'should be a non-empty String, but %v was given.',
|
|
48
50
|
modelName,
|
|
49
51
|
);
|
|
50
|
-
|
|
52
|
+
}
|
|
53
|
+
if (!relName || typeof relName !== 'string') {
|
|
51
54
|
throw new InvalidArgumentError(
|
|
52
55
|
'The relation name of the model %v should be ' +
|
|
53
56
|
'a non-empty String, but %v was given.',
|
|
54
57
|
modelName,
|
|
55
58
|
relName,
|
|
56
59
|
);
|
|
57
|
-
|
|
60
|
+
}
|
|
61
|
+
if (!relDef || typeof relDef !== 'object' || Array.isArray(relDef)) {
|
|
58
62
|
throw new InvalidArgumentError(
|
|
59
63
|
'The relation %v of the model %v should be an Object, but %v was given.',
|
|
60
64
|
relName,
|
|
61
65
|
modelName,
|
|
62
66
|
relDef,
|
|
63
67
|
);
|
|
64
|
-
|
|
68
|
+
}
|
|
69
|
+
if (!relDef.type || !Object.values(RelationType).includes(relDef.type)) {
|
|
65
70
|
throw new InvalidArgumentError(
|
|
66
71
|
'The relation %v of the model %v requires the option "type" ' +
|
|
67
72
|
'to have one of relation types: %l, but %v was given.',
|
|
@@ -70,6 +75,7 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
70
75
|
Object.values(RelationType),
|
|
71
76
|
relDef.type,
|
|
72
77
|
);
|
|
78
|
+
}
|
|
73
79
|
this._validateBelongsTo(modelName, relName, relDef);
|
|
74
80
|
this._validateHasOne(modelName, relName, relDef);
|
|
75
81
|
this._validateHasMany(modelName, relName, relDef);
|
|
@@ -104,10 +110,12 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
104
110
|
* @private
|
|
105
111
|
*/
|
|
106
112
|
_validateBelongsTo(modelName, relName, relDef) {
|
|
107
|
-
if (relDef.type !== RelationType.BELONGS_TO)
|
|
113
|
+
if (relDef.type !== RelationType.BELONGS_TO) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
108
116
|
if (relDef.polymorphic) {
|
|
109
117
|
// A polymorphic "belongsTo" relation.
|
|
110
|
-
if (typeof relDef.polymorphic !== 'boolean')
|
|
118
|
+
if (typeof relDef.polymorphic !== 'boolean') {
|
|
111
119
|
throw new InvalidArgumentError(
|
|
112
120
|
'The relation %v of the model %v has the type "belongsTo", ' +
|
|
113
121
|
'so it expects the option "polymorphic" to be a Boolean, ' +
|
|
@@ -116,7 +124,8 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
116
124
|
modelName,
|
|
117
125
|
relDef.polymorphic,
|
|
118
126
|
);
|
|
119
|
-
|
|
127
|
+
}
|
|
128
|
+
if (relDef.foreignKey && typeof relDef.foreignKey !== 'string') {
|
|
120
129
|
throw new InvalidArgumentError(
|
|
121
130
|
'The relation %v of the model %v is a polymorphic "belongsTo" relation, ' +
|
|
122
131
|
'so it expects the provided option "foreignKey" to be a String, ' +
|
|
@@ -125,7 +134,8 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
125
134
|
modelName,
|
|
126
135
|
relDef.foreignKey,
|
|
127
136
|
);
|
|
128
|
-
|
|
137
|
+
}
|
|
138
|
+
if (relDef.discriminator && typeof relDef.discriminator !== 'string') {
|
|
129
139
|
throw new InvalidArgumentError(
|
|
130
140
|
'The relation %v of the model %v is a polymorphic "belongsTo" relation, ' +
|
|
131
141
|
'so it expects the provided option "discriminator" to be a String, ' +
|
|
@@ -134,9 +144,10 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
134
144
|
modelName,
|
|
135
145
|
relDef.discriminator,
|
|
136
146
|
);
|
|
147
|
+
}
|
|
137
148
|
} else {
|
|
138
149
|
// A regular "belongsTo" relation.
|
|
139
|
-
if (!relDef.model || typeof relDef.model !== 'string')
|
|
150
|
+
if (!relDef.model || typeof relDef.model !== 'string') {
|
|
140
151
|
throw new InvalidArgumentError(
|
|
141
152
|
'The relation %v of the model %v has the type "belongsTo", ' +
|
|
142
153
|
'so it requires the option "model" to be a non-empty String, ' +
|
|
@@ -145,7 +156,8 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
145
156
|
modelName,
|
|
146
157
|
relDef.model,
|
|
147
158
|
);
|
|
148
|
-
|
|
159
|
+
}
|
|
160
|
+
if (relDef.foreignKey && typeof relDef.foreignKey !== 'string') {
|
|
149
161
|
throw new InvalidArgumentError(
|
|
150
162
|
'The relation %v of the model %v has the type "belongsTo", ' +
|
|
151
163
|
'so it expects the provided option "foreignKey" to be a String, ' +
|
|
@@ -154,13 +166,15 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
154
166
|
modelName,
|
|
155
167
|
relDef.foreignKey,
|
|
156
168
|
);
|
|
157
|
-
|
|
169
|
+
}
|
|
170
|
+
if (relDef.discriminator) {
|
|
158
171
|
throw new InvalidArgumentError(
|
|
159
172
|
'The relation %v of the model %v is a non-polymorphic "belongsTo" relation, ' +
|
|
160
173
|
'so it should not have the option "discriminator" to be provided.',
|
|
161
174
|
relName,
|
|
162
175
|
modelName,
|
|
163
176
|
);
|
|
177
|
+
}
|
|
164
178
|
}
|
|
165
179
|
}
|
|
166
180
|
|
|
@@ -202,8 +216,10 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
202
216
|
* @private
|
|
203
217
|
*/
|
|
204
218
|
_validateHasOne(modelName, relName, relDef) {
|
|
205
|
-
if (relDef.type !== RelationType.HAS_ONE)
|
|
206
|
-
|
|
219
|
+
if (relDef.type !== RelationType.HAS_ONE) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
if (!relDef.model || typeof relDef.model !== 'string') {
|
|
207
223
|
throw new InvalidArgumentError(
|
|
208
224
|
'The relation %v of the model %v has the type "hasOne", ' +
|
|
209
225
|
'so it requires the option "model" to be a non-empty String, ' +
|
|
@@ -212,10 +228,11 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
212
228
|
modelName,
|
|
213
229
|
relDef.model,
|
|
214
230
|
);
|
|
231
|
+
}
|
|
215
232
|
if (relDef.polymorphic) {
|
|
216
233
|
if (typeof relDef.polymorphic === 'string') {
|
|
217
234
|
// A polymorphic "hasOne" relation with a target relation name.
|
|
218
|
-
if (relDef.foreignKey)
|
|
235
|
+
if (relDef.foreignKey) {
|
|
219
236
|
throw new InvalidArgumentError(
|
|
220
237
|
'The relation %v of the model %v has the option "polymorphic" with ' +
|
|
221
238
|
'a String value, so it should not have the option "foreignKey" ' +
|
|
@@ -223,7 +240,8 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
223
240
|
relName,
|
|
224
241
|
modelName,
|
|
225
242
|
);
|
|
226
|
-
|
|
243
|
+
}
|
|
244
|
+
if (relDef.discriminator) {
|
|
227
245
|
throw new InvalidArgumentError(
|
|
228
246
|
'The relation %v of the model %v has the option "polymorphic" with ' +
|
|
229
247
|
'a String value, so it should not have the option "discriminator" ' +
|
|
@@ -231,9 +249,10 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
231
249
|
relName,
|
|
232
250
|
modelName,
|
|
233
251
|
);
|
|
252
|
+
}
|
|
234
253
|
} else if (typeof relDef.polymorphic === 'boolean') {
|
|
235
254
|
// A polymorphic "hasOne" relation with target relation keys.
|
|
236
|
-
if (!relDef.foreignKey || typeof relDef.foreignKey !== 'string')
|
|
255
|
+
if (!relDef.foreignKey || typeof relDef.foreignKey !== 'string') {
|
|
237
256
|
throw new InvalidArgumentError(
|
|
238
257
|
'The relation %v of the model %v has the option "polymorphic" ' +
|
|
239
258
|
'with "true" value, so it requires the option "foreignKey" ' +
|
|
@@ -242,7 +261,8 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
242
261
|
modelName,
|
|
243
262
|
relDef.foreignKey,
|
|
244
263
|
);
|
|
245
|
-
|
|
264
|
+
}
|
|
265
|
+
if (!relDef.discriminator || typeof relDef.discriminator !== 'string') {
|
|
246
266
|
throw new InvalidArgumentError(
|
|
247
267
|
'The relation %v of the model %v has the option "polymorphic" ' +
|
|
248
268
|
'with "true" value, so it requires the option "discriminator" ' +
|
|
@@ -251,6 +271,7 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
251
271
|
modelName,
|
|
252
272
|
relDef.discriminator,
|
|
253
273
|
);
|
|
274
|
+
}
|
|
254
275
|
} else {
|
|
255
276
|
throw new InvalidArgumentError(
|
|
256
277
|
'The relation %v of the model %v has the type "hasOne", ' +
|
|
@@ -263,7 +284,7 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
263
284
|
}
|
|
264
285
|
} else {
|
|
265
286
|
// A regular "hasOne" relation.
|
|
266
|
-
if (!relDef.foreignKey || typeof relDef.foreignKey !== 'string')
|
|
287
|
+
if (!relDef.foreignKey || typeof relDef.foreignKey !== 'string') {
|
|
267
288
|
throw new InvalidArgumentError(
|
|
268
289
|
'The relation %v of the model %v has the type "hasOne", ' +
|
|
269
290
|
'so it requires the option "foreignKey" to be a non-empty String, ' +
|
|
@@ -272,13 +293,15 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
272
293
|
modelName,
|
|
273
294
|
relDef.foreignKey,
|
|
274
295
|
);
|
|
275
|
-
|
|
296
|
+
}
|
|
297
|
+
if (relDef.discriminator) {
|
|
276
298
|
throw new InvalidArgumentError(
|
|
277
299
|
'The relation %v of the model %v is a non-polymorphic "hasOne" relation, ' +
|
|
278
300
|
'so it should not have the option "discriminator" to be provided.',
|
|
279
301
|
relName,
|
|
280
302
|
modelName,
|
|
281
303
|
);
|
|
304
|
+
}
|
|
282
305
|
}
|
|
283
306
|
}
|
|
284
307
|
|
|
@@ -320,8 +343,10 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
320
343
|
* @private
|
|
321
344
|
*/
|
|
322
345
|
_validateHasMany(modelName, relName, relDef) {
|
|
323
|
-
if (relDef.type !== RelationType.HAS_MANY)
|
|
324
|
-
|
|
346
|
+
if (relDef.type !== RelationType.HAS_MANY) {
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
if (!relDef.model || typeof relDef.model !== 'string') {
|
|
325
350
|
throw new InvalidArgumentError(
|
|
326
351
|
'The relation %v of the model %v has the type "hasMany", ' +
|
|
327
352
|
'so it requires the option "model" to be a non-empty String, ' +
|
|
@@ -330,10 +355,11 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
330
355
|
modelName,
|
|
331
356
|
relDef.model,
|
|
332
357
|
);
|
|
358
|
+
}
|
|
333
359
|
if (relDef.polymorphic) {
|
|
334
360
|
if (typeof relDef.polymorphic === 'string') {
|
|
335
361
|
// A polymorphic "hasMany" relation with a target relation name.
|
|
336
|
-
if (relDef.foreignKey)
|
|
362
|
+
if (relDef.foreignKey) {
|
|
337
363
|
throw new InvalidArgumentError(
|
|
338
364
|
'The relation %v of the model %v has the option "polymorphic" with ' +
|
|
339
365
|
'a String value, so it should not have the option "foreignKey" ' +
|
|
@@ -341,7 +367,8 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
341
367
|
relName,
|
|
342
368
|
modelName,
|
|
343
369
|
);
|
|
344
|
-
|
|
370
|
+
}
|
|
371
|
+
if (relDef.discriminator) {
|
|
345
372
|
throw new InvalidArgumentError(
|
|
346
373
|
'The relation %v of the model %v has the option "polymorphic" with ' +
|
|
347
374
|
'a String value, so it should not have the option "discriminator" ' +
|
|
@@ -349,9 +376,10 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
349
376
|
relName,
|
|
350
377
|
modelName,
|
|
351
378
|
);
|
|
379
|
+
}
|
|
352
380
|
} else if (typeof relDef.polymorphic === 'boolean') {
|
|
353
381
|
// A polymorphic "hasMany" relation with target relation keys.
|
|
354
|
-
if (!relDef.foreignKey || typeof relDef.foreignKey !== 'string')
|
|
382
|
+
if (!relDef.foreignKey || typeof relDef.foreignKey !== 'string') {
|
|
355
383
|
throw new InvalidArgumentError(
|
|
356
384
|
'The relation %v of the model %v has the option "polymorphic" ' +
|
|
357
385
|
'with "true" value, so it requires the option "foreignKey" ' +
|
|
@@ -360,7 +388,8 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
360
388
|
modelName,
|
|
361
389
|
relDef.foreignKey,
|
|
362
390
|
);
|
|
363
|
-
|
|
391
|
+
}
|
|
392
|
+
if (!relDef.discriminator || typeof relDef.discriminator !== 'string') {
|
|
364
393
|
throw new InvalidArgumentError(
|
|
365
394
|
'The relation %v of the model %v has the option "polymorphic" ' +
|
|
366
395
|
'with "true" value, so it requires the option "discriminator" ' +
|
|
@@ -369,6 +398,7 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
369
398
|
modelName,
|
|
370
399
|
relDef.discriminator,
|
|
371
400
|
);
|
|
401
|
+
}
|
|
372
402
|
} else {
|
|
373
403
|
throw new InvalidArgumentError(
|
|
374
404
|
'The relation %v of the model %v has the type "hasMany", ' +
|
|
@@ -381,7 +411,7 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
381
411
|
}
|
|
382
412
|
} else {
|
|
383
413
|
// A regular "hasMany" relation.
|
|
384
|
-
if (!relDef.foreignKey || typeof relDef.foreignKey !== 'string')
|
|
414
|
+
if (!relDef.foreignKey || typeof relDef.foreignKey !== 'string') {
|
|
385
415
|
throw new InvalidArgumentError(
|
|
386
416
|
'The relation %v of the model %v has the type "hasMany", ' +
|
|
387
417
|
'so it requires the option "foreignKey" to be a non-empty String, ' +
|
|
@@ -390,13 +420,15 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
390
420
|
modelName,
|
|
391
421
|
relDef.foreignKey,
|
|
392
422
|
);
|
|
393
|
-
|
|
423
|
+
}
|
|
424
|
+
if (relDef.discriminator) {
|
|
394
425
|
throw new InvalidArgumentError(
|
|
395
426
|
'The relation %v of the model %v is a non-polymorphic "hasMany" relation, ' +
|
|
396
427
|
'so it should not have the option "discriminator" to be provided.',
|
|
397
428
|
relName,
|
|
398
429
|
modelName,
|
|
399
430
|
);
|
|
431
|
+
}
|
|
400
432
|
}
|
|
401
433
|
}
|
|
402
434
|
|
|
@@ -418,8 +450,10 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
418
450
|
* @private
|
|
419
451
|
*/
|
|
420
452
|
_validateReferencesMany(modelName, relName, relDef) {
|
|
421
|
-
if (relDef.type !== RelationType.REFERENCES_MANY)
|
|
422
|
-
|
|
453
|
+
if (relDef.type !== RelationType.REFERENCES_MANY) {
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
if (!relDef.model || typeof relDef.model !== 'string') {
|
|
423
457
|
throw new InvalidArgumentError(
|
|
424
458
|
'The relation %v of the model %v has the type "referencesMany", ' +
|
|
425
459
|
'so it requires the option "model" to be a non-empty String, ' +
|
|
@@ -428,7 +462,8 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
428
462
|
modelName,
|
|
429
463
|
relDef.model,
|
|
430
464
|
);
|
|
431
|
-
|
|
465
|
+
}
|
|
466
|
+
if (relDef.foreignKey && typeof relDef.foreignKey !== 'string') {
|
|
432
467
|
throw new InvalidArgumentError(
|
|
433
468
|
'The relation %v of the model %v has the type "referencesMany", ' +
|
|
434
469
|
'so it expects the provided option "foreignKey" to be a String, ' +
|
|
@@ -437,12 +472,14 @@ export class RelationsDefinitionValidator extends Service {
|
|
|
437
472
|
modelName,
|
|
438
473
|
relDef.foreignKey,
|
|
439
474
|
);
|
|
440
|
-
|
|
475
|
+
}
|
|
476
|
+
if (relDef.discriminator) {
|
|
441
477
|
throw new InvalidArgumentError(
|
|
442
478
|
'The relation %v of the model %v has the type "referencesMany", ' +
|
|
443
479
|
'so it should not have the option "discriminator" to be provided.',
|
|
444
480
|
relName,
|
|
445
481
|
modelName,
|
|
446
482
|
);
|
|
483
|
+
}
|
|
447
484
|
}
|
|
448
485
|
}
|