schematype.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. 'use strict';
  2. /*!
  3. * Module dependencies.
  4. */
  5. const MongooseError = require('./error');
  6. const $exists = require('./schema/operators/exists');
  7. const $type = require('./schema/operators/type');
  8. const get = require('./helpers/get');
  9. const immediate = require('./helpers/immediate');
  10. const schemaTypeSymbol = require('./helpers/symbols').schemaTypeSymbol;
  11. const util = require('util');
  12. const utils = require('./utils');
  13. const validatorErrorSymbol = require('./helpers/symbols').validatorErrorSymbol;
  14. const CastError = MongooseError.CastError;
  15. const ValidatorError = MongooseError.ValidatorError;
  16. /**
  17. * SchemaType constructor. Do **not** instantiate `SchemaType` directly.
  18. * Mongoose converts your schema paths into SchemaTypes automatically.
  19. *
  20. * ####Example:
  21. *
  22. * const schema = new Schema({ name: String });
  23. * schema.path('name') instanceof SchemaType; // true
  24. *
  25. * @param {String} path
  26. * @param {Object} [options]
  27. * @param {String} [instance]
  28. * @api public
  29. */
  30. function SchemaType(path, options, instance) {
  31. this[schemaTypeSymbol] = true;
  32. this.path = path;
  33. this.instance = instance;
  34. this.validators = options != null && Array.isArray(options.validators) ?
  35. [].concat(options.validators) :
  36. [];
  37. this.getters = this.constructor.hasOwnProperty('getters') ?
  38. this.constructor.getters.slice() :
  39. [];
  40. this.setters = [];
  41. this.options = options;
  42. this._index = null;
  43. this.selected;
  44. for (const prop in options) {
  45. if (this[prop] && typeof this[prop] === 'function') {
  46. // { unique: true, index: true }
  47. if (prop === 'index' && this._index) {
  48. if (options.index === false) {
  49. const index = this._index;
  50. if (typeof index === 'object' && index != null) {
  51. if (index.unique) {
  52. throw new Error('Path "' + this.path + '" may not have `index` ' +
  53. 'set to false and `unique` set to true');
  54. }
  55. if (index.sparse) {
  56. throw new Error('Path "' + this.path + '" may not have `index` ' +
  57. 'set to false and `sparse` set to true');
  58. }
  59. }
  60. this._index = false;
  61. }
  62. continue;
  63. }
  64. const val = options[prop];
  65. // Special case so we don't screw up array defaults, see gh-5780
  66. if (prop === 'default') {
  67. this.default(val);
  68. continue;
  69. }
  70. const opts = Array.isArray(val) ? val : [val];
  71. this[prop].apply(this, opts);
  72. }
  73. }
  74. Object.defineProperty(this, '$$context', {
  75. enumerable: false,
  76. configurable: false,
  77. writable: true,
  78. value: null
  79. });
  80. }
  81. /**
  82. * Get/set the function used to cast arbitrary values to this type.
  83. *
  84. * ####Example:
  85. *
  86. * // Disallow `null` for numbers, and don't try to cast any values to
  87. * // numbers, so even strings like '123' will cause a CastError.
  88. * mongoose.Number.cast(function(v) {
  89. * assert.ok(v === undefined || typeof v === 'number');
  90. * return v;
  91. * });
  92. *
  93. * @param {Function|false} caster Function that casts arbitrary values to this type, or throws an error if casting failed
  94. * @return {Function}
  95. * @static
  96. * @receiver SchemaType
  97. * @function cast
  98. * @api public
  99. */
  100. SchemaType.cast = function cast(caster) {
  101. if (arguments.length === 0) {
  102. return this._cast;
  103. }
  104. if (caster === false) {
  105. caster = v => v;
  106. }
  107. this._cast = caster;
  108. return this._cast;
  109. };
  110. /**
  111. * Attaches a getter for all instances of this schema type.
  112. *
  113. * ####Example:
  114. *
  115. * // Make all numbers round down
  116. * mongoose.Number.get(function(v) { return Math.floor(v); });
  117. *
  118. * @param {Function} getter
  119. * @return {this}
  120. * @static
  121. * @receiver SchemaType
  122. * @function get
  123. * @api public
  124. */
  125. SchemaType.get = function(getter) {
  126. this.getters = this.hasOwnProperty('getters') ? this.getters : [];
  127. this.getters.push(getter);
  128. };
  129. /**
  130. * Sets a default value for this SchemaType.
  131. *
  132. * ####Example:
  133. *
  134. * var schema = new Schema({ n: { type: Number, default: 10 })
  135. * var M = db.model('M', schema)
  136. * var m = new M;
  137. * console.log(m.n) // 10
  138. *
  139. * Defaults can be either `functions` which return the value to use as the default or the literal value itself. Either way, the value will be cast based on its schema type before being set during document creation.
  140. *
  141. * ####Example:
  142. *
  143. * // values are cast:
  144. * var schema = new Schema({ aNumber: { type: Number, default: 4.815162342 }})
  145. * var M = db.model('M', schema)
  146. * var m = new M;
  147. * console.log(m.aNumber) // 4.815162342
  148. *
  149. * // default unique objects for Mixed types:
  150. * var schema = new Schema({ mixed: Schema.Types.Mixed });
  151. * schema.path('mixed').default(function () {
  152. * return {};
  153. * });
  154. *
  155. * // if we don't use a function to return object literals for Mixed defaults,
  156. * // each document will receive a reference to the same object literal creating
  157. * // a "shared" object instance:
  158. * var schema = new Schema({ mixed: Schema.Types.Mixed });
  159. * schema.path('mixed').default({});
  160. * var M = db.model('M', schema);
  161. * var m1 = new M;
  162. * m1.mixed.added = 1;
  163. * console.log(m1.mixed); // { added: 1 }
  164. * var m2 = new M;
  165. * console.log(m2.mixed); // { added: 1 }
  166. *
  167. * @param {Function|any} val the default value
  168. * @return {defaultValue}
  169. * @api public
  170. */
  171. SchemaType.prototype.default = function(val) {
  172. if (arguments.length === 1) {
  173. if (val === void 0) {
  174. this.defaultValue = void 0;
  175. return void 0;
  176. }
  177. this.defaultValue = val;
  178. return this.defaultValue;
  179. } else if (arguments.length > 1) {
  180. this.defaultValue = utils.args(arguments);
  181. }
  182. return this.defaultValue;
  183. };
  184. /**
  185. * Declares the index options for this schematype.
  186. *
  187. * ####Example:
  188. *
  189. * var s = new Schema({ name: { type: String, index: true })
  190. * var s = new Schema({ loc: { type: [Number], index: 'hashed' })
  191. * var s = new Schema({ loc: { type: [Number], index: '2d', sparse: true })
  192. * var s = new Schema({ loc: { type: [Number], index: { type: '2dsphere', sparse: true }})
  193. * var s = new Schema({ date: { type: Date, index: { unique: true, expires: '1d' }})
  194. * Schema.path('my.path').index(true);
  195. * Schema.path('my.date').index({ expires: 60 });
  196. * Schema.path('my.path').index({ unique: true, sparse: true });
  197. *
  198. * ####NOTE:
  199. *
  200. * _Indexes are created [in the background](https://docs.mongodb.com/manual/core/index-creation/#index-creation-background)
  201. * by default. If `background` is set to `false`, MongoDB will not execute any
  202. * read/write operations you send until the index build.
  203. * Specify `background: false` to override Mongoose's default._
  204. *
  205. * @param {Object|Boolean|String} options
  206. * @return {SchemaType} this
  207. * @api public
  208. */
  209. SchemaType.prototype.index = function(options) {
  210. this._index = options;
  211. utils.expires(this._index);
  212. return this;
  213. };
  214. /**
  215. * Declares an unique index.
  216. *
  217. * ####Example:
  218. *
  219. * var s = new Schema({ name: { type: String, unique: true }});
  220. * Schema.path('name').index({ unique: true });
  221. *
  222. * _NOTE: violating the constraint returns an `E11000` error from MongoDB when saving, not a Mongoose validation error._
  223. *
  224. * @param {Boolean} bool
  225. * @return {SchemaType} this
  226. * @api public
  227. */
  228. SchemaType.prototype.unique = function(bool) {
  229. if (this._index === false) {
  230. if (!bool) {
  231. return;
  232. }
  233. throw new Error('Path "' + this.path + '" may not have `index` set to ' +
  234. 'false and `unique` set to true');
  235. }
  236. if (this._index == null || this._index === true) {
  237. this._index = {};
  238. } else if (typeof this._index === 'string') {
  239. this._index = {type: this._index};
  240. }
  241. this._index.unique = bool;
  242. return this;
  243. };
  244. /**
  245. * Declares a full text index.
  246. *
  247. * ###Example:
  248. *
  249. * var s = new Schema({name : {type: String, text : true })
  250. * Schema.path('name').index({text : true});
  251. * @param {Boolean} bool
  252. * @return {SchemaType} this
  253. * @api public
  254. */
  255. SchemaType.prototype.text = function(bool) {
  256. if (this._index === false) {
  257. if (!bool) {
  258. return;
  259. }
  260. throw new Error('Path "' + this.path + '" may not have `index` set to ' +
  261. 'false and `text` set to true');
  262. }
  263. if (this._index === null || this._index === undefined ||
  264. typeof this._index === 'boolean') {
  265. this._index = {};
  266. } else if (typeof this._index === 'string') {
  267. this._index = {type: this._index};
  268. }
  269. this._index.text = bool;
  270. return this;
  271. };
  272. /**
  273. * Declares a sparse index.
  274. *
  275. * ####Example:
  276. *
  277. * var s = new Schema({ name: { type: String, sparse: true })
  278. * Schema.path('name').index({ sparse: true });
  279. *
  280. * @param {Boolean} bool
  281. * @return {SchemaType} this
  282. * @api public
  283. */
  284. SchemaType.prototype.sparse = function(bool) {
  285. if (this._index === false) {
  286. if (!bool) {
  287. return;
  288. }
  289. throw new Error('Path "' + this.path + '" may not have `index` set to ' +
  290. 'false and `sparse` set to true');
  291. }
  292. if (this._index == null || typeof this._index === 'boolean') {
  293. this._index = {};
  294. } else if (typeof this._index === 'string') {
  295. this._index = {type: this._index};
  296. }
  297. this._index.sparse = bool;
  298. return this;
  299. };
  300. /**
  301. * Adds a setter to this schematype.
  302. *
  303. * ####Example:
  304. *
  305. * function capitalize (val) {
  306. * if (typeof val !== 'string') val = '';
  307. * return val.charAt(0).toUpperCase() + val.substring(1);
  308. * }
  309. *
  310. * // defining within the schema
  311. * var s = new Schema({ name: { type: String, set: capitalize }});
  312. *
  313. * // or with the SchemaType
  314. * var s = new Schema({ name: String })
  315. * s.path('name').set(capitalize);
  316. *
  317. * Setters allow you to transform the data before it gets to the raw mongodb
  318. * document or query.
  319. *
  320. * Suppose you are implementing user registration for a website. Users provide
  321. * an email and password, which gets saved to mongodb. The email is a string
  322. * that you will want to normalize to lower case, in order to avoid one email
  323. * having more than one account -- e.g., otherwise, avenue@q.com can be registered for 2 accounts via avenue@q.com and AvEnUe@Q.CoM.
  324. *
  325. * You can set up email lower case normalization easily via a Mongoose setter.
  326. *
  327. * function toLower(v) {
  328. * return v.toLowerCase();
  329. * }
  330. *
  331. * var UserSchema = new Schema({
  332. * email: { type: String, set: toLower }
  333. * });
  334. *
  335. * var User = db.model('User', UserSchema);
  336. *
  337. * var user = new User({email: 'AVENUE@Q.COM'});
  338. * console.log(user.email); // 'avenue@q.com'
  339. *
  340. * // or
  341. * var user = new User();
  342. * user.email = 'Avenue@Q.com';
  343. * console.log(user.email); // 'avenue@q.com'
  344. * User.updateOne({ _id: _id }, { $set: { email: 'AVENUE@Q.COM' } }); // update to 'avenue@q.com'
  345. *
  346. * As you can see above, setters allow you to transform the data before it
  347. * stored in MongoDB, or before executing a query.
  348. *
  349. * _NOTE: we could have also just used the built-in `lowercase: true` SchemaType option instead of defining our own function._
  350. *
  351. * new Schema({ email: { type: String, lowercase: true }})
  352. *
  353. * Setters are also passed a second argument, the schematype on which the setter was defined. This allows for tailored behavior based on options passed in the schema.
  354. *
  355. * function inspector (val, schematype) {
  356. * if (schematype.options.required) {
  357. * return schematype.path + ' is required';
  358. * } else {
  359. * return val;
  360. * }
  361. * }
  362. *
  363. * var VirusSchema = new Schema({
  364. * name: { type: String, required: true, set: inspector },
  365. * taxonomy: { type: String, set: inspector }
  366. * })
  367. *
  368. * var Virus = db.model('Virus', VirusSchema);
  369. * var v = new Virus({ name: 'Parvoviridae', taxonomy: 'Parvovirinae' });
  370. *
  371. * console.log(v.name); // name is required
  372. * console.log(v.taxonomy); // Parvovirinae
  373. *
  374. * You can also use setters to modify other properties on the document. If
  375. * you're setting a property `name` on a document, the setter will run with
  376. * `this` as the document. Be careful, in mongoose 5 setters will also run
  377. * when querying by `name` with `this` as the query.
  378. *
  379. * ```javascript
  380. * const nameSchema = new Schema({ name: String, keywords: [String] });
  381. * nameSchema.path('name').set(function(v) {
  382. * // Need to check if `this` is a document, because in mongoose 5
  383. * // setters will also run on queries, in which case `this` will be a
  384. * // mongoose query object.
  385. * if (this instanceof Document && v != null) {
  386. * this.keywords = v.split(' ');
  387. * }
  388. * return v;
  389. * });
  390. * ```
  391. *
  392. * @param {Function} fn
  393. * @return {SchemaType} this
  394. * @api public
  395. */
  396. SchemaType.prototype.set = function(fn) {
  397. if (typeof fn !== 'function') {
  398. throw new TypeError('A setter must be a function.');
  399. }
  400. this.setters.push(fn);
  401. return this;
  402. };
  403. /**
  404. * Adds a getter to this schematype.
  405. *
  406. * ####Example:
  407. *
  408. * function dob (val) {
  409. * if (!val) return val;
  410. * return (val.getMonth() + 1) + "/" + val.getDate() + "/" + val.getFullYear();
  411. * }
  412. *
  413. * // defining within the schema
  414. * var s = new Schema({ born: { type: Date, get: dob })
  415. *
  416. * // or by retreiving its SchemaType
  417. * var s = new Schema({ born: Date })
  418. * s.path('born').get(dob)
  419. *
  420. * Getters allow you to transform the representation of the data as it travels from the raw mongodb document to the value that you see.
  421. *
  422. * Suppose you are storing credit card numbers and you want to hide everything except the last 4 digits to the mongoose user. You can do so by defining a getter in the following way:
  423. *
  424. * function obfuscate (cc) {
  425. * return '****-****-****-' + cc.slice(cc.length-4, cc.length);
  426. * }
  427. *
  428. * var AccountSchema = new Schema({
  429. * creditCardNumber: { type: String, get: obfuscate }
  430. * });
  431. *
  432. * var Account = db.model('Account', AccountSchema);
  433. *
  434. * Account.findById(id, function (err, found) {
  435. * console.log(found.creditCardNumber); // '****-****-****-1234'
  436. * });
  437. *
  438. * Getters are also passed a second argument, the schematype on which the getter was defined. This allows for tailored behavior based on options passed in the schema.
  439. *
  440. * function inspector (val, schematype) {
  441. * if (schematype.options.required) {
  442. * return schematype.path + ' is required';
  443. * } else {
  444. * return schematype.path + ' is not';
  445. * }
  446. * }
  447. *
  448. * var VirusSchema = new Schema({
  449. * name: { type: String, required: true, get: inspector },
  450. * taxonomy: { type: String, get: inspector }
  451. * })
  452. *
  453. * var Virus = db.model('Virus', VirusSchema);
  454. *
  455. * Virus.findById(id, function (err, virus) {
  456. * console.log(virus.name); // name is required
  457. * console.log(virus.taxonomy); // taxonomy is not
  458. * })
  459. *
  460. * @param {Function} fn
  461. * @return {SchemaType} this
  462. * @api public
  463. */
  464. SchemaType.prototype.get = function(fn) {
  465. if (typeof fn !== 'function') {
  466. throw new TypeError('A getter must be a function.');
  467. }
  468. this.getters.push(fn);
  469. return this;
  470. };
  471. /**
  472. * Adds validator(s) for this document path.
  473. *
  474. * Validators always receive the value to validate as their first argument and
  475. * must return `Boolean`. Returning `false` or throwing an error means
  476. * validation failed.
  477. *
  478. * The error message argument is optional. If not passed, the [default generic error message template](#error_messages_MongooseError-messages) will be used.
  479. *
  480. * ####Examples:
  481. *
  482. * // make sure every value is equal to "something"
  483. * function validator (val) {
  484. * return val == 'something';
  485. * }
  486. * new Schema({ name: { type: String, validate: validator }});
  487. *
  488. * // with a custom error message
  489. *
  490. * var custom = [validator, 'Uh oh, {PATH} does not equal "something".']
  491. * new Schema({ name: { type: String, validate: custom }});
  492. *
  493. * // adding many validators at a time
  494. *
  495. * var many = [
  496. * { validator: validator, msg: 'uh oh' }
  497. * , { validator: anotherValidator, msg: 'failed' }
  498. * ]
  499. * new Schema({ name: { type: String, validate: many }});
  500. *
  501. * // or utilizing SchemaType methods directly:
  502. *
  503. * var schema = new Schema({ name: 'string' });
  504. * schema.path('name').validate(validator, 'validation of `{PATH}` failed with value `{VALUE}`');
  505. *
  506. * ####Error message templates:
  507. *
  508. * From the examples above, you may have noticed that error messages support
  509. * basic templating. There are a few other template keywords besides `{PATH}`
  510. * and `{VALUE}` too. To find out more, details are available
  511. * [here](#error_messages_MongooseError.messages).
  512. *
  513. * If Mongoose's built-in error message templating isn't enough, Mongoose
  514. * supports setting the `message` property to a function.
  515. *
  516. * schema.path('name').validate({
  517. * validator: function() { return v.length > 5; },
  518. * // `errors['name']` will be "name must have length 5, got 'foo'"
  519. * message: function(props) {
  520. * return `${props.path} must have length 5, got '${props.value}'`;
  521. * }
  522. * });
  523. *
  524. * To bypass Mongoose's error messages and just copy the error message that
  525. * the validator throws, do this:
  526. *
  527. * schema.path('name').validate({
  528. * validator: function() { throw new Error('Oops!'); },
  529. * // `errors['name']` will be "Oops!"
  530. * message: function(props) { return props.reason.message; }
  531. * });
  532. *
  533. * ####Asynchronous validation:
  534. *
  535. * Mongoose supports validators that return a promise. A validator that returns
  536. * a promise is called an _async validator_. Async validators run in
  537. * parallel, and `validate()` will wait until all async validators have settled.
  538. *
  539. * schema.path('name').validate({
  540. * validator: function (value) {
  541. * return new Promise(function (resolve, reject) {
  542. * resolve(false); // validation failed
  543. * });
  544. * }
  545. * });
  546. *
  547. * You might use asynchronous validators to retreive other documents from the database to validate against or to meet other I/O bound validation needs.
  548. *
  549. * Validation occurs `pre('save')` or whenever you manually execute [document#validate](#document_Document-validate).
  550. *
  551. * If validation fails during `pre('save')` and no callback was passed to receive the error, an `error` event will be emitted on your Models associated db [connection](#connection_Connection), passing the validation error object along.
  552. *
  553. * var conn = mongoose.createConnection(..);
  554. * conn.on('error', handleError);
  555. *
  556. * var Product = conn.model('Product', yourSchema);
  557. * var dvd = new Product(..);
  558. * dvd.save(); // emits error on the `conn` above
  559. *
  560. * If you want to handle these errors at the Model level, add an `error`
  561. * listener to your Model as shown below.
  562. *
  563. * // registering an error listener on the Model lets us handle errors more locally
  564. * Product.on('error', handleError);
  565. *
  566. * @param {RegExp|Function|Object} obj validator function, or hash describing options
  567. * @param {Function} [obj.validator] validator function. If the validator function returns `undefined` or a truthy value, validation succeeds. If it returns falsy (except `undefined`) or throws an error, validation fails.
  568. * @param {String|Function} [obj.message] optional error message. If function, should return the error message as a string
  569. * @param {Boolean} [obj.propsParameter=false] If true, Mongoose will pass the validator properties object (with the `validator` function, `message`, etc.) as the 2nd arg to the validator function. This is disabled by default because many validators [rely on positional args](https://github.com/chriso/validator.js#validators), so turning this on may cause unpredictable behavior in external validators.
  570. * @param {String|Function} [errorMsg] optional error message. If function, should return the error message as a string
  571. * @param {String} [type] optional validator type
  572. * @return {SchemaType} this
  573. * @api public
  574. */
  575. SchemaType.prototype.validate = function(obj, message, type) {
  576. if (typeof obj === 'function' || obj && utils.getFunctionName(obj.constructor) === 'RegExp') {
  577. let properties;
  578. if (message instanceof Object && !type) {
  579. properties = utils.clone(message);
  580. if (!properties.message) {
  581. properties.message = properties.msg;
  582. }
  583. properties.validator = obj;
  584. properties.type = properties.type || 'user defined';
  585. } else {
  586. if (!message) {
  587. message = MongooseError.messages.general.default;
  588. }
  589. if (!type) {
  590. type = 'user defined';
  591. }
  592. properties = {message: message, type: type, validator: obj};
  593. }
  594. if (properties.isAsync) {
  595. handleIsAsync();
  596. }
  597. this.validators.push(properties);
  598. return this;
  599. }
  600. let i;
  601. let length;
  602. let arg;
  603. for (i = 0, length = arguments.length; i < length; i++) {
  604. arg = arguments[i];
  605. if (!utils.isPOJO(arg)) {
  606. const msg = 'Invalid validator. Received (' + typeof arg + ') '
  607. + arg
  608. + '. See http://mongoosejs.com/docs/api.html#schematype_SchemaType-validate';
  609. throw new Error(msg);
  610. }
  611. this.validate(arg.validator, arg);
  612. }
  613. return this;
  614. };
  615. /*!
  616. * ignore
  617. */
  618. const handleIsAsync = util.deprecate(function handleIsAsync() {},
  619. 'Mongoose: the `isAsync` option for custom validators is deprecated. Make ' +
  620. 'your async validators return a promise instead: ' +
  621. 'https://mongoosejs.com/docs/validation.html#async-custom-validators');
  622. /**
  623. * Adds a required validator to this SchemaType. The validator gets added
  624. * to the front of this SchemaType's validators array using `unshift()`.
  625. *
  626. * ####Example:
  627. *
  628. * var s = new Schema({ born: { type: Date, required: true })
  629. *
  630. * // or with custom error message
  631. *
  632. * var s = new Schema({ born: { type: Date, required: '{PATH} is required!' })
  633. *
  634. * // or with a function
  635. *
  636. * var s = new Schema({
  637. * userId: ObjectId,
  638. * username: {
  639. * type: String,
  640. * required: function() { return this.userId != null; }
  641. * }
  642. * })
  643. *
  644. * // or with a function and a custom message
  645. * var s = new Schema({
  646. * userId: ObjectId,
  647. * username: {
  648. * type: String,
  649. * required: [
  650. * function() { return this.userId != null; },
  651. * 'username is required if id is specified'
  652. * ]
  653. * }
  654. * })
  655. *
  656. * // or through the path API
  657. *
  658. * Schema.path('name').required(true);
  659. *
  660. * // with custom error messaging
  661. *
  662. * Schema.path('name').required(true, 'grrr :( ');
  663. *
  664. * // or make a path conditionally required based on a function
  665. * var isOver18 = function() { return this.age >= 18; };
  666. * Schema.path('voterRegistrationId').required(isOver18);
  667. *
  668. * The required validator uses the SchemaType's `checkRequired` function to
  669. * determine whether a given value satisfies the required validator. By default,
  670. * a value satisfies the required validator if `val != null` (that is, if
  671. * the value is not null nor undefined). However, most built-in mongoose schema
  672. * types override the default `checkRequired` function:
  673. *
  674. * @param {Boolean|Function|Object} required enable/disable the validator, or function that returns required boolean, or options object
  675. * @param {Boolean|Function} [options.isRequired] enable/disable the validator, or function that returns required boolean
  676. * @param {Function} [options.ErrorConstructor] custom error constructor. The constructor receives 1 parameter, an object containing the validator properties.
  677. * @param {String} [message] optional custom error message
  678. * @return {SchemaType} this
  679. * @see Customized Error Messages #error_messages_MongooseError-messages
  680. * @see SchemaArray#checkRequired #schema_array_SchemaArray.checkRequired
  681. * @see SchemaBoolean#checkRequired #schema_boolean_SchemaBoolean-checkRequired
  682. * @see SchemaBuffer#checkRequired #schema_buffer_SchemaBuffer.schemaName
  683. * @see SchemaNumber#checkRequired #schema_number_SchemaNumber-min
  684. * @see SchemaObjectId#checkRequired #schema_objectid_ObjectId-auto
  685. * @see SchemaString#checkRequired #schema_string_SchemaString-checkRequired
  686. * @api public
  687. */
  688. SchemaType.prototype.required = function(required, message) {
  689. let customOptions = {};
  690. if (typeof required === 'object') {
  691. customOptions = required;
  692. message = customOptions.message || message;
  693. required = required.isRequired;
  694. }
  695. if (required === false) {
  696. this.validators = this.validators.filter(function(v) {
  697. return v.validator !== this.requiredValidator;
  698. }, this);
  699. this.isRequired = false;
  700. delete this.originalRequiredValue;
  701. return this;
  702. }
  703. const _this = this;
  704. this.isRequired = true;
  705. this.requiredValidator = function(v) {
  706. const cachedRequired = get(this, '$__.cachedRequired');
  707. // no validation when this path wasn't selected in the query.
  708. if (cachedRequired != null && !this.isSelected(_this.path) && !this.isModified(_this.path)) {
  709. return true;
  710. }
  711. // `$cachedRequired` gets set in `_evaluateRequiredFunctions()` so we
  712. // don't call required functions multiple times in one validate call
  713. // See gh-6801
  714. if (cachedRequired != null && _this.path in cachedRequired) {
  715. const res = cachedRequired[_this.path] ?
  716. _this.checkRequired(v, this) :
  717. true;
  718. delete cachedRequired[_this.path];
  719. return res;
  720. } else if (typeof required === 'function') {
  721. return required.apply(this) ? _this.checkRequired(v, this) : true;
  722. }
  723. return _this.checkRequired(v, this);
  724. };
  725. this.originalRequiredValue = required;
  726. if (typeof required === 'string') {
  727. message = required;
  728. required = undefined;
  729. }
  730. const msg = message || MongooseError.messages.general.required;
  731. this.validators.unshift(Object.assign({}, customOptions, {
  732. validator: this.requiredValidator,
  733. message: msg,
  734. type: 'required'
  735. }));
  736. return this;
  737. };
  738. /**
  739. * Set the model that this path refers to. This is the option that [populate](https://mongoosejs.com/docs/populate.html)
  740. * looks at to determine the foreign collection it should query.
  741. *
  742. * ####Example:
  743. * const userSchema = new Schema({ name: String });
  744. * const User = mongoose.model('User', userSchema);
  745. *
  746. * const postSchema = new Schema({ user: mongoose.ObjectId });
  747. * postSchema.path('user').ref('User'); // By model name
  748. * postSchema.path('user').ref(User); // Can pass the model as well
  749. *
  750. * // Or you can just declare the `ref` inline in your schema
  751. * const postSchema2 = new Schema({
  752. * user: { type: mongoose.ObjectId, ref: User }
  753. * });
  754. *
  755. * @param {String|Model|Function} ref either a model name, a [Model](https://mongoosejs.com/docs/models.html), or a function that returns a model name or model.
  756. * @return {SchemaType} this
  757. * @api public
  758. */
  759. SchemaType.prototype.ref = function(ref) {
  760. this.options.ref = ref;
  761. return this;
  762. };
  763. /**
  764. * Gets the default value
  765. *
  766. * @param {Object} scope the scope which callback are executed
  767. * @param {Boolean} init
  768. * @api private
  769. */
  770. SchemaType.prototype.getDefault = function(scope, init) {
  771. let ret = typeof this.defaultValue === 'function'
  772. ? this.defaultValue.call(scope)
  773. : this.defaultValue;
  774. if (ret !== null && ret !== undefined) {
  775. if (typeof ret === 'object' && (!this.options || !this.options.shared)) {
  776. ret = utils.clone(ret);
  777. }
  778. const casted = this.cast(ret, scope, init);
  779. if (casted && casted.$isSingleNested) {
  780. casted.$parent = scope;
  781. }
  782. return casted;
  783. }
  784. return ret;
  785. };
  786. /*!
  787. * Applies setters without casting
  788. *
  789. * @api private
  790. */
  791. SchemaType.prototype._applySetters = function(value, scope, init, priorVal) {
  792. let v = value;
  793. const setters = this.setters;
  794. let len = setters.length;
  795. const caster = this.caster;
  796. while (len--) {
  797. v = setters[len].call(scope, v, this);
  798. }
  799. if (Array.isArray(v) && caster && caster.setters) {
  800. const newVal = [];
  801. for (let i = 0; i < v.length; i++) {
  802. newVal.push(caster.applySetters(v[i], scope, init, priorVal));
  803. }
  804. v = newVal;
  805. }
  806. return v;
  807. };
  808. /**
  809. * Applies setters
  810. *
  811. * @param {Object} value
  812. * @param {Object} scope
  813. * @param {Boolean} init
  814. * @api private
  815. */
  816. SchemaType.prototype.applySetters = function(value, scope, init, priorVal, options) {
  817. let v = this._applySetters(value, scope, init, priorVal, options);
  818. if (v == null) {
  819. return v;
  820. }
  821. // do not cast until all setters are applied #665
  822. v = this.cast(v, scope, init, priorVal, options);
  823. return v;
  824. };
  825. /**
  826. * Applies getters to a value
  827. *
  828. * @param {Object} value
  829. * @param {Object} scope
  830. * @api private
  831. */
  832. SchemaType.prototype.applyGetters = function(value, scope) {
  833. let v = value;
  834. const getters = this.getters;
  835. const len = getters.length;
  836. if (len === 0) {
  837. return v;
  838. }
  839. for (let i = 0; i < len; ++i) {
  840. v = getters[i].call(scope, v, this);
  841. }
  842. return v;
  843. };
  844. /**
  845. * Sets default `select()` behavior for this path.
  846. *
  847. * Set to `true` if this path should always be included in the results, `false` if it should be excluded by default. This setting can be overridden at the query level.
  848. *
  849. * ####Example:
  850. *
  851. * T = db.model('T', new Schema({ x: { type: String, select: true }}));
  852. * T.find(..); // field x will always be selected ..
  853. * // .. unless overridden;
  854. * T.find().select('-x').exec(callback);
  855. *
  856. * @param {Boolean} val
  857. * @return {SchemaType} this
  858. * @api public
  859. */
  860. SchemaType.prototype.select = function select(val) {
  861. this.selected = !!val;
  862. return this;
  863. };
  864. /**
  865. * Performs a validation of `value` using the validators declared for this SchemaType.
  866. *
  867. * @param {any} value
  868. * @param {Function} callback
  869. * @param {Object} scope
  870. * @api private
  871. */
  872. SchemaType.prototype.doValidate = function(value, fn, scope, options) {
  873. let err = false;
  874. const path = this.path;
  875. // Avoid non-object `validators`
  876. const validators = this.validators.
  877. filter(v => v != null && typeof v === 'object');
  878. let count = validators.length;
  879. if (!count) {
  880. return fn(null);
  881. }
  882. const validate = function(ok, validatorProperties) {
  883. if (err) {
  884. return;
  885. }
  886. if (ok === undefined || ok) {
  887. if (--count <= 0) {
  888. immediate(function() {
  889. fn(null);
  890. });
  891. }
  892. } else {
  893. const ErrorConstructor = validatorProperties.ErrorConstructor || ValidatorError;
  894. err = new ErrorConstructor(validatorProperties);
  895. err[validatorErrorSymbol] = true;
  896. immediate(function() {
  897. fn(err);
  898. });
  899. }
  900. };
  901. const _this = this;
  902. validators.forEach(function(v) {
  903. if (err) {
  904. return;
  905. }
  906. const validator = v.validator;
  907. let ok;
  908. const validatorProperties = utils.clone(v);
  909. validatorProperties.path = options && options.path ? options.path : path;
  910. validatorProperties.value = value;
  911. if (validator instanceof RegExp) {
  912. validate(validator.test(value), validatorProperties);
  913. } else if (typeof validator === 'function') {
  914. if (value === undefined && validator !== _this.requiredValidator) {
  915. validate(true, validatorProperties);
  916. return;
  917. }
  918. if (validatorProperties.isAsync) {
  919. asyncValidate(validator, scope, value, validatorProperties, validate);
  920. } else {
  921. try {
  922. if (validatorProperties.propsParameter) {
  923. ok = validator.call(scope, value, validatorProperties);
  924. } else {
  925. ok = validator.call(scope, value);
  926. }
  927. } catch (error) {
  928. ok = false;
  929. validatorProperties.reason = error;
  930. if (error.message) {
  931. validatorProperties.message = error.message;
  932. }
  933. }
  934. if (ok != null && typeof ok.then === 'function') {
  935. ok.then(
  936. function(ok) { validate(ok, validatorProperties); },
  937. function(error) {
  938. validatorProperties.reason = error;
  939. ok = false;
  940. validate(ok, validatorProperties);
  941. });
  942. } else {
  943. validate(ok, validatorProperties);
  944. }
  945. }
  946. }
  947. });
  948. };
  949. /*!
  950. * Handle async validators
  951. */
  952. function asyncValidate(validator, scope, value, props, cb) {
  953. let called = false;
  954. const returnVal = validator.call(scope, value, function(ok, customMsg) {
  955. if (called) {
  956. return;
  957. }
  958. called = true;
  959. if (customMsg) {
  960. props.message = customMsg;
  961. }
  962. cb(ok, props);
  963. });
  964. if (typeof returnVal === 'boolean') {
  965. called = true;
  966. cb(returnVal, props);
  967. } else if (returnVal && typeof returnVal.then === 'function') {
  968. // Promise
  969. returnVal.then(
  970. function(ok) {
  971. if (called) {
  972. return;
  973. }
  974. called = true;
  975. cb(ok, props);
  976. },
  977. function(error) {
  978. if (called) {
  979. return;
  980. }
  981. called = true;
  982. props.reason = error;
  983. cb(false, props);
  984. });
  985. }
  986. }
  987. /**
  988. * Performs a validation of `value` using the validators declared for this SchemaType.
  989. *
  990. * ####Note:
  991. *
  992. * This method ignores the asynchronous validators.
  993. *
  994. * @param {any} value
  995. * @param {Object} scope
  996. * @return {MongooseError|undefined}
  997. * @api private
  998. */
  999. SchemaType.prototype.doValidateSync = function(value, scope, options) {
  1000. let err = null;
  1001. const path = this.path;
  1002. const count = this.validators.length;
  1003. if (!count) {
  1004. return null;
  1005. }
  1006. const validate = function(ok, validatorProperties) {
  1007. if (err) {
  1008. return;
  1009. }
  1010. if (ok !== undefined && !ok) {
  1011. const ErrorConstructor = validatorProperties.ErrorConstructor || ValidatorError;
  1012. err = new ErrorConstructor(validatorProperties);
  1013. err[validatorErrorSymbol] = true;
  1014. }
  1015. };
  1016. let validators = this.validators;
  1017. if (value === void 0) {
  1018. if (this.validators.length > 0 && this.validators[0].type === 'required') {
  1019. validators = [this.validators[0]];
  1020. } else {
  1021. return null;
  1022. }
  1023. }
  1024. validators.forEach(function(v) {
  1025. if (err) {
  1026. return;
  1027. }
  1028. if (v == null || typeof v !== 'object') {
  1029. return;
  1030. }
  1031. const validator = v.validator;
  1032. const validatorProperties = utils.clone(v);
  1033. validatorProperties.path = options && options.path ? options.path : path;
  1034. validatorProperties.value = value;
  1035. let ok;
  1036. // Skip any explicit async validators. Validators that return a promise
  1037. // will still run, but won't trigger any errors.
  1038. if (validator.isAsync) {
  1039. return;
  1040. }
  1041. if (validator instanceof RegExp) {
  1042. validate(validator.test(value), validatorProperties);
  1043. } else if (typeof validator === 'function') {
  1044. try {
  1045. if (validatorProperties.propsParameter) {
  1046. ok = validator.call(scope, value, validatorProperties);
  1047. } else {
  1048. ok = validator.call(scope, value);
  1049. }
  1050. } catch (error) {
  1051. ok = false;
  1052. validatorProperties.reason = error;
  1053. }
  1054. // Skip any validators that return a promise, we can't handle those
  1055. // synchronously
  1056. if (ok != null && typeof ok.then === 'function') {
  1057. return;
  1058. }
  1059. validate(ok, validatorProperties);
  1060. }
  1061. });
  1062. return err;
  1063. };
  1064. /**
  1065. * Determines if value is a valid Reference.
  1066. *
  1067. * @param {SchemaType} self
  1068. * @param {Object} value
  1069. * @param {Document} doc
  1070. * @param {Boolean} init
  1071. * @return {Boolean}
  1072. * @api private
  1073. */
  1074. SchemaType._isRef = function(self, value, doc, init) {
  1075. // fast path
  1076. let ref = init && self.options && (self.options.ref || self.options.refPath);
  1077. if (!ref && doc && doc.$__ != null) {
  1078. // checks for
  1079. // - this populated with adhoc model and no ref was set in schema OR
  1080. // - setting / pushing values after population
  1081. const path = doc.$__fullPath(self.path);
  1082. const owner = doc.ownerDocument ? doc.ownerDocument() : doc;
  1083. ref = owner.populated(path);
  1084. }
  1085. if (ref) {
  1086. if (value == null) {
  1087. return true;
  1088. }
  1089. if (!Buffer.isBuffer(value) && // buffers are objects too
  1090. value._bsontype !== 'Binary' // raw binary value from the db
  1091. && utils.isObject(value) // might have deselected _id in population query
  1092. ) {
  1093. return true;
  1094. }
  1095. }
  1096. return false;
  1097. };
  1098. /*!
  1099. * ignore
  1100. */
  1101. function handleSingle(val) {
  1102. return this.castForQuery(val);
  1103. }
  1104. /*!
  1105. * ignore
  1106. */
  1107. function handleArray(val) {
  1108. const _this = this;
  1109. if (!Array.isArray(val)) {
  1110. return [this.castForQuery(val)];
  1111. }
  1112. return val.map(function(m) {
  1113. return _this.castForQuery(m);
  1114. });
  1115. }
  1116. /*!
  1117. * Just like handleArray, except also allows `[]` because surprisingly
  1118. * `$in: [1, []]` works fine
  1119. */
  1120. function handle$in(val) {
  1121. const _this = this;
  1122. if (!Array.isArray(val)) {
  1123. return [this.castForQuery(val)];
  1124. }
  1125. return val.map(function(m) {
  1126. if (Array.isArray(m) && m.length === 0) {
  1127. return m;
  1128. }
  1129. return _this.castForQuery(m);
  1130. });
  1131. }
  1132. /*!
  1133. * ignore
  1134. */
  1135. SchemaType.prototype.$conditionalHandlers = {
  1136. $all: handleArray,
  1137. $eq: handleSingle,
  1138. $in: handle$in,
  1139. $ne: handleSingle,
  1140. $nin: handleArray,
  1141. $exists: $exists,
  1142. $type: $type
  1143. };
  1144. /*!
  1145. * Wraps `castForQuery` to handle context
  1146. */
  1147. SchemaType.prototype.castForQueryWrapper = function(params) {
  1148. this.$$context = params.context;
  1149. if ('$conditional' in params) {
  1150. return this.castForQuery(params.$conditional, params.val);
  1151. }
  1152. if (params.$skipQueryCastForUpdate) {
  1153. return this._castForQuery(params.val);
  1154. }
  1155. return this.castForQuery(params.val);
  1156. };
  1157. /**
  1158. * Cast the given value with the given optional query operator.
  1159. *
  1160. * @param {String} [$conditional] query operator, like `$eq` or `$in`
  1161. * @param {any} val
  1162. * @api private
  1163. */
  1164. SchemaType.prototype.castForQuery = function($conditional, val) {
  1165. let handler;
  1166. if (arguments.length === 2) {
  1167. handler = this.$conditionalHandlers[$conditional];
  1168. if (!handler) {
  1169. throw new Error('Can\'t use ' + $conditional);
  1170. }
  1171. return handler.call(this, val);
  1172. }
  1173. val = $conditional;
  1174. return this._castForQuery(val);
  1175. };
  1176. /*!
  1177. * Internal switch for runSetters
  1178. *
  1179. * @api private
  1180. */
  1181. SchemaType.prototype._castForQuery = function(val) {
  1182. return this.applySetters(val, this.$$context);
  1183. };
  1184. /**
  1185. * Override the function the required validator uses to check whether a value
  1186. * passes the `required` check. Override this on the individual SchemaType.
  1187. *
  1188. * ####Example:
  1189. *
  1190. * // Use this to allow empty strings to pass the `required` validator
  1191. * mongoose.Schema.Types.String.checkRequired(v => typeof v === 'string');
  1192. *
  1193. * @param {Function} fn
  1194. * @return {Function}
  1195. * @static
  1196. * @receiver SchemaType
  1197. * @function checkRequired
  1198. * @api public
  1199. */
  1200. SchemaType.checkRequired = function(fn) {
  1201. if (arguments.length > 0) {
  1202. this._checkRequired = fn;
  1203. }
  1204. return this._checkRequired;
  1205. };
  1206. /**
  1207. * Default check for if this path satisfies the `required` validator.
  1208. *
  1209. * @param {any} val
  1210. * @api private
  1211. */
  1212. SchemaType.prototype.checkRequired = function(val) {
  1213. return val != null;
  1214. };
  1215. /*!
  1216. * ignore
  1217. */
  1218. SchemaType.prototype.clone = function() {
  1219. const options = Object.assign({}, this.options, { validators: this.validators });
  1220. return new this.constructor(this.path, options, this.instance);
  1221. };
  1222. /*!
  1223. * Module exports.
  1224. */
  1225. module.exports = exports = SchemaType;
  1226. exports.CastError = CastError;
  1227. exports.ValidatorError = ValidatorError;