Skip to content

Commit

Permalink
fix: nullObjects were skipping required rules
Browse files Browse the repository at this point in the history
  • Loading branch information
boycce committed May 26, 2022
1 parent 0e57b4c commit 315af00
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/model-validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ module.exports = {
if (typeof value === 'undefined' && (!validateUndefined || !rule.validateUndefined)) return

// Ignore null (if nullObject is set on objects or arrays)
if (value === null && (field.isObject || field.isArray) && field.nullObject) return
if (value === null && (field.isObject || field.isArray) && field.nullObject && !rule.validateNull) return

// Ignore null
if (value === null && !(field.isObject || field.isArray) && !rule.validateNull) return
Expand Down
36 changes: 33 additions & 3 deletions test/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -808,8 +808,10 @@ module.exports = function(monastery, opendb) {
})
})

test('schema default rules', async () => {
test('schema options default', async () => {
// Setup
// todo: test objects
// todo: change test name
let db = (await opendb(false)).db
let user = db.model('user', { fields: {
name: { type: 'string', minLength: 7 },
Expand Down Expand Up @@ -889,7 +891,35 @@ module.exports = function(monastery, opendb) {
await expect(user.validate({ amount: 'bad' })).rejects.toContainEqual(mock2)
})

test('schema default objects', async () => {
test('schema options objects', async () => {
let db = (await opendb(null, {
timestamps: false,
nullObjects: true,
serverSelectionTimeoutMS: 2000
})).db
let user = db.model('user', {
fields: {
location: {
lat: { type: 'number' },
lng: { type: 'number' },
schema: { required: true },
},
},
})

// Subdocument data (null/string)
await expect(user.validate({ location: null })).rejects.toEqual([{
'detail': 'This field is required.',
'meta': {'detailLong': undefined, 'field': 'location', 'model': 'user', 'rule': 'required'},
'status': '400',
'title': 'location'
}])
await expect(user.validate({ location: {} })).resolves.toEqual({ location: {} })

db.close()
})

test('validate defaultObjects', async () => {
let db = (await opendb(null, {
timestamps: false,
defaultObjects: true,
Expand All @@ -915,7 +945,7 @@ module.exports = function(monastery, opendb) {
db.close()
})

test('schema nullObjects', async () => {
test('validate nullObjects', async () => {
let db = (await opendb(null, {
timestamps: false,
nullObjects: true,
Expand Down

0 comments on commit 315af00

Please sign in to comment.