monastery 1.28.1 → 1.28.5
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/lib/index.js +0 -1
- package/lib/model-crud.js +32 -16
- package/lib/model-validate.js +11 -10
- package/lib/model.js +12 -4
- package/lib/rules.js +21 -29
- package/lib/util.js +13 -7
- package/package.json +17 -16
- package/plugins/images/index.js +4 -4
- package/test/blacklisting.js +4 -8
- package/test/crud.js +28 -31
- package/test/model.js +47 -51
- 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 +50 -43
- 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)',
|
|
@@ -494,7 +500,9 @@ module.exports = function(monastery, opendb) {
|
|
|
494
500
|
})
|
|
495
501
|
|
|
496
502
|
// subdocument in an array
|
|
497
|
-
await expect(user.validate({ animals: [{ name: 'benjamin' }] })).resolves.toEqual({
|
|
503
|
+
await expect(user.validate({ animals: [{ name: 'benjamin' }] })).resolves.toEqual({
|
|
504
|
+
animals: [{ name: 'benjamin' }]
|
|
505
|
+
})
|
|
498
506
|
await expect(user.validate({ animals: [{ name: 'ben' }] })).rejects.toContainEqual({
|
|
499
507
|
status: '400',
|
|
500
508
|
title: 'animals.0.name',
|
|
@@ -588,11 +596,11 @@ module.exports = function(monastery, opendb) {
|
|
|
588
596
|
|
|
589
597
|
// Index, mongodb connection error
|
|
590
598
|
await expect(user3._setupIndexes({ name: { type: 'string', index: 'text' }})).rejects
|
|
591
|
-
.toEqual({ type:
|
|
599
|
+
.toEqual({ type: 'info', detail: 'Skipping createIndex on the \'user3\' model, no mongodb connection found.' })
|
|
592
600
|
|
|
593
601
|
// Model id (Monk ObjectId)
|
|
594
|
-
let data = await user4.validate({ name:
|
|
595
|
-
await expect(data.name.toString()).toEqual(db.id(
|
|
602
|
+
let data = await user4.validate({ name: '5d4356299d0f010017602f6b' })
|
|
603
|
+
await expect(data.name.toString()).toEqual(db.id('5d4356299d0f010017602f6b').toString())
|
|
596
604
|
await expect(data.name).toEqual(expect.any(Object))
|
|
597
605
|
|
|
598
606
|
// Bad model id (Monk ObjectId)
|
|
@@ -617,7 +625,6 @@ module.exports = function(monastery, opendb) {
|
|
|
617
625
|
amount: { type: 'number', required: true },
|
|
618
626
|
}})
|
|
619
627
|
|
|
620
|
-
|
|
621
628
|
// MinLength
|
|
622
629
|
await expect(user.validate({ name: 'Martin Luther' })).resolves.toEqual({name: 'Martin Luther'})
|
|
623
630
|
await expect(user.validate({ name: 'Carl' })).rejects.toContainEqual({
|
|
@@ -686,14 +693,14 @@ module.exports = function(monastery, opendb) {
|
|
|
686
693
|
await expect(user.validate({ amount: 'bad' })).rejects.toContainEqual(mock2)
|
|
687
694
|
})
|
|
688
695
|
|
|
689
|
-
test('Schema default objects', async (
|
|
696
|
+
test('Schema default objects', async () => {
|
|
690
697
|
let db = (await opendb(null, {
|
|
691
698
|
timestamps: false,
|
|
692
699
|
defaultObjects: true,
|
|
693
700
|
serverSelectionTimeoutMS: 2000
|
|
694
701
|
})).db
|
|
695
702
|
|
|
696
|
-
let base = { names: [], animals: { dogs: [] }}
|
|
703
|
+
// let base = { names: [], animals: { dogs: [] }}
|
|
697
704
|
let user = db.model('user', { fields: {
|
|
698
705
|
name: { type: 'string' },
|
|
699
706
|
names: [{ type: 'string' }],
|
|
@@ -710,10 +717,9 @@ module.exports = function(monastery, opendb) {
|
|
|
710
717
|
})
|
|
711
718
|
|
|
712
719
|
db.close()
|
|
713
|
-
done()
|
|
714
720
|
})
|
|
715
721
|
|
|
716
|
-
test('Schema nullObjects', async (
|
|
722
|
+
test('Schema nullObjects', async () => {
|
|
717
723
|
let db = (await opendb(null, {
|
|
718
724
|
timestamps: false,
|
|
719
725
|
nullObjects: true,
|
|
@@ -731,7 +737,6 @@ module.exports = function(monastery, opendb) {
|
|
|
731
737
|
await expect(user.validate({ animals: '', names: null })).resolves.toEqual({ animals: null, names: null })
|
|
732
738
|
|
|
733
739
|
db.close()
|
|
734
|
-
done()
|
|
735
740
|
})
|
|
736
741
|
|
|
737
742
|
test('Validation options', async () => {
|
|
@@ -788,13 +793,13 @@ module.exports = function(monastery, opendb) {
|
|
|
788
793
|
{ people2: [{ people3: [{}, {}] }] },
|
|
789
794
|
{ skipValidation: ['people2.$.people3.0.name'] }
|
|
790
795
|
)).rejects.toContainEqual({
|
|
791
|
-
detail:
|
|
792
|
-
status:
|
|
793
|
-
title:
|
|
796
|
+
detail: 'This field is required.',
|
|
797
|
+
status: '400',
|
|
798
|
+
title: 'people2.0.people3.1.name',
|
|
794
799
|
meta: {
|
|
795
|
-
field:
|
|
796
|
-
model:
|
|
797
|
-
rule:
|
|
800
|
+
field: 'name',
|
|
801
|
+
model: 'user3',
|
|
802
|
+
rule: 'required'
|
|
798
803
|
}
|
|
799
804
|
})
|
|
800
805
|
|
|
@@ -808,18 +813,18 @@ module.exports = function(monastery, opendb) {
|
|
|
808
813
|
|
|
809
814
|
// Non existing validation field entries
|
|
810
815
|
await expect(user3.validate({ people: [{}] }, { skipValidation: ['people.badField'] })).rejects.toContainEqual({
|
|
811
|
-
detail:
|
|
812
|
-
status:
|
|
813
|
-
title:
|
|
816
|
+
detail: 'This field is required.',
|
|
817
|
+
status: '400',
|
|
818
|
+
title: 'people.0.name',
|
|
814
819
|
meta: {
|
|
815
|
-
model:
|
|
816
|
-
field:
|
|
817
|
-
rule:
|
|
820
|
+
model: 'user3',
|
|
821
|
+
field: 'name',
|
|
822
|
+
rule: 'required'
|
|
818
823
|
}
|
|
819
824
|
})
|
|
820
825
|
})
|
|
821
826
|
|
|
822
|
-
test('Validation hooks', async (
|
|
827
|
+
test('Validation hooks', async () => {
|
|
823
828
|
let db = (await opendb(null)).db
|
|
824
829
|
let user = db.model('user', {
|
|
825
830
|
fields: {
|
|
@@ -841,16 +846,16 @@ module.exports = function(monastery, opendb) {
|
|
|
841
846
|
let userDoc = await user.insert({ data: { first: 'Martin', last: 'Luther' }})
|
|
842
847
|
|
|
843
848
|
// 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(
|
|
849
|
+
await expect(user.validate({ first: '' })).rejects.toThrow('beforeValidate error 1..')
|
|
850
|
+
await expect(user.validate({ first: 'Martin' })).rejects.toThrow('beforeValidate error 2..')
|
|
846
851
|
await expect(user.validate({ first: 'Martin', last: 'Luther' })).resolves.toEqual({
|
|
847
852
|
first: 'Martin',
|
|
848
853
|
last: 'Luther'
|
|
849
854
|
})
|
|
850
855
|
|
|
851
856
|
// 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(
|
|
857
|
+
await expect(user.insert({ data: { first: '' } })).rejects.toThrow('beforeValidate error 1..')
|
|
858
|
+
await expect(user.insert({ data: { first: 'Martin' } })).rejects.toThrow('beforeValidate error 2..')
|
|
854
859
|
await expect(user.insert({ data: { first: 'Martin', last: 'Luther' } })).resolves.toEqual({
|
|
855
860
|
_id: expect.any(Object),
|
|
856
861
|
first: 'Martin',
|
|
@@ -858,15 +863,17 @@ module.exports = function(monastery, opendb) {
|
|
|
858
863
|
})
|
|
859
864
|
|
|
860
865
|
// 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
|
-
|
|
866
|
+
await expect(user.update({ query: userDoc._id, data: { first: '' } }))
|
|
867
|
+
.rejects.toThrow('beforeValidate error 1..')
|
|
868
|
+
await expect(user.update({ query: userDoc._id, data: { first: 'Martin' } }))
|
|
869
|
+
.rejects.toThrow('beforeValidate error 2..')
|
|
870
|
+
await expect(user.update({ query: userDoc._id, data: { first: 'Martin', last: 'Luther' } }))
|
|
871
|
+
.resolves.toEqual({
|
|
872
|
+
first: 'Martin',
|
|
873
|
+
last: 'Luther'
|
|
874
|
+
})
|
|
867
875
|
|
|
868
876
|
db.close()
|
|
869
|
-
done()
|
|
870
877
|
})
|
|
871
878
|
|
|
872
879
|
}
|
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
|
}
|