monastery 1.28.2 → 1.29.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/.eslintrc.json +30 -4
- package/docs/Gemfile +8 -18
- package/docs/_config.yml +1 -4
- package/docs/errors.md +0 -0
- package/docs/image-plugin.md +0 -0
- package/docs/manager/model.md +0 -0
- package/docs/manager/models.md +0 -0
- package/docs/model/findOne.md +0 -0
- package/docs/model/index.md +0 -0
- package/docs/model/remove.md +0 -0
- package/docs/readme.md +1 -1
- package/docs/rules.md +0 -0
- package/lib/index.js +0 -1
- package/lib/model-crud.js +32 -16
- package/lib/model-validate.js +20 -24
- package/lib/model.js +13 -4
- package/lib/rules.js +38 -16
- package/lib/util.js +13 -7
- package/package.json +17 -16
- package/plugins/images/index.js +4 -4
- package/test/assets/bad.svg +0 -0
- package/test/assets/image.ico +0 -0
- package/test/assets/image.webp +0 -0
- package/test/assets/lion1.png +0 -0
- package/test/assets/lion2.jpg +0 -0
- package/test/assets/logo.png +0 -0
- package/test/assets/logo2.png +0 -0
- package/test/blacklisting.js +4 -8
- package/test/crud.js +28 -31
- package/test/model.js +22 -25
- package/test/monk.js +4 -4
- package/test/plugin-images.js +400 -390
- package/test/populate.js +15 -18
- package/test/util.js +8 -8
- package/test/validate.js +126 -50
- package/test/virtuals.js +2 -4
package/test/populate.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
module.exports = function(monastery, opendb) {
|
|
2
2
|
|
|
3
|
-
test('Model populate', async (
|
|
3
|
+
test('Model populate', async () => {
|
|
4
4
|
// Setup
|
|
5
5
|
let db = (await opendb(null)).db
|
|
6
6
|
let bird = db.model('bird', { fields: {
|
|
@@ -30,7 +30,7 @@ module.exports = function(monastery, opendb) {
|
|
|
30
30
|
name: 'Martin Luther',
|
|
31
31
|
myBird: {
|
|
32
32
|
_id: bird1._id,
|
|
33
|
-
name:
|
|
33
|
+
name: 'ponyo'
|
|
34
34
|
},
|
|
35
35
|
pets: {
|
|
36
36
|
myBird: bird1._id
|
|
@@ -46,7 +46,7 @@ module.exports = function(monastery, opendb) {
|
|
|
46
46
|
pets: {
|
|
47
47
|
myBird: {
|
|
48
48
|
_id: bird1._id,
|
|
49
|
-
name:
|
|
49
|
+
name: 'ponyo'
|
|
50
50
|
},
|
|
51
51
|
}
|
|
52
52
|
})
|
|
@@ -62,7 +62,7 @@ module.exports = function(monastery, opendb) {
|
|
|
62
62
|
name: 'Martin Luther',
|
|
63
63
|
myBird: {
|
|
64
64
|
_id: bird1._id,
|
|
65
|
-
name:
|
|
65
|
+
name: 'ponyo'
|
|
66
66
|
},
|
|
67
67
|
pets: {
|
|
68
68
|
myBird: bird1._id
|
|
@@ -72,7 +72,7 @@ module.exports = function(monastery, opendb) {
|
|
|
72
72
|
name: 'Martin Luther2',
|
|
73
73
|
myBird: {
|
|
74
74
|
_id: bird1._id,
|
|
75
|
-
name:
|
|
75
|
+
name: 'ponyo'
|
|
76
76
|
},
|
|
77
77
|
pets: {
|
|
78
78
|
myBird: bird1._id
|
|
@@ -80,10 +80,9 @@ module.exports = function(monastery, opendb) {
|
|
|
80
80
|
}])
|
|
81
81
|
|
|
82
82
|
db.close()
|
|
83
|
-
done()
|
|
84
83
|
})
|
|
85
84
|
|
|
86
|
-
test('Model populate type=any', async (
|
|
85
|
+
test('Model populate type=any', async () => {
|
|
87
86
|
let db = (await opendb(null)).db
|
|
88
87
|
db.model('company', { fields: {
|
|
89
88
|
address: { type: 'any' }
|
|
@@ -131,10 +130,9 @@ module.exports = function(monastery, opendb) {
|
|
|
131
130
|
})
|
|
132
131
|
|
|
133
132
|
db.close()
|
|
134
|
-
done()
|
|
135
133
|
})
|
|
136
134
|
|
|
137
|
-
test('Model populate/blacklisting via $lookup', async (
|
|
135
|
+
test('Model populate/blacklisting via $lookup', async () => {
|
|
138
136
|
// Setup
|
|
139
137
|
let db = (await opendb(null)).db
|
|
140
138
|
let user = db.model('user', {
|
|
@@ -167,10 +165,10 @@ module.exports = function(monastery, opendb) {
|
|
|
167
165
|
let find1 = await user.findOne({
|
|
168
166
|
query: user1._id,
|
|
169
167
|
populate: [{
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
168
|
+
'as': 'birds',
|
|
169
|
+
'from': 'bird',
|
|
170
|
+
'let': { id: '$_id' },
|
|
171
|
+
'pipeline': [
|
|
174
172
|
{ $match: { $expr: { $eq: ['$owner', '$$id'] }}}
|
|
175
173
|
]
|
|
176
174
|
}]
|
|
@@ -193,10 +191,10 @@ module.exports = function(monastery, opendb) {
|
|
|
193
191
|
let find2 = await user.findOne({
|
|
194
192
|
query: user1._id,
|
|
195
193
|
populate: [{
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
194
|
+
'as': 'anyModel',
|
|
195
|
+
'from': 'bird',
|
|
196
|
+
'let': { id: '$_id' },
|
|
197
|
+
'pipeline': [
|
|
200
198
|
{ $match: { $expr: { $eq: ['$owner', '$$id'] }}}
|
|
201
199
|
]
|
|
202
200
|
}]
|
|
@@ -217,7 +215,6 @@ module.exports = function(monastery, opendb) {
|
|
|
217
215
|
})
|
|
218
216
|
|
|
219
217
|
db.close()
|
|
220
|
-
done()
|
|
221
218
|
})
|
|
222
219
|
|
|
223
220
|
}
|
package/test/util.js
CHANGED
|
@@ -10,10 +10,10 @@ module.exports = function(monastery, opendb) {
|
|
|
10
10
|
'deep[companyLogos][0]': 'b',
|
|
11
11
|
'deep[companyLogos2][0][logo]':'c',
|
|
12
12
|
'deep[companyLogos2][1][logo]': '',
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
'users[0][first]': 'Martin',
|
|
14
|
+
'users[0][last]': 'Luther',
|
|
15
|
+
'users[1][first]': 'Bruce',
|
|
16
|
+
'users[1][last]': 'Lee',
|
|
17
17
|
})).toEqual({
|
|
18
18
|
name: 'Martin',
|
|
19
19
|
pets: expect.any(Array),
|
|
@@ -23,12 +23,12 @@ module.exports = function(monastery, opendb) {
|
|
|
23
23
|
companyLogos2: [{ logo: 'c' }, { logo: '' }]
|
|
24
24
|
},
|
|
25
25
|
users: [
|
|
26
|
-
{
|
|
27
|
-
{
|
|
26
|
+
{ 'first': 'Martin', 'last': 'Luther' },
|
|
27
|
+
{ 'first': 'Bruce', 'last': 'Lee' },
|
|
28
28
|
]
|
|
29
29
|
})
|
|
30
|
-
expect(util.parseFormData({
|
|
31
|
-
.toEqual(
|
|
30
|
+
expect(util.parseFormData({ 'users[][\'name\']': 'Martin' })).rejects
|
|
31
|
+
.toEqual('Array items in bracket notation need array indexes "users[][\'name\']", e.g. users[0][name]')
|
|
32
32
|
})
|
|
33
33
|
|
|
34
34
|
test('Utilities: isId', async () => {
|
package/test/validate.js
CHANGED
|
@@ -157,7 +157,8 @@ module.exports = function(monastery, opendb) {
|
|
|
157
157
|
})
|
|
158
158
|
|
|
159
159
|
// Required subdocument property (defined with ignoreUndefined)
|
|
160
|
-
await expect(user.validate({ animals: { cat: '' }}, { update: true, ignoreUndefined: true }))
|
|
160
|
+
await expect(user.validate({ animals: { cat: '' }}, { update: true, ignoreUndefined: true }))
|
|
161
|
+
.rejects.toContainEqual({
|
|
161
162
|
status: '400',
|
|
162
163
|
title: 'animals.cat',
|
|
163
164
|
detail: 'This field is required.',
|
|
@@ -233,7 +234,6 @@ module.exports = function(monastery, opendb) {
|
|
|
233
234
|
let fn = validate._getMostSpecificKeyMatchingPath
|
|
234
235
|
let mock = {
|
|
235
236
|
'cats.name': true,
|
|
236
|
-
'cats.name': true,
|
|
237
237
|
|
|
238
238
|
'dogs.name': true,
|
|
239
239
|
'dogs.$.name': true,
|
|
@@ -318,7 +318,7 @@ module.exports = function(monastery, opendb) {
|
|
|
318
318
|
// Setup
|
|
319
319
|
// Todo: Setup testing for array array subdocument field messages
|
|
320
320
|
let db = (await opendb(false)).db
|
|
321
|
-
let arrayWithSchema = (array, schema) => { array.schema = schema; return array }
|
|
321
|
+
// let arrayWithSchema = (array, schema) => { array.schema = schema; return array }
|
|
322
322
|
let user = db.model('user', {
|
|
323
323
|
fields: {
|
|
324
324
|
name: { type: 'string', minLength: 4 },
|
|
@@ -415,14 +415,16 @@ module.exports = function(monastery, opendb) {
|
|
|
415
415
|
meta: { rule: 'minLength', model: 'user', field: 'name' }
|
|
416
416
|
})
|
|
417
417
|
// array-subdocument-1-field error
|
|
418
|
-
await expect(user.validate({ catNames: [{ name: 'carla' }, { name: 'ben' }] }))
|
|
418
|
+
await expect(user.validate({ catNames: [{ name: 'carla' }, { name: 'ben' }] }))
|
|
419
|
+
.rejects.toContainEqual({
|
|
419
420
|
status: '400',
|
|
420
421
|
title: 'catNames.1.name',
|
|
421
422
|
detail: 'min length error (1)',
|
|
422
423
|
meta: { rule: 'minLength', model: 'user', field: 'name' }
|
|
423
424
|
})
|
|
424
425
|
// array-subdocument-2-field error
|
|
425
|
-
await expect(user.validate({ catNames: [{ name: 'carla' }, { name: 'carla' }, { name: 'ben' }] }))
|
|
426
|
+
await expect(user.validate({ catNames: [{ name: 'carla' }, { name: 'carla' }, { name: 'ben' }] }))
|
|
427
|
+
.rejects.toContainEqual({
|
|
426
428
|
status: '400',
|
|
427
429
|
title: 'catNames.2.name',
|
|
428
430
|
detail: 'min length error (2)',
|
|
@@ -431,28 +433,32 @@ module.exports = function(monastery, opendb) {
|
|
|
431
433
|
|
|
432
434
|
|
|
433
435
|
// array-subdocument-field error (loose $ match)
|
|
434
|
-
await expect(user.validate({ pigNames: [[{ name: 'ben' }]] }))
|
|
436
|
+
await expect(user.validate({ pigNames: [[{ name: 'ben' }]] }))
|
|
437
|
+
.rejects.toContainEqual({
|
|
435
438
|
status: '400',
|
|
436
439
|
title: 'pigNames.0.0.name',
|
|
437
440
|
detail: 'min length error ($)',
|
|
438
441
|
meta: { rule: 'minLength', model: 'user', field: 'name' }
|
|
439
442
|
})
|
|
440
443
|
// array-subdocument-1-field error
|
|
441
|
-
await expect(user.validate({ pigNames: [[{ name: 'carla' }, { name: 'ben' }]] }))
|
|
444
|
+
await expect(user.validate({ pigNames: [[{ name: 'carla' }, { name: 'ben' }]] }))
|
|
445
|
+
.rejects.toContainEqual({
|
|
442
446
|
status: '400',
|
|
443
447
|
title: 'pigNames.0.1.name',
|
|
444
448
|
detail: 'min length error (1)',
|
|
445
449
|
meta: { rule: 'minLength', model: 'user', field: 'name' }
|
|
446
450
|
})
|
|
447
451
|
// array-subdocument-0-2-field error
|
|
448
|
-
await expect(user.validate({ pigNames: [[{ name: 'carla' }, { name: 'carla' }, { name: 'ben' }]] }))
|
|
452
|
+
await expect(user.validate({ pigNames: [[{ name: 'carla' }, { name: 'carla' }, { name: 'ben' }]] }))
|
|
453
|
+
.rejects.toContainEqual({
|
|
449
454
|
status: '400',
|
|
450
455
|
title: 'pigNames.0.2.name',
|
|
451
456
|
detail: 'min length error (deep 0 2)',
|
|
452
457
|
meta: { rule: 'minLength', model: 'user', field: 'name' }
|
|
453
458
|
})
|
|
454
459
|
// array-subdocument-2-0-field error (fallback)
|
|
455
|
-
await expect(user.validate({ pigNames: [[],[],[{ name: 'carla' },{ name: 'carla' },{ name: 'ben' }]] }))
|
|
460
|
+
await expect(user.validate({ pigNames: [[],[],[{ name: 'carla' },{ name: 'carla' },{ name: 'ben' }]] }))
|
|
461
|
+
.rejects.toContainEqual({
|
|
456
462
|
status: '400',
|
|
457
463
|
title: 'pigNames.2.2.name',
|
|
458
464
|
detail: 'min length error (deep $ 2)',
|
|
@@ -475,7 +481,7 @@ module.exports = function(monastery, opendb) {
|
|
|
475
481
|
name: { type: 'string', bigName: 8 },
|
|
476
482
|
animals: [{
|
|
477
483
|
name: { type: 'string', bigName: 8 }
|
|
478
|
-
}]
|
|
484
|
+
}],
|
|
479
485
|
},
|
|
480
486
|
rules: {
|
|
481
487
|
bigName: function(value, ruleArg) {
|
|
@@ -483,6 +489,21 @@ module.exports = function(monastery, opendb) {
|
|
|
483
489
|
}
|
|
484
490
|
}
|
|
485
491
|
})
|
|
492
|
+
let user2 = db.model('user2', {
|
|
493
|
+
fields: {
|
|
494
|
+
name: { type: 'string' },
|
|
495
|
+
nickname: { type: 'string', requiredIfNoName: true },
|
|
496
|
+
age: { type: 'number', required: true },
|
|
497
|
+
},
|
|
498
|
+
rules: {
|
|
499
|
+
requiredIfNoName: {
|
|
500
|
+
ignoreUndefined: false,
|
|
501
|
+
fn: function(value, ruleArg) {
|
|
502
|
+
return value || this.name
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
})
|
|
486
507
|
|
|
487
508
|
// Basic field
|
|
488
509
|
await expect(user.validate({ name: 'benjamin' })).resolves.toEqual({ name: 'benjamin' })
|
|
@@ -494,13 +515,48 @@ module.exports = function(monastery, opendb) {
|
|
|
494
515
|
})
|
|
495
516
|
|
|
496
517
|
// subdocument in an array
|
|
497
|
-
await expect(user.validate({ animals: [{ name: 'benjamin' }] })).resolves.toEqual({
|
|
518
|
+
await expect(user.validate({ animals: [{ name: 'benjamin' }] })).resolves.toEqual({
|
|
519
|
+
animals: [{ name: 'benjamin' }]
|
|
520
|
+
})
|
|
498
521
|
await expect(user.validate({ animals: [{ name: 'ben' }] })).rejects.toContainEqual({
|
|
499
522
|
status: '400',
|
|
500
523
|
title: 'animals.0.name',
|
|
501
524
|
detail: 'Invalid data property for rule "bigName".',
|
|
502
525
|
meta: { rule: 'bigName', model: 'user', field: 'name' }
|
|
503
526
|
})
|
|
527
|
+
|
|
528
|
+
// Required rule based off another field (create)
|
|
529
|
+
await expect(user2.validate({ name: 'benjamin', age: 12 })).resolves.toEqual({
|
|
530
|
+
name: 'benjamin',
|
|
531
|
+
age: 12
|
|
532
|
+
})
|
|
533
|
+
await expect(user2.validate({ nickname: 'benny', age: 12 })).resolves.toEqual({
|
|
534
|
+
nickname: 'benny',
|
|
535
|
+
age: 12
|
|
536
|
+
})
|
|
537
|
+
await expect(user2.validate({ })).rejects.toEqual([
|
|
538
|
+
{
|
|
539
|
+
'detail': 'Invalid data property for rule "requiredIfNoName".',
|
|
540
|
+
'meta': { 'field': 'nickname', 'model': 'user2', 'rule': 'requiredIfNoName'},
|
|
541
|
+
'status': '400',
|
|
542
|
+
'title': 'nickname'
|
|
543
|
+
}, {
|
|
544
|
+
'detail': 'This field is required.',
|
|
545
|
+
'meta': { 'field': 'age', 'model': 'user2', 'rule': 'required'},
|
|
546
|
+
'status': '400',
|
|
547
|
+
'title': 'age'
|
|
548
|
+
}
|
|
549
|
+
])
|
|
550
|
+
await expect(user2.validate({ }, { ignoreUndefined: true })).resolves.toEqual({})
|
|
551
|
+
|
|
552
|
+
// Required rule based off another field (update)
|
|
553
|
+
await expect(user2.validate({ }, { update: true })).resolves.toEqual({})
|
|
554
|
+
await expect(user2.validate({ nickname: '' }, { update: true })).rejects.toEqual([{
|
|
555
|
+
'detail': 'Invalid data property for rule "requiredIfNoName".',
|
|
556
|
+
'meta': { 'field': 'nickname', 'model': 'user2', 'rule': 'requiredIfNoName'},
|
|
557
|
+
'status': '400',
|
|
558
|
+
'title': 'nickname'
|
|
559
|
+
}])
|
|
504
560
|
})
|
|
505
561
|
|
|
506
562
|
test('Validated data', async () => {
|
|
@@ -524,8 +580,13 @@ module.exports = function(monastery, opendb) {
|
|
|
524
580
|
// Ignores invalid data
|
|
525
581
|
await expect(user.validate({ badprop: true, schema: {} })).resolves.toEqual({})
|
|
526
582
|
|
|
527
|
-
//
|
|
528
|
-
await expect(user.validate({ name: null })).
|
|
583
|
+
// Rejects null for non object/array fields
|
|
584
|
+
await expect(user.validate({ name: null })).rejects.toEqual([{
|
|
585
|
+
'detail': 'Value was not a string.',
|
|
586
|
+
'meta': {'detailLong': undefined, 'field': 'name', 'model': 'user', 'rule': 'isString'},
|
|
587
|
+
'status':'400',
|
|
588
|
+
'title': 'name'
|
|
589
|
+
}])
|
|
529
590
|
|
|
530
591
|
// String data
|
|
531
592
|
await expect(user.validate({ name: 'Martin Luther' })).resolves.toEqual({ name: 'Martin Luther' })
|
|
@@ -548,9 +609,14 @@ module.exports = function(monastery, opendb) {
|
|
|
548
609
|
|
|
549
610
|
// Subdocument property data (null)
|
|
550
611
|
await expect(user.validate({ animals: { dog: null }}))
|
|
551
|
-
.
|
|
552
|
-
|
|
553
|
-
|
|
612
|
+
.rejects.toEqual([{
|
|
613
|
+
'detail': 'Value was not a string.',
|
|
614
|
+
'meta': {'detailLong': undefined, 'field': 'dog', 'model': 'user', 'rule': 'isString'},
|
|
615
|
+
'status': '400',
|
|
616
|
+
'title': 'animals.dog',
|
|
617
|
+
}])
|
|
618
|
+
|
|
619
|
+
// Subdocument property data (unknown data)
|
|
554
620
|
await expect(user.validate({ animals: { dog: 'sparky', cat: 'grumpy' } }))
|
|
555
621
|
.resolves.toEqual({ animals: { dog: 'sparky' } })
|
|
556
622
|
|
|
@@ -588,11 +654,11 @@ module.exports = function(monastery, opendb) {
|
|
|
588
654
|
|
|
589
655
|
// Index, mongodb connection error
|
|
590
656
|
await expect(user3._setupIndexes({ name: { type: 'string', index: 'text' }})).rejects
|
|
591
|
-
.toEqual({ type:
|
|
657
|
+
.toEqual({ type: 'info', detail: 'Skipping createIndex on the \'user3\' model, no mongodb connection found.' })
|
|
592
658
|
|
|
593
659
|
// Model id (Monk ObjectId)
|
|
594
|
-
let data = await user4.validate({ name:
|
|
595
|
-
await expect(data.name.toString()).toEqual(db.id(
|
|
660
|
+
let data = await user4.validate({ name: '5d4356299d0f010017602f6b' })
|
|
661
|
+
await expect(data.name.toString()).toEqual(db.id('5d4356299d0f010017602f6b').toString())
|
|
596
662
|
await expect(data.name).toEqual(expect.any(Object))
|
|
597
663
|
|
|
598
664
|
// Bad model id (Monk ObjectId)
|
|
@@ -617,7 +683,6 @@ module.exports = function(monastery, opendb) {
|
|
|
617
683
|
amount: { type: 'number', required: true },
|
|
618
684
|
}})
|
|
619
685
|
|
|
620
|
-
|
|
621
686
|
// MinLength
|
|
622
687
|
await expect(user.validate({ name: 'Martin Luther' })).resolves.toEqual({name: 'Martin Luther'})
|
|
623
688
|
await expect(user.validate({ name: 'Carl' })).rejects.toContainEqual({
|
|
@@ -661,7 +726,12 @@ module.exports = function(monastery, opendb) {
|
|
|
661
726
|
await expect(user2.validate({ amount: 0 })).resolves.toEqual({ amount: 0 }) // required
|
|
662
727
|
await expect(user.validate({ amount: '0' })).resolves.toEqual({ amount: 0 }) // required
|
|
663
728
|
await expect(user.validate({ amount: undefined })).resolves.toEqual({}) // not required
|
|
664
|
-
await expect(user.validate({ amount: null })).
|
|
729
|
+
await expect(user.validate({ amount: null })).rejects.toEqual([{ // type error
|
|
730
|
+
'detail': 'Value was not a number.',
|
|
731
|
+
'meta': { 'field': 'amount', 'model': 'user', 'rule': 'isNumber'},
|
|
732
|
+
'status': '400',
|
|
733
|
+
'title': 'amount'
|
|
734
|
+
}])
|
|
665
735
|
|
|
666
736
|
// Number required
|
|
667
737
|
let mock1 = {
|
|
@@ -686,14 +756,14 @@ module.exports = function(monastery, opendb) {
|
|
|
686
756
|
await expect(user.validate({ amount: 'bad' })).rejects.toContainEqual(mock2)
|
|
687
757
|
})
|
|
688
758
|
|
|
689
|
-
test('Schema default objects', async (
|
|
759
|
+
test('Schema default objects', async () => {
|
|
690
760
|
let db = (await opendb(null, {
|
|
691
761
|
timestamps: false,
|
|
692
762
|
defaultObjects: true,
|
|
693
763
|
serverSelectionTimeoutMS: 2000
|
|
694
764
|
})).db
|
|
695
765
|
|
|
696
|
-
let base = { names: [], animals: { dogs: [] }}
|
|
766
|
+
// let base = { names: [], animals: { dogs: [] }}
|
|
697
767
|
let user = db.model('user', { fields: {
|
|
698
768
|
name: { type: 'string' },
|
|
699
769
|
names: [{ type: 'string' }],
|
|
@@ -710,10 +780,9 @@ module.exports = function(monastery, opendb) {
|
|
|
710
780
|
})
|
|
711
781
|
|
|
712
782
|
db.close()
|
|
713
|
-
done()
|
|
714
783
|
})
|
|
715
784
|
|
|
716
|
-
test('Schema nullObjects', async (
|
|
785
|
+
test('Schema nullObjects', async () => {
|
|
717
786
|
let db = (await opendb(null, {
|
|
718
787
|
timestamps: false,
|
|
719
788
|
nullObjects: true,
|
|
@@ -728,10 +797,15 @@ module.exports = function(monastery, opendb) {
|
|
|
728
797
|
}})
|
|
729
798
|
|
|
730
799
|
// Subdocument data (null/string)
|
|
800
|
+
await expect(user.validate({ animals: 'notAnObject' })).rejects.toEqual([{
|
|
801
|
+
'detail': 'Value was not an object.',
|
|
802
|
+
'meta': {'detailLong': undefined, 'field': 'animals', 'model': 'user', 'rule': 'isObject'},
|
|
803
|
+
'status': '400',
|
|
804
|
+
'title': 'animals'
|
|
805
|
+
}])
|
|
731
806
|
await expect(user.validate({ animals: '', names: null })).resolves.toEqual({ animals: null, names: null })
|
|
732
807
|
|
|
733
808
|
db.close()
|
|
734
|
-
done()
|
|
735
809
|
})
|
|
736
810
|
|
|
737
811
|
test('Validation options', async () => {
|
|
@@ -788,13 +862,13 @@ module.exports = function(monastery, opendb) {
|
|
|
788
862
|
{ people2: [{ people3: [{}, {}] }] },
|
|
789
863
|
{ skipValidation: ['people2.$.people3.0.name'] }
|
|
790
864
|
)).rejects.toContainEqual({
|
|
791
|
-
detail:
|
|
792
|
-
status:
|
|
793
|
-
title:
|
|
865
|
+
detail: 'This field is required.',
|
|
866
|
+
status: '400',
|
|
867
|
+
title: 'people2.0.people3.1.name',
|
|
794
868
|
meta: {
|
|
795
|
-
field:
|
|
796
|
-
model:
|
|
797
|
-
rule:
|
|
869
|
+
field: 'name',
|
|
870
|
+
model: 'user3',
|
|
871
|
+
rule: 'required'
|
|
798
872
|
}
|
|
799
873
|
})
|
|
800
874
|
|
|
@@ -808,18 +882,18 @@ module.exports = function(monastery, opendb) {
|
|
|
808
882
|
|
|
809
883
|
// Non existing validation field entries
|
|
810
884
|
await expect(user3.validate({ people: [{}] }, { skipValidation: ['people.badField'] })).rejects.toContainEqual({
|
|
811
|
-
detail:
|
|
812
|
-
status:
|
|
813
|
-
title:
|
|
885
|
+
detail: 'This field is required.',
|
|
886
|
+
status: '400',
|
|
887
|
+
title: 'people.0.name',
|
|
814
888
|
meta: {
|
|
815
|
-
model:
|
|
816
|
-
field:
|
|
817
|
-
rule:
|
|
889
|
+
model: 'user3',
|
|
890
|
+
field: 'name',
|
|
891
|
+
rule: 'required'
|
|
818
892
|
}
|
|
819
893
|
})
|
|
820
894
|
})
|
|
821
895
|
|
|
822
|
-
test('Validation hooks', async (
|
|
896
|
+
test('Validation hooks', async () => {
|
|
823
897
|
let db = (await opendb(null)).db
|
|
824
898
|
let user = db.model('user', {
|
|
825
899
|
fields: {
|
|
@@ -841,16 +915,16 @@ module.exports = function(monastery, opendb) {
|
|
|
841
915
|
let userDoc = await user.insert({ data: { first: 'Martin', last: 'Luther' }})
|
|
842
916
|
|
|
843
917
|
// Catch validate (a)synchronous errors thrown in function or through `next(err)`
|
|
844
|
-
await expect(user.validate({ first: '' })).rejects.toThrow(
|
|
845
|
-
await expect(user.validate({ first: 'Martin' })).rejects.toThrow(
|
|
918
|
+
await expect(user.validate({ first: '' })).rejects.toThrow('beforeValidate error 1..')
|
|
919
|
+
await expect(user.validate({ first: 'Martin' })).rejects.toThrow('beforeValidate error 2..')
|
|
846
920
|
await expect(user.validate({ first: 'Martin', last: 'Luther' })).resolves.toEqual({
|
|
847
921
|
first: 'Martin',
|
|
848
922
|
last: 'Luther'
|
|
849
923
|
})
|
|
850
924
|
|
|
851
925
|
// Catch insert (a)synchronous errors thrown in function or through `next(err)`
|
|
852
|
-
await expect(user.insert({ data: { first: '' } })).rejects.toThrow(
|
|
853
|
-
await expect(user.insert({ data: { first: 'Martin' } })).rejects.toThrow(
|
|
926
|
+
await expect(user.insert({ data: { first: '' } })).rejects.toThrow('beforeValidate error 1..')
|
|
927
|
+
await expect(user.insert({ data: { first: 'Martin' } })).rejects.toThrow('beforeValidate error 2..')
|
|
854
928
|
await expect(user.insert({ data: { first: 'Martin', last: 'Luther' } })).resolves.toEqual({
|
|
855
929
|
_id: expect.any(Object),
|
|
856
930
|
first: 'Martin',
|
|
@@ -858,15 +932,17 @@ module.exports = function(monastery, opendb) {
|
|
|
858
932
|
})
|
|
859
933
|
|
|
860
934
|
// Catch update (a)synchronous errors thrown in function or through `next(err)`
|
|
861
|
-
await expect(user.update({ query: userDoc._id, data: { first: '' } }))
|
|
862
|
-
|
|
863
|
-
await expect(user.update({ query: userDoc._id, data: { first: 'Martin'
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
935
|
+
await expect(user.update({ query: userDoc._id, data: { first: '' } }))
|
|
936
|
+
.rejects.toThrow('beforeValidate error 1..')
|
|
937
|
+
await expect(user.update({ query: userDoc._id, data: { first: 'Martin' } }))
|
|
938
|
+
.rejects.toThrow('beforeValidate error 2..')
|
|
939
|
+
await expect(user.update({ query: userDoc._id, data: { first: 'Martin', last: 'Luther' } }))
|
|
940
|
+
.resolves.toEqual({
|
|
941
|
+
first: 'Martin',
|
|
942
|
+
last: 'Luther'
|
|
943
|
+
})
|
|
867
944
|
|
|
868
945
|
db.close()
|
|
869
|
-
done()
|
|
870
946
|
})
|
|
871
947
|
|
|
872
948
|
}
|
package/test/virtuals.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
module.exports = function(monastery, opendb) {
|
|
2
2
|
|
|
3
|
-
test('Virtuals', async (
|
|
3
|
+
test('Virtuals', async () => {
|
|
4
4
|
// Setup
|
|
5
5
|
let db = (await opendb(null)).db
|
|
6
6
|
// Test model setup
|
|
@@ -145,10 +145,9 @@ module.exports = function(monastery, opendb) {
|
|
|
145
145
|
})
|
|
146
146
|
|
|
147
147
|
db.close()
|
|
148
|
-
done()
|
|
149
148
|
})
|
|
150
149
|
|
|
151
|
-
test('Insert/update virtuals (validate)', async (
|
|
150
|
+
test('Insert/update virtuals (validate)', async () => {
|
|
152
151
|
// Setup
|
|
153
152
|
let db = (await opendb(null)).db
|
|
154
153
|
let user = db.model('user', {
|
|
@@ -246,7 +245,6 @@ module.exports = function(monastery, opendb) {
|
|
|
246
245
|
}
|
|
247
246
|
})
|
|
248
247
|
db.close()
|
|
249
|
-
done()
|
|
250
248
|
})
|
|
251
249
|
|
|
252
250
|
}
|