monastery 1.42.1 → 2.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/changelog.md +4 -0
- package/docs/readme.md +2 -0
- package/lib/index.js +2 -2
- package/lib/model-crud.js +0 -4
- package/lib/model-validate.js +8 -25
- package/lib/model.js +37 -1
- package/package.json +2 -1
- package/test/model.js +146 -0
- package/test/validate.js +59 -49
package/changelog.md
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## [2.0.0](https://github.com/boycce/monastery/compare/1.42.2...2.0.0) (2023-12-06)
|
|
6
|
+
|
|
7
|
+
### [1.42.2](https://github.com/boycce/monastery/compare/1.42.1...1.42.2) (2023-10-31)
|
|
8
|
+
|
|
5
9
|
### [1.42.1](https://github.com/boycce/monastery/compare/1.42.0...1.42.1) (2023-10-09)
|
|
6
10
|
|
|
7
11
|
## [1.42.0](https://github.com/boycce/monastery/compare/1.41.2...1.42.0) (2023-10-09)
|
package/docs/readme.md
CHANGED
|
@@ -104,6 +104,8 @@ Coming soon...
|
|
|
104
104
|
- Add a warning if an invalid model is referenced in jthe schema
|
|
105
105
|
- Remove leading forward slashes from custom image paths (AWS adds this as a seperate folder)
|
|
106
106
|
- double check await db.model.remove({ query: idfromparam }) doesnt cause issues for null, undefined or '', but continue to allow {}
|
|
107
|
+
- ~~can't insert/update model id (maybe we can allow this and add _id to default insert/update blacklists)~~
|
|
108
|
+
- timstamps are blacklisted by default (instead of the `timestamps` opt), and can be switched off via blacklisting
|
|
107
109
|
|
|
108
110
|
## Versions
|
|
109
111
|
|
package/lib/index.js
CHANGED
|
@@ -98,10 +98,10 @@ let models = async function(pathname, waitForIndexes) {
|
|
|
98
98
|
let name = filename.replace('.js', '')
|
|
99
99
|
let filepath = path.join(pathname, filename)
|
|
100
100
|
// We can't use require() here since we need to be able to import both CJS and ES6 modules
|
|
101
|
-
let definition = (await import(filepath))
|
|
101
|
+
let definition = ((await import(filepath))||{}).default
|
|
102
102
|
// When a commonJS project uses babel (e.g. `nodemon -r @babel/register`, similar to `-r esm`), import()
|
|
103
103
|
// will return ES6 model definitions in another nested `default` object
|
|
104
|
-
if (definition
|
|
104
|
+
if (definition && definition.default) definition = definition.default
|
|
105
105
|
if (!definition) throw new Error(`The model definition for '${name}' must export a default object`)
|
|
106
106
|
// Wait for indexes to be created?
|
|
107
107
|
if (waitForIndexes) out[name] = await this.model(name, { ...definition, waitForIndexes })
|
package/lib/model-crud.js
CHANGED
|
@@ -25,13 +25,9 @@ module.exports = {
|
|
|
25
25
|
try {
|
|
26
26
|
opts = await this._queryObject(opts, 'insert')
|
|
27
27
|
|
|
28
|
-
// console.log(11, opts.data )
|
|
29
|
-
|
|
30
28
|
// Validate
|
|
31
29
|
let data = await this.validate(opts.data || {}, opts) // was { ...opts }
|
|
32
30
|
|
|
33
|
-
// console.log(22, data)
|
|
34
|
-
|
|
35
31
|
// Insert
|
|
36
32
|
await util.runSeries(this.beforeInsert.map(f => f.bind(opts, data)))
|
|
37
33
|
let response = await this._insert(data, util.omit(opts, this._queryOptions))
|
package/lib/model-validate.js
CHANGED
|
@@ -65,29 +65,18 @@ module.exports = {
|
|
|
65
65
|
_getMostSpecificKeyMatchingPath: function(object, path) {
|
|
66
66
|
/**
|
|
67
67
|
* Get all possible array variation matches from the object, and return the most specifc key
|
|
68
|
-
* @param {object} object - e.g. { 'pets.1.name', 'pets.$.name', 'pets.name', .. }
|
|
68
|
+
* @param {object} object - messages, e.g. { 'pets.1.name', 'pets.$.name', 'pets.name', .. }
|
|
69
69
|
* @path {string} path - must be a specifc path, e.g. 'pets.1.name'
|
|
70
70
|
* @return most specific key in object
|
|
71
|
-
*
|
|
72
|
-
* 1. Get all viable messages keys, e.g. (key)dogs.$ == (path)dogs.1
|
|
73
|
-
* 2. Order array key list by scoring, i.e. [0-9]=2, $=1, ''=0
|
|
74
|
-
* 3. Return first
|
|
75
71
|
*/
|
|
76
|
-
let
|
|
77
|
-
let
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
if (key.match(pathreg)) {
|
|
82
|
-
let score = (key.match(/\.[0-9]+/g)||[]).length * 1001
|
|
83
|
-
score += (key.match(/\.\$/g)||[]).length * 1000
|
|
84
|
-
keys.push({ score: score, key: key })
|
|
72
|
+
let key
|
|
73
|
+
for (let k in object) {
|
|
74
|
+
if (path.match(object[k].regex)) {
|
|
75
|
+
key = k
|
|
76
|
+
break
|
|
85
77
|
}
|
|
86
78
|
}
|
|
87
|
-
|
|
88
|
-
if (!keys.length) return
|
|
89
|
-
else if (keys.length == 1) return keys[0].key
|
|
90
|
-
return keys.sort((a, b) => a.score - b.score).reverse()[0].key // descending
|
|
79
|
+
return key
|
|
91
80
|
},
|
|
92
81
|
|
|
93
82
|
_validateFields: function(dataRoot, fields, data, opts, path) {
|
|
@@ -108,8 +97,6 @@ module.exports = {
|
|
|
108
97
|
let data2 = util.isArray(fields)? [] : {}
|
|
109
98
|
let timestamps = util.isDefined(opts.timestamps)? opts.timestamps : this.manager.timestamps
|
|
110
99
|
|
|
111
|
-
// console.log(8888, fields)
|
|
112
|
-
|
|
113
100
|
util.forEach(util.forceArray(data), function(data, i) {
|
|
114
101
|
util.forEach(fields, function(field, fieldName) {
|
|
115
102
|
let verrors = []
|
|
@@ -133,11 +120,8 @@ module.exports = {
|
|
|
133
120
|
}
|
|
134
121
|
}
|
|
135
122
|
|
|
136
|
-
// console.log(333,path3)
|
|
137
|
-
|
|
138
123
|
// Ignore blacklisted
|
|
139
124
|
if (this._pathBlacklisted(path3, opts.projectionValidate) && !schema.defaultOverride) return
|
|
140
|
-
// console.log(444,path3)
|
|
141
125
|
// Ignore insert only
|
|
142
126
|
if (opts.update && schema.insertOnly) return
|
|
143
127
|
// Ignore virtual fields
|
|
@@ -146,7 +130,6 @@ module.exports = {
|
|
|
146
130
|
if (isTypeRule && util.isFunction(isTypeRule.tryParse)) {
|
|
147
131
|
value = isTypeRule.tryParse.call(dataRoot, value, fieldName, this)
|
|
148
132
|
}
|
|
149
|
-
// console.log(555,path3)
|
|
150
133
|
|
|
151
134
|
// Schema field (ignore object/array schemas)
|
|
152
135
|
if (util.isSchema(field) && fieldName !== 'schema') {
|
|
@@ -242,7 +225,7 @@ module.exports = {
|
|
|
242
225
|
let rule = this.rules[ruleName] || rules[ruleName]
|
|
243
226
|
let fieldName = path.match(/[^.]+$/)[0]
|
|
244
227
|
let isDeepProp = path.match(/\./) // todo: not dot-notation
|
|
245
|
-
let ruleMessageKey = this._getMostSpecificKeyMatchingPath(this.messages, path)
|
|
228
|
+
let ruleMessageKey = this.messagesLen && this._getMostSpecificKeyMatchingPath(this.messages, path)
|
|
246
229
|
let ruleMessage = ruleMessageKey && this.messages[ruleMessageKey][ruleName]
|
|
247
230
|
let validateUndefined = util.isDefined(opts.validateUndefined) ? opts.validateUndefined : opts.insert || isDeepProp
|
|
248
231
|
if (!ruleMessage) ruleMessage = rule.message
|
package/lib/model.js
CHANGED
|
@@ -43,14 +43,50 @@ let Model = module.exports = function(name, opts, manager) {
|
|
|
43
43
|
? !opts.insertBL.includes('_id') && !opts.insertBL.includes('-_id') ? ['_id'].concat(opts.insertBL) : opts.insertBL
|
|
44
44
|
: ['_id'],
|
|
45
45
|
fields: { ...(util.deepCopy(opts.fields) || {}) },
|
|
46
|
-
findBL: opts.findBL || ['password'],
|
|
46
|
+
findBL: opts.findBL || ['password'], // todo: password should be removed
|
|
47
47
|
manager: manager,
|
|
48
48
|
messages: opts.messages || {},
|
|
49
|
+
messagesLen: Object.keys(opts.messages || {}).length > 0,
|
|
49
50
|
name: name,
|
|
50
51
|
rules: { ...(opts.rules || {}) },
|
|
51
52
|
updateBL: opts.updateBL || [],
|
|
52
53
|
})
|
|
53
54
|
|
|
55
|
+
// Sort messages by specifity first, then we can just return the first match
|
|
56
|
+
this.messages = Object
|
|
57
|
+
.keys(this.messages)
|
|
58
|
+
.sort((a, b) => {
|
|
59
|
+
function getScore(key) {
|
|
60
|
+
// Make sure the keys are sorted by specifity, e.g. the most specific keys are at the top
|
|
61
|
+
// That means the variable indexes need to be sorted last,
|
|
62
|
+
// e.g. 'gulls.1.name' is more specific than 'gulls.$.name'
|
|
63
|
+
// e.g. 'gulls.1.name' is more specific than 'gulls.1.$'
|
|
64
|
+
// e.g. 'gulls.1.$' is more specific than 'gulls.$.1'
|
|
65
|
+
// e.g. 'gulls.1.1.$' is more specific than 'gulls.$.1.1'
|
|
66
|
+
if (!key.match(/\.\$/)) return 0
|
|
67
|
+
let score = 0
|
|
68
|
+
let parts = key.split('.')
|
|
69
|
+
for (let i = 0; i < parts.length; i++) {
|
|
70
|
+
if (parts[i] == '$') score += 100 * (100 - i) // higher score is less specific
|
|
71
|
+
}
|
|
72
|
+
return score
|
|
73
|
+
}
|
|
74
|
+
const scoreA = getScore(a)
|
|
75
|
+
const scoreB = getScore(b)
|
|
76
|
+
// this.messages[a].score = scoreA
|
|
77
|
+
// this.messages[b].score = scoreB
|
|
78
|
+
return scoreA > scoreB ? 1 : (scoreA < scoreB ? -1 : 0)
|
|
79
|
+
})
|
|
80
|
+
.reduce((acc, key) => {
|
|
81
|
+
// Now covert the path to a regex
|
|
82
|
+
// e.g. pets.$.names.4.first => pets\.[0-9]+\.names\.4\.first
|
|
83
|
+
this.messages[key].regex = new RegExp(`^${key.replace(/\./g, '\\.').replace(/\.\$/g, '.[0-9]+')}$`)
|
|
84
|
+
// this.messages[key].regex = new RegExp(`^${key.replace(/\.\$/g, '(.[0-9]+|.)').replace(/\./g, '\\.')}$`)
|
|
85
|
+
// return an ordered object
|
|
86
|
+
acc[key] = this.messages[key]
|
|
87
|
+
return acc
|
|
88
|
+
}, {})
|
|
89
|
+
|
|
54
90
|
// Run before model hooks
|
|
55
91
|
for (let hook of this.manager.beforeModel) {
|
|
56
92
|
hook(this)
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "monastery",
|
|
3
3
|
"description": "⛪ A straight forward MongoDB ODM built around Monk",
|
|
4
4
|
"author": "Ricky Boyce",
|
|
5
|
-
"version": "
|
|
5
|
+
"version": "2.0.0",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "github:boycce/monastery",
|
|
8
8
|
"homepage": "https://boycce.github.io/monastery/",
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"docs": "cd docs && bundle exec jekyll serve --livereload --livereload-port 4001",
|
|
22
22
|
"lint": "eslint ./lib ./plugins ./test",
|
|
23
23
|
"mong": "nodemon resources/mong.js",
|
|
24
|
+
"major": "standard-version --release-as major && npm publish",
|
|
24
25
|
"minor": "standard-version --release-as minor && npm publish",
|
|
25
26
|
"patch": "standard-version --release-as patch && npm publish",
|
|
26
27
|
"release": "standard-version && npm publish && git push --tags",
|
package/test/model.js
CHANGED
|
@@ -170,6 +170,134 @@ module.exports = function(monastery, opendb) {
|
|
|
170
170
|
})
|
|
171
171
|
})
|
|
172
172
|
|
|
173
|
+
test('model setup with messages', async () => {
|
|
174
|
+
// Setup
|
|
175
|
+
let db = (await opendb(false)).db
|
|
176
|
+
let user = db.model('user', {
|
|
177
|
+
fields: {
|
|
178
|
+
name: { type: 'string' },
|
|
179
|
+
},
|
|
180
|
+
messages: {
|
|
181
|
+
// these are sorted when trhe model's initialised
|
|
182
|
+
'cats.name': {},
|
|
183
|
+
|
|
184
|
+
'dogs.name': {},
|
|
185
|
+
'dogs.$.name': {},
|
|
186
|
+
'dogs.1.name': {},
|
|
187
|
+
'dogs.$': {},
|
|
188
|
+
'dogs.1': {},
|
|
189
|
+
|
|
190
|
+
'pigs.name': {},
|
|
191
|
+
'pigs.$.name': {},
|
|
192
|
+
'pigs.1.name': {},
|
|
193
|
+
'pigs.2.name': {},
|
|
194
|
+
|
|
195
|
+
'gulls.$.1.$': {},
|
|
196
|
+
'gulls.1.$.1': {},
|
|
197
|
+
'gulls.$': {},
|
|
198
|
+
'gulls.$.$': {},
|
|
199
|
+
'gulls.$.$.1': {},
|
|
200
|
+
'gulls.$.1': {},
|
|
201
|
+
'gulls.1.$': {},
|
|
202
|
+
'gulls.1.1': {},
|
|
203
|
+
'gulls.1.1.$': {},
|
|
204
|
+
'gulls.name': {},
|
|
205
|
+
'gulls.$.name': {},
|
|
206
|
+
},
|
|
207
|
+
})
|
|
208
|
+
// Object with schema
|
|
209
|
+
// console.log(user.messages)
|
|
210
|
+
expect(Object.keys(user.messages)).toEqual([
|
|
211
|
+
'cats.name',
|
|
212
|
+
'dogs.name',
|
|
213
|
+
'dogs.1.name',
|
|
214
|
+
'dogs.1',
|
|
215
|
+
'pigs.name',
|
|
216
|
+
'pigs.1.name',
|
|
217
|
+
'pigs.2.name',
|
|
218
|
+
'gulls.1.1',
|
|
219
|
+
'gulls.name',
|
|
220
|
+
'gulls.1.1.$',
|
|
221
|
+
'gulls.1.$.1',
|
|
222
|
+
'gulls.1.$',
|
|
223
|
+
'dogs.$.name',
|
|
224
|
+
'dogs.$',
|
|
225
|
+
'pigs.$.name',
|
|
226
|
+
'gulls.$',
|
|
227
|
+
'gulls.$.1',
|
|
228
|
+
'gulls.$.name',
|
|
229
|
+
'gulls.$.1.$',
|
|
230
|
+
'gulls.$.$',
|
|
231
|
+
'gulls.$.$.1',
|
|
232
|
+
])
|
|
233
|
+
|
|
234
|
+
expect(user.messages).toEqual({
|
|
235
|
+
// these are sorted in model initialisation
|
|
236
|
+
'cats.name': {
|
|
237
|
+
'regex': /^cats\.name$/,
|
|
238
|
+
},
|
|
239
|
+
'dogs.$': {
|
|
240
|
+
'regex': /^dogs\.[0-9]+$/,
|
|
241
|
+
},
|
|
242
|
+
'dogs.$.name': {
|
|
243
|
+
'regex': /^dogs\.[0-9]+\.name$/,
|
|
244
|
+
},
|
|
245
|
+
'dogs.1': {
|
|
246
|
+
'regex': /^dogs\.1$/,
|
|
247
|
+
},
|
|
248
|
+
'dogs.1.name': {
|
|
249
|
+
'regex': /^dogs\.1\.name$/,
|
|
250
|
+
},
|
|
251
|
+
'dogs.name': {
|
|
252
|
+
'regex': /^dogs\.name$/,
|
|
253
|
+
},
|
|
254
|
+
'gulls.$': {
|
|
255
|
+
'regex': /^gulls\.[0-9]+$/,
|
|
256
|
+
},
|
|
257
|
+
'gulls.$.$': {
|
|
258
|
+
'regex': /^gulls\.[0-9]+\.[0-9]+$/,
|
|
259
|
+
},
|
|
260
|
+
'gulls.$.$.1': {
|
|
261
|
+
'regex': /^gulls\.[0-9]+\.[0-9]+\.1$/,
|
|
262
|
+
},
|
|
263
|
+
'gulls.$.1': {
|
|
264
|
+
'regex': /^gulls\.[0-9]+\.1$/,
|
|
265
|
+
},
|
|
266
|
+
'gulls.$.1.$': {
|
|
267
|
+
'regex': /^gulls\.[0-9]+\.1\.[0-9]+$/,
|
|
268
|
+
},
|
|
269
|
+
'gulls.$.name': {
|
|
270
|
+
'regex': /^gulls\.[0-9]+\.name$/,
|
|
271
|
+
},
|
|
272
|
+
'gulls.1.$': {
|
|
273
|
+
'regex': /^gulls\.1\.[0-9]+$/,
|
|
274
|
+
},
|
|
275
|
+
'gulls.1.$.1': {
|
|
276
|
+
'regex': /^gulls\.1\.[0-9]+\.1$/,
|
|
277
|
+
},
|
|
278
|
+
'gulls.1.1': {
|
|
279
|
+
'regex': /^gulls\.1\.1$/,
|
|
280
|
+
},
|
|
281
|
+
'gulls.1.1.$': {
|
|
282
|
+
'regex': /^gulls\.1\.1\.[0-9]+$/,
|
|
283
|
+
},
|
|
284
|
+
'gulls.name': {
|
|
285
|
+
'regex': /^gulls\.name$/,
|
|
286
|
+
},
|
|
287
|
+
'pigs.$.name': {
|
|
288
|
+
'regex': /^pigs\.[0-9]+\.name$/,
|
|
289
|
+
},
|
|
290
|
+
'pigs.1.name': {
|
|
291
|
+
'regex': /^pigs\.1\.name$/,
|
|
292
|
+
},
|
|
293
|
+
'pigs.2.name': {
|
|
294
|
+
'regex': /^pigs\.2\.name$/,
|
|
295
|
+
},
|
|
296
|
+
'pigs.name': {
|
|
297
|
+
'regex': /^pigs\.name$/,
|
|
298
|
+
},
|
|
299
|
+
})
|
|
300
|
+
}),
|
|
173
301
|
|
|
174
302
|
test('model reserved rules', async () => {
|
|
175
303
|
// Setup
|
|
@@ -462,6 +590,24 @@ module.exports = function(monastery, opendb) {
|
|
|
462
590
|
type: 'Point'
|
|
463
591
|
},
|
|
464
592
|
})
|
|
593
|
+
// Insert no 2dsphere point data
|
|
594
|
+
await expect(db.user3.insert({
|
|
595
|
+
data: {}
|
|
596
|
+
})).resolves.toEqual({
|
|
597
|
+
_id: expect.any(Object),
|
|
598
|
+
})
|
|
599
|
+
// Insert bad 2dsphere point data
|
|
600
|
+
let MongoError = require('mongodb').MongoError
|
|
601
|
+
let id = db.id()
|
|
602
|
+
await expect(db.user3.insert({
|
|
603
|
+
data: { _id: id, location: {} },
|
|
604
|
+
blacklist: ['-_id'],
|
|
605
|
+
})).rejects.toEqual(
|
|
606
|
+
new MongoError(
|
|
607
|
+
`Can't extract geo keys: { _id: ObjectId('${String(id)}'), location: { type: "Point" } }` +
|
|
608
|
+
' Point must be an array or object'
|
|
609
|
+
)
|
|
610
|
+
)
|
|
465
611
|
|
|
466
612
|
db.close()
|
|
467
613
|
})
|
package/test/validate.js
CHANGED
|
@@ -381,35 +381,43 @@ module.exports = function(monastery, opendb) {
|
|
|
381
381
|
})
|
|
382
382
|
|
|
383
383
|
test('validation getMostSpecificKeyMatchingPath', async () => {
|
|
384
|
-
let
|
|
385
|
-
let
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
384
|
+
let db = (await opendb(false)).db
|
|
385
|
+
let user = db.model('user', {
|
|
386
|
+
fields: {
|
|
387
|
+
name: { type: 'string' },
|
|
388
|
+
},
|
|
389
|
+
messages: {
|
|
390
|
+
// these are sorted when trhe model's initialised
|
|
391
|
+
'cats.name': {},
|
|
392
|
+
|
|
393
|
+
'dogs.name': {},
|
|
394
|
+
'dogs.$.name': {},
|
|
395
|
+
|
|
396
|
+
'pigs.name': {},
|
|
397
|
+
'pigs.$.name': {},
|
|
398
|
+
'pigs.1.name': {},
|
|
399
|
+
'pigs.2.name': {},
|
|
400
|
+
|
|
401
|
+
'gulls.$': {},
|
|
402
|
+
'gulls.$.$': {},
|
|
403
|
+
'gulls.name': {},
|
|
404
|
+
'gulls.$.name': {},
|
|
405
|
+
},
|
|
406
|
+
})
|
|
395
407
|
|
|
396
|
-
|
|
397
|
-
'gulls.$.$': true,
|
|
398
|
-
'gulls.name': true,
|
|
399
|
-
'gulls.$.name': true,
|
|
400
|
-
}
|
|
408
|
+
let fn = validate._getMostSpecificKeyMatchingPath
|
|
401
409
|
// subdocument
|
|
402
|
-
expect(fn(
|
|
410
|
+
expect(fn(user.messages, 'cats.name')).toEqual('cats.name')
|
|
403
411
|
// array subdocuments
|
|
404
|
-
expect(fn(
|
|
405
|
-
expect(fn(
|
|
406
|
-
expect(fn(
|
|
407
|
-
expect(fn(
|
|
408
|
-
expect(fn(
|
|
409
|
-
expect(fn(
|
|
412
|
+
// expect(fn(user.messages, 'cats.1.name')).toEqual('cats.name') // no longer matches
|
|
413
|
+
expect(fn(user.messages, 'dogs.1.name')).toEqual('dogs.$.name')
|
|
414
|
+
expect(fn(user.messages, 'dogs.2.name')).toEqual('dogs.$.name')
|
|
415
|
+
expect(fn(user.messages, 'pigs.1.name')).toEqual('pigs.1.name')
|
|
416
|
+
expect(fn(user.messages, 'pigs.2.name')).toEqual('pigs.2.name')
|
|
417
|
+
expect(fn(user.messages, 'pigs.3.name')).toEqual('pigs.$.name')
|
|
410
418
|
// array
|
|
411
|
-
expect(fn(
|
|
412
|
-
expect(fn(
|
|
419
|
+
expect(fn(user.messages, 'gulls.1.2')).toEqual('gulls.$.$')
|
|
420
|
+
expect(fn(user.messages, 'gulls.1')).toEqual('gulls.$')
|
|
413
421
|
})
|
|
414
422
|
|
|
415
423
|
test('validation default messages', async () => {
|
|
@@ -478,7 +486,7 @@ module.exports = function(monastery, opendb) {
|
|
|
478
486
|
messages: {
|
|
479
487
|
'name': { minLength: 'Oops min length is 4' },
|
|
480
488
|
'dog.name': { minLength: 'Oops min length is 4' },
|
|
481
|
-
'dogNames': { minLength: 'Oops min length is 4' },
|
|
489
|
+
'dogNames.$': { minLength: 'Oops min length is 4' },
|
|
482
490
|
}
|
|
483
491
|
})
|
|
484
492
|
|
|
@@ -519,24 +527,26 @@ module.exports = function(monastery, opendb) {
|
|
|
519
527
|
catNames: [{
|
|
520
528
|
name: { type: 'string', minLength: 4 }
|
|
521
529
|
}],
|
|
522
|
-
pigNames: [
|
|
523
|
-
|
|
524
|
-
|
|
530
|
+
pigNames: [
|
|
531
|
+
[{
|
|
532
|
+
name: { type: 'string', minLength: 4 },
|
|
533
|
+
}]
|
|
534
|
+
],
|
|
525
535
|
},
|
|
526
536
|
messages: {
|
|
527
537
|
'dogNames': { minLength: 'add one dog name' },
|
|
528
538
|
'dogNames.$': { minLength: 'add one sub dog name' },
|
|
529
539
|
|
|
530
|
-
'catNames
|
|
540
|
+
'catNames.$.name': { minLength: 'min length error (name)' },
|
|
531
541
|
'catNames.1.name': { minLength: 'min length error (1)' },
|
|
532
542
|
'catNames.2.name': { minLength: 'min length error (2)' },
|
|
533
543
|
|
|
534
|
-
'pigNames
|
|
535
|
-
'pigNames
|
|
536
|
-
'pigNames
|
|
537
|
-
'pigNames.2
|
|
538
|
-
'pigNames.0.2.name': { minLength: 'min length error (
|
|
539
|
-
'pigNames.$.2.name': { minLength: 'min length error (
|
|
544
|
+
// 'pigNames.$.$.name': { minLength: 'min length error (name)' },
|
|
545
|
+
'pigNames.$.$.name': { minLength: 'min length error ($ $)' }, // catches
|
|
546
|
+
'pigNames.$.1.name': { minLength: 'min length error ($ 1)' },
|
|
547
|
+
'pigNames.2.$.name': { minLength: 'min length error (2 $)' },
|
|
548
|
+
'pigNames.0.2.name': { minLength: 'min length error (0 2)' },
|
|
549
|
+
'pigNames.$.2.name': { minLength: 'min length error ($ 2)' },
|
|
540
550
|
}
|
|
541
551
|
})
|
|
542
552
|
|
|
@@ -587,7 +597,7 @@ module.exports = function(monastery, opendb) {
|
|
|
587
597
|
.rejects.toContainEqual({
|
|
588
598
|
status: '400',
|
|
589
599
|
title: 'pigNames.0.0.name',
|
|
590
|
-
detail: 'min length error ($)',
|
|
600
|
+
detail: 'min length error ($ $)',
|
|
591
601
|
meta: { rule: 'minLength', model: 'user', field: 'name' }
|
|
592
602
|
})
|
|
593
603
|
// array-subdocument-1-field error
|
|
@@ -595,7 +605,14 @@ module.exports = function(monastery, opendb) {
|
|
|
595
605
|
.rejects.toContainEqual({
|
|
596
606
|
status: '400',
|
|
597
607
|
title: 'pigNames.0.1.name',
|
|
598
|
-
detail: 'min length error (1)',
|
|
608
|
+
detail: 'min length error ($ 1)',
|
|
609
|
+
meta: { rule: 'minLength', model: 'user', field: 'name' }
|
|
610
|
+
})
|
|
611
|
+
// array-subdocument-2-0-field error (lower fallback)
|
|
612
|
+
await expect(user.validate({ pigNames: [[],[],[{ name: 'ben' }]] })).rejects.toContainEqual({
|
|
613
|
+
status: '400',
|
|
614
|
+
title: 'pigNames.2.0.name',
|
|
615
|
+
detail: 'min length error (2 $)',
|
|
599
616
|
meta: { rule: 'minLength', model: 'user', field: 'name' }
|
|
600
617
|
})
|
|
601
618
|
// array-subdocument-0-2-field error
|
|
@@ -603,22 +620,15 @@ module.exports = function(monastery, opendb) {
|
|
|
603
620
|
.rejects.toContainEqual({
|
|
604
621
|
status: '400',
|
|
605
622
|
title: 'pigNames.0.2.name',
|
|
606
|
-
detail: 'min length error (
|
|
623
|
+
detail: 'min length error (0 2)',
|
|
607
624
|
meta: { rule: 'minLength', model: 'user', field: 'name' }
|
|
608
625
|
})
|
|
609
626
|
// array-subdocument-2-0-field error (fallback)
|
|
610
|
-
await expect(user.validate({ pigNames: [[],[
|
|
627
|
+
await expect(user.validate({ pigNames: [[], [{ name: 'carla' },{ name: 'carla' },{ name: 'ben' }], []] }))
|
|
611
628
|
.rejects.toContainEqual({
|
|
612
629
|
status: '400',
|
|
613
|
-
title: 'pigNames.
|
|
614
|
-
detail: 'min length error (
|
|
615
|
-
meta: { rule: 'minLength', model: 'user', field: 'name' }
|
|
616
|
-
})
|
|
617
|
-
// array-subdocument-2-0-field error (lower fallback)
|
|
618
|
-
await expect(user.validate({ pigNames: [[],[],[{ name: 'ben' }]] })).rejects.toContainEqual({
|
|
619
|
-
status: '400',
|
|
620
|
-
title: 'pigNames.2.0.name',
|
|
621
|
-
detail: 'min length error (2)',
|
|
630
|
+
title: 'pigNames.1.2.name',
|
|
631
|
+
detail: 'min length error ($ 2)',
|
|
622
632
|
meta: { rule: 'minLength', model: 'user', field: 'name' }
|
|
623
633
|
})
|
|
624
634
|
})
|