Skip to content

Instantly share code, notes, and snippets.

@ksloan
Last active June 16, 2017 17:13
Show Gist options
  • Save ksloan/2a2131bb93f8ffecd10fff5260e1a7bd to your computer and use it in GitHub Desktop.
Save ksloan/2a2131bb93f8ffecd10fff5260e1a7bd to your computer and use it in GitHub Desktop.
Mongoose Schema: child schema strict throw
const mongoose = require('mongoose')
const childSchema = mongoose.Schema({
name: String
}, {
strict: 'throw'
})
const parentSchema = mongoose.Schema({
children: [childSchema]
}, {
strict: 'throw'
})
const ParentModel = mongoose.model('Parent', parentSchema)
test('Valid Parent Model', () => {
expect(
new ParentModel({
children: [{ name: 'test' }]
}).validateSync()
).toBe(undefined)
})
// This test fails: validateSync() does not throw
test('Extra Child Attribute Throws', () => {
expect(() => {
new ParentModel({
children: [{ extraAttributeNotInSchema: 'test' }]
}).validateSync()
}).toThrow()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment