monastery 1.32.4 → 1.32.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/changelog.md +7 -0
- package/lib/rules.js +8 -7
- package/package.json +1 -1
- package/test/validate.js +41 -5
package/changelog.md
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
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
|
+
### [1.32.5](https://github.com/boycce/monastery/compare/1.32.4...1.32.5) (2022-03-21)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* dates not enforcing numbers ([d5a2a53](https://github.com/boycce/monastery/commit/d5a2a533f497b3c9e50cbeb66407cf9bc08dd2e4))
|
|
11
|
+
|
|
5
12
|
### [1.32.4](https://github.com/boycce/monastery/compare/1.32.3...1.32.4) (2022-03-07)
|
|
6
13
|
|
|
7
14
|
### [1.32.3](https://github.com/boycce/monastery/compare/1.32.2...1.32.3) (2022-03-07)
|
package/lib/rules.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// Todo: remove stringnums in date/number/integer rules
|
|
1
2
|
let ObjectId = require('mongodb').ObjectId
|
|
2
3
|
let util = require('./util')
|
|
3
4
|
let validator = require('validator')
|
|
@@ -48,12 +49,12 @@ module.exports = {
|
|
|
48
49
|
message: 'Value was not a unix timestamp.',
|
|
49
50
|
tryParse: function(x) {
|
|
50
51
|
if (x === '') return null
|
|
51
|
-
if (util.isString(x) && x.match(/^[+-]
|
|
52
|
-
return isNaN(
|
|
52
|
+
if (util.isString(x) && x.match(/^[+-][0-9]+$/)) return x // keep string nums intact
|
|
53
|
+
return isNaN(Number(x)) || (!x && x!==0) || x === true? x : Number(x)
|
|
53
54
|
},
|
|
54
55
|
fn: function(x) {
|
|
55
|
-
if (util.isString(x) && x.match(/^[+-]
|
|
56
|
-
return typeof x === 'number'
|
|
56
|
+
if (util.isString(x) && x.match(/^[+-][0-9]+$/)) return true
|
|
57
|
+
return typeof x === 'number'
|
|
57
58
|
}
|
|
58
59
|
},
|
|
59
60
|
isImageObject: {
|
|
@@ -78,10 +79,10 @@ module.exports = {
|
|
|
78
79
|
tryParse: function(x) {
|
|
79
80
|
if (x === '') return null
|
|
80
81
|
if (util.isString(x) && x.match(/^[+-][0-9]+$/)) return x // keep string nums intact
|
|
81
|
-
return isNaN(parseInt(x)) || (!x && x!==0) || x===true? x : parseInt(x)
|
|
82
|
+
return isNaN(parseInt(x)) || (!x && x!==0) || x === true? x : parseInt(x)
|
|
82
83
|
},
|
|
83
84
|
fn: function(x) {
|
|
84
|
-
if (util.isString(x) && x.match(/^[+-]
|
|
85
|
+
if (util.isString(x) && x.match(/^[+-][0-9]+$/)) return true
|
|
85
86
|
return typeof x === 'number' && (parseInt(x) === x)
|
|
86
87
|
}
|
|
87
88
|
},
|
|
@@ -91,7 +92,7 @@ module.exports = {
|
|
|
91
92
|
tryParse: function(x) {
|
|
92
93
|
if (x === '') return null
|
|
93
94
|
if (util.isString(x) && x.match(/^[+-][0-9]+$/)) return x // keep string nums intact
|
|
94
|
-
return isNaN(Number(x)) || (!x && x!==0) || x===true? x : Number(x)
|
|
95
|
+
return isNaN(Number(x)) || (!x && x!==0) || x === true? x : Number(x)
|
|
95
96
|
},
|
|
96
97
|
fn: function(x) {
|
|
97
98
|
if (util.isString(x) && x.match(/^[+-][0-9]+$/)) return true
|
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": "1.32.
|
|
5
|
+
"version": "1.32.5",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "github:boycce/monastery",
|
|
8
8
|
"homepage": "https://boycce.github.io/monastery/",
|
package/test/validate.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// Todo: split out basic 'type' tests
|
|
2
|
+
|
|
1
3
|
let validate = require('../lib/model-validate')
|
|
2
4
|
|
|
3
5
|
module.exports = function(monastery, opendb) {
|
|
@@ -51,21 +53,55 @@ module.exports = function(monastery, opendb) {
|
|
|
51
53
|
meta: { rule: 'isString', model: 'user', field: 'name' }
|
|
52
54
|
})
|
|
53
55
|
|
|
56
|
+
|
|
54
57
|
// Type error (date)
|
|
55
|
-
|
|
56
|
-
|
|
58
|
+
let userdate = db.model('userdate', { fields: { amount: { type: 'date', required: true }}})
|
|
59
|
+
let userdate2 = db.model('userdate2', { fields: { amount: { type: 'date' }}})
|
|
60
|
+
await expect(userdate.validate({ amount: 0 })).resolves.toEqual({ amount: 0 })
|
|
61
|
+
await expect(userdate.validate({ amount: '0' })).resolves.toEqual({ amount: 0 })
|
|
62
|
+
await expect(userdate.validate({ amount: '1646778655000' })).resolves.toEqual({ amount: 1646778655000 })
|
|
63
|
+
await expect(userdate2.validate({ amount: '' })).resolves.toEqual({ amount: null })
|
|
64
|
+
await expect(userdate2.validate({ amount: null })).resolves.toEqual({ amount: null })
|
|
65
|
+
await expect(userdate.validate({ amount: 'badnum' })).rejects.toEqual([{
|
|
57
66
|
status: '400',
|
|
58
|
-
title: '
|
|
67
|
+
title: 'amount',
|
|
59
68
|
detail: 'Value was not a unix timestamp.',
|
|
60
|
-
meta: { rule: 'isDate', model: '
|
|
61
|
-
})
|
|
69
|
+
meta: { rule: 'isDate', model: 'userdate', field: 'amount' }
|
|
70
|
+
}])
|
|
71
|
+
await expect(userdate.validate({ amount: false })).rejects.toEqual([{
|
|
72
|
+
status: '400',
|
|
73
|
+
title: 'amount',
|
|
74
|
+
detail: 'Value was not a unix timestamp.',
|
|
75
|
+
meta: { rule: 'isDate', model: 'userdate', field: 'amount' }
|
|
76
|
+
}])
|
|
77
|
+
await expect(userdate.validate({ amount: undefined })).rejects.toEqual([{
|
|
78
|
+
status: '400',
|
|
79
|
+
title: 'amount',
|
|
80
|
+
detail: 'This field is required.',
|
|
81
|
+
meta: { rule: 'required', model: 'userdate', field: 'amount' },
|
|
82
|
+
}])
|
|
83
|
+
await expect(userdate.validate({ amount: null })).rejects.toEqual([{
|
|
84
|
+
status: '400',
|
|
85
|
+
title: 'amount',
|
|
86
|
+
detail: 'This field is required.',
|
|
87
|
+
meta: { rule: 'required', model: 'userdate', field: 'amount' },
|
|
88
|
+
}])
|
|
89
|
+
|
|
62
90
|
|
|
63
91
|
// Type error (number)
|
|
64
92
|
let usernum = db.model('usernum', { fields: { amount: { type: 'number', required: true }}})
|
|
65
93
|
let usernum2 = db.model('usernum2', { fields: { amount: { type: 'number' }}})
|
|
66
94
|
await expect(usernum.validate({ amount: 0 })).resolves.toEqual({ amount: 0 })
|
|
67
95
|
await expect(usernum.validate({ amount: '0' })).resolves.toEqual({ amount: 0 })
|
|
96
|
+
await expect(usernum.validate({ amount: '1646778655000' })).resolves.toEqual({ amount: 1646778655000 })
|
|
68
97
|
await expect(usernum2.validate({ amount: '' })).resolves.toEqual({ amount: null })
|
|
98
|
+
await expect(usernum2.validate({ amount: null })).resolves.toEqual({ amount: null })
|
|
99
|
+
await expect(usernum.validate({ amount: 'badnum' })).rejects.toEqual([{
|
|
100
|
+
status: '400',
|
|
101
|
+
title: 'amount',
|
|
102
|
+
detail: 'Value was not a number.',
|
|
103
|
+
meta: { rule: 'isNumber', model: 'usernum', field: 'amount' }
|
|
104
|
+
}])
|
|
69
105
|
await expect(usernum.validate({ amount: false })).rejects.toEqual([{
|
|
70
106
|
status: '400',
|
|
71
107
|
title: 'amount',
|