Created
August 22, 2017 04:53
-
-
Save binki/cf735173ac4c5b17376f8f69c846a558 to your computer and use it in GitHub Desktop.
validators ignore undefined (as documented)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ohnob@DESKTOP-A4G2C0J MSYS ~/repos/mongoose-validators-hard-to-use | |
$ node --version | |
v6.11.1 | |
ohnob@DESKTOP-A4G2C0J MSYS ~/repos/mongoose-validators-hard-to-use | |
$ npm list mongoose | |
[email protected] C:\Users\ohnob\repos\mongoose-validators-hard-to-use | |
`-- [email protected] extraneous | |
npm ERR! extraneous: [email protected] C:\Users\ohnob\repos\mongoose-validators-hard-to-use\node_modules\mongoose | |
ohnob@DESKTOP-A4G2C0J MSYS ~/repos/mongoose-validators-hard-to-use | |
$ node validators-ignore-undefined.js | |
assert.js:81 | |
throw new assert.AssertionError({ | |
^ | |
AssertionError: undefined == true | |
at Object.<anonymous> (C:\Users\ohnob\repos\mongoose-validators-hard-to-use\validators-ignore-undefined.js:23:8) | |
at Module._compile (module.js:570:32) | |
at Object.Module._extensions..js (module.js:579:10) | |
at Module.load (module.js:487:32) | |
at tryModuleLoad (module.js:446:12) | |
at Function.Module._load (module.js:438:3) | |
at Module.runMain (module.js:604:10) | |
at run (bootstrap_node.js:389:7) | |
at startup (bootstrap_node.js:149:9) | |
at bootstrap_node.js:504:3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const assert = require('assert'); | |
const mongoose = require('mongoose'); | |
const ThingSchema = new mongoose.Schema({ | |
// https://stackoverflow.com/a/44336895/429091, doesn’t support | |
// subdocuments. | |
undefinedDisallowed: { | |
type: String, | |
valiate: { | |
validator: function (v) { | |
return v !== undefined; | |
}, | |
}, | |
default: null, | |
}, | |
}); | |
const Thing = mongoose.model('Thing', ThingSchema); | |
const thing = new Thing(); | |
assert.ifError(thing.validateSync()); | |
thing.undefinedDisallowed = undefined; | |
assert.ok(thing.validateSync()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment