Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 202x 202x 202x 202x 202x 202x 202x 202x 202x 202x 202x 202x 201x 202x 202x 202x 202x 1x 1x 202x 202x 258x 258x 70x 70x 70x 6x 6x 64x 64x 202x 1x 1x 1x 1x 1x 432x 230x 202x 202x 202x 1x 1x 1x 1x 1x 1x 70x 70x 70x 70x 70x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 42x 42x 42x 1x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 17x 17x 17x 17x 8x 8x 8x 8x 9x 9x 9x 13x 13x 13x 13x 2x 11x 9x 9x 9x 9x 9x 1x 6x 6x 1x 66x 15x 15x 51x 66x 15x 15x 15x 15x 15x 15x 51x 1x 64x 64x 64x 64x 11x 64x 1x 3x 1x 64x 64x 3x 61x 61x 1x 37x 37x 37x 37x 37x 1x 1x 1x 1x 27x 27x 27x 27x 15x 27x 27x 14x 13x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 13x 13x 1x 1x 1x 1x 1x 1x 11x 11x 1x 1x 1x 1x 12x 1x 1x 2x 1x 1x 1x | 'use strict';
/*!
* Module dependencies.
*/
const MongooseError = require('./error/index');
const SchemaTypeOptions = require('./options/schemaTypeOptions');
const $exists = require('./schema/operators/exists');
const $type = require('./schema/operators/type');
const clone = require('./helpers/clone');
const handleImmutable = require('./helpers/schematype/handleImmutable');
const isAsyncFunction = require('./helpers/isAsyncFunction');
const isSimpleValidator = require('./helpers/isSimpleValidator');
const immediate = require('./helpers/immediate');
const schemaTypeSymbol = require('./helpers/symbols').schemaTypeSymbol;
const utils = require('./utils');
const validatorErrorSymbol = require('./helpers/symbols').validatorErrorSymbol;
const documentIsModified = require('./helpers/symbols').documentIsModified;
const populateModelSymbol = require('./helpers/symbols').populateModelSymbol;
const CastError = MongooseError.CastError;
const ValidatorError = MongooseError.ValidatorError;
const setOptionsForDefaults = { _skipMarkModified: true };
/**
* SchemaType constructor. Do **not** instantiate `SchemaType` directly.
* Mongoose converts your schema paths into SchemaTypes automatically.
*
* #### Example:
*
* const schema = new Schema({ name: String });
* schema.path('name') instanceof SchemaType; // true
*
* @param {String} path
* @param {SchemaTypeOptions} [options] See [SchemaTypeOptions docs](https://mongoosejs.com/docs/api/schematypeoptions.html)
* @param {String} [instance]
* @api public
*/
function SchemaType(path, options, instance) {
this[schemaTypeSymbol] = true;
this.path = path;
this.instance = instance;
this.validators = [];
this.getters = this.constructor.hasOwnProperty('getters') ?
this.constructor.getters.slice() :
[];
this.setters = this.constructor.hasOwnProperty('setters') ?
this.constructor.setters.slice() :
[];
this.splitPath();
options = options || {};
const defaultOptions = this.constructor.defaultOptions || {};
const defaultOptionsKeys = Object.keys(defaultOptions);
for (const option of defaultOptionsKeys) {
if (option === 'validate') {
this.validate(defaultOptions.validate);
} else if (defaultOptions.hasOwnProperty(option) && !Object.prototype.hasOwnProperty.call(options, option)) {
options[option] = defaultOptions[option];
}
}
if (options.select == null) {
delete options.select;
}
const Options = this.OptionsConstructor || SchemaTypeOptions;
this.options = new Options(options);
this._index = null;
if (utils.hasUserDefinedProperty(this.options, 'immutable')) {
this.$immutable = this.options.immutable;
handleImmutable(this);
}
const keys = Object.keys(this.options);
for (const prop of keys) {
Iif (prop === 'cast') {
if (Array.isArray(this.options[prop])) {
this.castFunction.apply(this, this.options[prop]);
} else {
this.castFunction(this.options[prop]);
}
continue;
}
if (utils.hasUserDefinedProperty(this.options, prop) && typeof this[prop] === 'function') {
// { unique: true, index: true }
Iif (prop === 'index' && this._index) {
if (options.index === false) {
const index = this._index;
if (typeof index === 'object' && index != null) {
if (index.unique) {
throw new Error('Path "' + this.path + '" may not have `index` ' +
'set to false and `unique` set to true');
}
if (index.sparse) {
throw new Error('Path "' + this.path + '" may not have `index` ' +
'set to false and `sparse` set to true');
}
}
this._index = false;
}
continue;
}
const val = options[prop];
// Special case so we don't screw up array defaults, see gh-5780
if (prop === 'default') {
this.default(val);
continue;
}
const opts = Array.isArray(val) ? val : [val];
this[prop].apply(this, opts);
}
}
Object.defineProperty(this, '$$context', {
enumerable: false,
configurable: false,
writable: true,
value: null
});
}
/**
* The class that Mongoose uses internally to instantiate this SchemaType's `options` property.
* @memberOf SchemaType
* @instance
* @api private
*/
SchemaType.prototype.OptionsConstructor = SchemaTypeOptions;
/**
* The path to this SchemaType in a Schema.
*
* #### Example:
*
* const schema = new Schema({ name: String });
* schema.path('name').path; // 'name'
*
* @property path
* @api public
* @memberOf SchemaType
*/
SchemaType.prototype.path;
/**
* The validators that Mongoose should run to validate properties at this SchemaType's path.
*
* #### Example:
*
* const schema = new Schema({ name: { type: String, required: true } });
* schema.path('name').validators.length; // 1, the `required` validator
*
* @property validators
* @api public
* @memberOf SchemaType
*/
SchemaType.prototype.validators;
/**
* True if this SchemaType has a required validator. False otherwise.
*
* #### Example:
*
* const schema = new Schema({ name: { type: String, required: true } });
* schema.path('name').isRequired; // true
*
* schema.path('name').required(false);
* schema.path('name').isRequired; // false
*
* @property isRequired
* @api public
* @memberOf SchemaType
*/
SchemaType.prototype.isRequired;
/**
* Split the current dottet path into segments
*
* @return {String[]|undefined}
* @api private
*/
SchemaType.prototype.splitPath = function() {
if (this._presplitPath != null) {
return this._presplitPath;
}
Iif (this.path == null) {
return undefined;
}
this._presplitPath = this.path.indexOf('.') === -1 ? [this.path] : this.path.split('.');
return this._presplitPath;
};
/**
* Get/set the function used to cast arbitrary values to this type.
*
* #### Example:
*
* // Disallow `null` for numbers, and don't try to cast any values to
* // numbers, so even strings like '123' will cause a CastError.
* mongoose.Number.cast(function(v) {
* assert.ok(v === undefined || typeof v === 'number');
* return v;
* });
*
* @param {Function|false} caster Function that casts arbitrary values to this type, or throws an error if casting failed
* @return {Function}
* @static
* @memberOf SchemaType
* @function cast
* @api public
*/
SchemaType.cast = function cast(caster) {
if (arguments.length === 0) {
return this._cast;
}
if (caster === false) {
caster = v => v;
}
this._cast = caster;
return this._cast;
};
/**
* Get/set the function used to cast arbitrary values to this particular schematype instance.
* Overrides `SchemaType.cast()`.
*
* #### Example:
*
* // Disallow `null` for numbers, and don't try to cast any values to
* // numbers, so even strings like '123' will cause a CastError.
* const number = new mongoose.Number('mypath', {});
* number.cast(function(v) {
* assert.ok(v === undefined || typeof v === 'number');
* return v;
* });
*
* @param {Function|false} caster Function that casts arbitrary values to this type, or throws an error if casting failed
* @return {Function}
* @memberOf SchemaType
* @api public
*/
SchemaType.prototype.castFunction = function castFunction(caster, message) {
if (arguments.length === 0) {
return this._castFunction;
}
if (caster === false) {
caster = this.constructor._defaultCaster || (v => v);
}
if (typeof caster === 'string') {
this._castErrorMessage = caster;
return this._castFunction;
}
if (caster != null) {
this._castFunction = caster;
}
if (message != null) {
this._castErrorMessage = message;
}
return this._castFunction;
};
/**
* The function that Mongoose calls to cast arbitrary values to this SchemaType.
*
* @param {Object} value value to cast
* @param {Document} doc document that triggers the casting
* @param {Boolean} init
* @api public
*/
SchemaType.prototype.cast = function cast() {
throw new Error('Base SchemaType class does not implement a `cast()` function');
};
/**
* Sets a default option for this schema type.
*
* #### Example:
*
* // Make all strings be trimmed by default
* mongoose.SchemaTypes.String.set('trim', true);
*
* @param {String} option The name of the option you'd like to set (e.g. trim, lowercase, etc...)
* @param {Any} value The value of the option you'd like to set.
* @return {void}
* @static
* @memberOf SchemaType
* @function set
* @api public
*/
SchemaType.set = function set(option, value) {
if (!this.hasOwnProperty('defaultOptions')) {
this.defaultOptions = Object.assign({}, this.defaultOptions);
}
this.defaultOptions[option] = value;
};
/**
* Attaches a getter for all instances of this schema type.
*
* #### Example:
*
* // Make all numbers round down
* mongoose.Number.get(function(v) { return Math.floor(v); });
*
* @param {Function} getter
* @return {this}
* @static
* @memberOf SchemaType
* @function get
* @api public
*/
SchemaType.get = function(getter) {
this.getters = this.hasOwnProperty('getters') ? this.getters : [];
this.getters.push(getter);
};
/**
* Sets a default value for this SchemaType.
*
* #### Example:
*
* const schema = new Schema({ n: { type: Number, default: 10 })
* const M = db.model('M', schema)
* const m = new M;
* console.log(m.n) // 10
*
* 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.
*
* #### Example:
*
* // values are cast:
* const schema = new Schema({ aNumber: { type: Number, default: 4.815162342 }})
* const M = db.model('M', schema)
* const m = new M;
* console.log(m.aNumber) // 4.815162342
*
* // default unique objects for Mixed types:
* const schema = new Schema({ mixed: Schema.Types.Mixed });
* schema.path('mixed').default(function () {
* return {};
* });
*
* // if we don't use a function to return object literals for Mixed defaults,
* // each document will receive a reference to the same object literal creating
* // a "shared" object instance:
* const schema = new Schema({ mixed: Schema.Types.Mixed });
* schema.path('mixed').default({});
* const M = db.model('M', schema);
* const m1 = new M;
* m1.mixed.added = 1;
* console.log(m1.mixed); // { added: 1 }
* const m2 = new M;
* console.log(m2.mixed); // { added: 1 }
*
* @param {Function|any} val The default value to set
* @return {Any|undefined} Returns the set default value.
* @api public
*/
SchemaType.prototype.default = function(val) {
Eif (arguments.length === 1) {
Iif (val === void 0) {
this.defaultValue = void 0;
return void 0;
}
Iif (val != null && val.instanceOfSchema) {
throw new MongooseError('Cannot set default value of path `' + this.path +
'` to a mongoose Schema instance.');
}
this.defaultValue = val;
return this.defaultValue;
} else if (arguments.length > 1) {
this.defaultValue = [...arguments];
}
return this.defaultValue;
};
/**
* Declares the index options for this schematype.
*
* #### Example:
*
* const s = new Schema({ name: { type: String, index: true })
* const s = new Schema({ name: { type: String, index: -1 })
* const s = new Schema({ loc: { type: [Number], index: 'hashed' })
* const s = new Schema({ loc: { type: [Number], index: '2d', sparse: true })
* const s = new Schema({ loc: { type: [Number], index: { type: '2dsphere', sparse: true }})
* const s = new Schema({ date: { type: Date, index: { unique: true, expires: '1d' }})
* s.path('my.path').index(true);
* s.path('my.date').index({ expires: 60 });
* s.path('my.path').index({ unique: true, sparse: true });
*
* #### Note:
*
* _Indexes are created [in the background](https://www.mongodb.com/docs/manual/core/index-creation/#index-creation-background)
* by default. If `background` is set to `false`, MongoDB will not execute any
* read/write operations you send until the index build.
* Specify `background: false` to override Mongoose's default._
*
* @param {Object|Boolean|String|Number} options
* @return {SchemaType} this
* @api public
*/
SchemaType.prototype.index = function(options) {
this._index = options;
utils.expires(this._index);
return this;
};
/**
* Declares an unique index.
*
* #### Example:
*
* const s = new Schema({ name: { type: String, unique: true } });
* s.path('name').index({ unique: true });
*
* _NOTE: violating the constraint returns an `E11000` error from MongoDB when saving, not a Mongoose validation error._
*
* You can optionally specify an error message to replace MongoDB's default `E11000 duplicate key error` message.
* The following will throw a "Email must be unique" error if `save()`, `updateOne()`, `updateMany()`, `replaceOne()`,
* `findOneAndUpdate()`, or `findOneAndReplace()` throws a duplicate key error:
*
* ```javascript
* new Schema({
* email: {
* type: String,
* unique: [true, 'Email must be unique']
* }
* });
* ```
*
* Note that the above syntax does **not** work for `bulkWrite()` or `insertMany()`. `bulkWrite()` and `insertMany()`
* will still throw MongoDB's default `E11000 duplicate key error` message.
*
* @param {Boolean} value
* @param {String} [message]
* @return {SchemaType} this
* @api public
*/
SchemaType.prototype.unique = function unique(value, message) {
if (this._index === false) {
if (!value) {
return;
}
throw new Error('Path "' + this.path + '" may not have `index` set to ' +
'false and `unique` set to true');
}
if (!this.options.hasOwnProperty('index') && value === false) {
return this;
}
if (this._index == null || this._index === true) {
this._index = {};
} else if (typeof this._index === 'string') {
this._index = { type: this._index };
}
this._index.unique = !!value;
if (typeof message === 'string') {
this._duplicateKeyErrorMessage = message;
}
return this;
};
/**
* Declares a full text index.
*
* ### Example:
*
* const s = new Schema({ name : { type: String, text : true } })
* s.path('name').index({ text : true });
*
* @param {Boolean} bool
* @return {SchemaType} this
* @api public
*/
SchemaType.prototype.text = function(bool) {
if (this._index === false) {
if (!bool) {
return this;
}
throw new Error('Path "' + this.path + '" may not have `index` set to ' +
'false and `text` set to true');
}
if (!this.options.hasOwnProperty('index') && bool === false) {
return this;
}
if (this._index === null || this._index === undefined ||
typeof this._index === 'boolean') {
this._index = {};
} else if (typeof this._index === 'string') {
this._index = { type: this._index };
}
this._index.text = bool;
return this;
};
/**
* Declares a sparse index.
*
* #### Example:
*
* const s = new Schema({ name: { type: String, sparse: true } });
* s.path('name').index({ sparse: true });
*
* @param {Boolean} bool
* @return {SchemaType} this
* @api public
*/
SchemaType.prototype.sparse = function(bool) {
if (this._index === false) {
if (!bool) {
return this;
}
throw new Error('Path "' + this.path + '" may not have `index` set to ' +
'false and `sparse` set to true');
}
if (!this.options.hasOwnProperty('index') && bool === false) {
return this;
}
if (this._index == null || typeof this._index === 'boolean') {
this._index = {};
} else if (typeof this._index === 'string') {
this._index = { type: this._index };
}
this._index.sparse = bool;
return this;
};
/**
* Defines this path as immutable. Mongoose prevents you from changing
* immutable paths unless the parent document has [`isNew: true`](https://mongoosejs.com/docs/api/document.html#Document.prototype.isNew()).
*
* #### Example:
*
* const schema = new Schema({
* name: { type: String, immutable: true },
* age: Number
* });
* const Model = mongoose.model('Test', schema);
*
* await Model.create({ name: 'test' });
* const doc = await Model.findOne();
*
* doc.isNew; // false
* doc.name = 'new name';
* doc.name; // 'test', because `name` is immutable
*
* Mongoose also prevents changing immutable properties using `updateOne()`
* and `updateMany()` based on [strict mode](https://mongoosejs.com/docs/guide.html#strict).
*
* #### Example:
*
* // Mongoose will strip out the `name` update, because `name` is immutable
* Model.updateOne({}, { $set: { name: 'test2' }, $inc: { age: 1 } });
*
* // If `strict` is set to 'throw', Mongoose will throw an error if you
* // update `name`
* const err = await Model.updateOne({}, { name: 'test2' }, { strict: 'throw' }).
* then(() => null, err => err);
* err.name; // StrictModeError
*
* // If `strict` is `false`, Mongoose allows updating `name` even though
* // the property is immutable.
* Model.updateOne({}, { name: 'test2' }, { strict: false });
*
* @param {Boolean} bool
* @return {SchemaType} this
* @see isNew https://mongoosejs.com/docs/api/document.html#Document.prototype.isNew()
* @api public
*/
SchemaType.prototype.immutable = function(bool) {
this.$immutable = bool;
handleImmutable(this);
return this;
};
/**
* Defines a custom function for transforming this path when converting a document to JSON.
*
* Mongoose calls this function with one parameter: the current `value` of the path. Mongoose
* then uses the return value in the JSON output.
*
* #### Example:
*
* const schema = new Schema({
* date: { type: Date, transform: v => v.getFullYear() }
* });
* const Model = mongoose.model('Test', schema);
*
* await Model.create({ date: new Date('2016-06-01') });
* const doc = await Model.findOne();
*
* doc.date instanceof Date; // true
*
* doc.toJSON().date; // 2016 as a number
* JSON.stringify(doc); // '{"_id":...,"date":2016}'
*
* @param {Function} fn
* @return {SchemaType} this
* @api public
*/
SchemaType.prototype.transform = function(fn) {
this.options.transform = fn;
return this;
};
/**
* Adds a setter to this schematype.
*
* #### Example:
*
* function capitalize (val) {
* if (typeof val !== 'string') val = '';
* return val.charAt(0).toUpperCase() + val.substring(1);
* }
*
* // defining within the schema
* const s = new Schema({ name: { type: String, set: capitalize }});
*
* // or with the SchemaType
* const s = new Schema({ name: String })
* s.path('name').set(capitalize);
*
* Setters allow you to transform the data before it gets to the raw mongodb
* document or query.
*
* Suppose you are implementing user registration for a website. Users provide
* an email and password, which gets saved to mongodb. The email is a string
* that you will want to normalize to lower case, in order to avoid one email
* 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.
*
* You can set up email lower case normalization easily via a Mongoose setter.
*
* function toLower(v) {
* return v.toLowerCase();
* }
*
* const UserSchema = new Schema({
* email: { type: String, set: toLower }
* });
*
* const User = db.model('User', UserSchema);
*
* const user = new User({email: 'AVENUE@Q.COM'});
* console.log(user.email); // 'avenue@q.com'
*
* // or
* const user = new User();
* user.email = 'Avenue@Q.com';
* console.log(user.email); // 'avenue@q.com'
* User.updateOne({ _id: _id }, { $set: { email: 'AVENUE@Q.COM' } }); // update to 'avenue@q.com'
*
* As you can see above, setters allow you to transform the data before it
* stored in MongoDB, or before executing a query.
*
* _NOTE: we could have also just used the built-in `lowercase: true` SchemaType option instead of defining our own function._
*
* new Schema({ email: { type: String, lowercase: true }})
*
* 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.
*
* function inspector (val, priorValue, schematype) {
* if (schematype.options.required) {
* return schematype.path + ' is required';
* } else {
* return val;
* }
* }
*
* const VirusSchema = new Schema({
* name: { type: String, required: true, set: inspector },
* taxonomy: { type: String, set: inspector }
* })
*
* const Virus = db.model('Virus', VirusSchema);
* const v = new Virus({ name: 'Parvoviridae', taxonomy: 'Parvovirinae' });
*
* console.log(v.name); // name is required
* console.log(v.taxonomy); // Parvovirinae
*
* You can also use setters to modify other properties on the document. If
* you're setting a property `name` on a document, the setter will run with
* `this` as the document. Be careful, in mongoose 5 setters will also run
* when querying by `name` with `this` as the query.
*
* const nameSchema = new Schema({ name: String, keywords: [String] });
* nameSchema.path('name').set(function(v) {
* // Need to check if `this` is a document, because in mongoose 5
* // setters will also run on queries, in which case `this` will be a
* // mongoose query object.
* if (this instanceof Document && v != null) {
* this.keywords = v.split(' ');
* }
* return v;
* });
*
* @param {Function} fn
* @return {SchemaType} this
* @api public
*/
SchemaType.prototype.set = function(fn) {
Iif (typeof fn !== 'function') {
throw new TypeError('A setter must be a function.');
}
this.setters.push(fn);
return this;
};
/**
* Adds a getter to this schematype.
*
* #### Example:
*
* function dob (val) {
* if (!val) return val;
* return (val.getMonth() + 1) + "/" + val.getDate() + "/" + val.getFullYear();
* }
*
* // defining within the schema
* const s = new Schema({ born: { type: Date, get: dob })
*
* // or by retreiving its SchemaType
* const s = new Schema({ born: Date })
* s.path('born').get(dob)
*
* Getters allow you to transform the representation of the data as it travels from the raw mongodb document to the value that you see.
*
* 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:
*
* function obfuscate (cc) {
* return '****-****-****-' + cc.slice(cc.length-4, cc.length);
* }
*
* const AccountSchema = new Schema({
* creditCardNumber: { type: String, get: obfuscate }
* });
*
* const Account = db.model('Account', AccountSchema);
*
* Account.findById(id, function (err, found) {
* console.log(found.creditCardNumber); // '****-****-****-1234'
* });
*
* 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.
*
* function inspector (val, priorValue, schematype) {
* if (schematype.options.required) {
* return schematype.path + ' is required';
* } else {
* return schematype.path + ' is not';
* }
* }
*
* const VirusSchema = new Schema({
* name: { type: String, required: true, get: inspector },
* taxonomy: { type: String, get: inspector }
* })
*
* const Virus = db.model('Virus', VirusSchema);
*
* Virus.findById(id, function (err, virus) {
* console.log(virus.name); // name is required
* console.log(virus.taxonomy); // taxonomy is not
* })
*
* @param {Function} fn
* @return {SchemaType} this
* @api public
*/
SchemaType.prototype.get = function(fn) {
Iif (typeof fn !== 'function') {
throw new TypeError('A getter must be a function.');
}
this.getters.push(fn);
return this;
};
/**
* Adds multiple validators for this document path.
* Calls `validate()` for every element in validators.
*
* @param {Array<RegExp|Function|Object>} validators
* @returns this
*/
SchemaType.prototype.validateAll = function(validators) {
for (let i = 0; i < validators.length; i++) {
this.validate(validators[i]);
}
return this;
};
/**
* Adds validator(s) for this document path.
*
* Validators always receive the value to validate as their first argument and
* must return `Boolean`. Returning `false` or throwing an error means
* validation failed.
*
* The error message argument is optional. If not passed, the [default generic error message template](https://mongoosejs.com/docs/api/error.html#Error.messages) will be used.
*
* #### Example:
*
* // make sure every value is equal to "something"
* function validator (val) {
* return val === 'something';
* }
* new Schema({ name: { type: String, validate: validator }});
*
* // with a custom error message
*
* const custom = [validator, 'Uh oh, {PATH} does not equal "something".']
* new Schema({ name: { type: String, validate: custom }});
*
* // adding many validators at a time
*
* const many = [
* { validator: validator, message: 'uh oh' }
* , { validator: anotherValidator, message: 'failed' }
* ]
* new Schema({ name: { type: String, validate: many }});
*
* // or utilizing SchemaType methods directly:
*
* const schema = new Schema({ name: 'string' });
* schema.path('name').validate(validator, 'validation of `{PATH}` failed with value `{VALUE}`');
*
* #### Error message templates:
*
* Below is a list of supported template keywords:
*
* - PATH: The schema path where the error is being triggered.
* - VALUE: The value assigned to the PATH that is triggering the error.
* - KIND: The validation property that triggered the error i.e. required.
* - REASON: The error object that caused this error if there was one.
*
* If Mongoose's built-in error message templating isn't enough, Mongoose
* supports setting the `message` property to a function.
*
* schema.path('name').validate({
* validator: function(v) { return v.length > 5; },
* // `errors['name']` will be "name must have length 5, got 'foo'"
* message: function(props) {
* return `${props.path} must have length 5, got '${props.value}'`;
* }
* });
*
* To bypass Mongoose's error messages and just copy the error message that
* the validator throws, do this:
*
* schema.path('name').validate({
* validator: function() { throw new Error('Oops!'); },
* // `errors['name'].message` will be "Oops!"
* message: function(props) { return props.reason.message; }
* });
*
* #### Asynchronous validation:
*
* Mongoose supports validators that return a promise. A validator that returns
* a promise is called an _async validator_. Async validators run in
* parallel, and `validate()` will wait until all async validators have settled.
*
* schema.path('name').validate({
* validator: function (value) {
* return new Promise(function (resolve, reject) {
* resolve(false); // validation failed
* });
* }
* });
*
* You might use asynchronous validators to retreive other documents from the database to validate against or to meet other I/O bound validation needs.
*
* Validation occurs `pre('save')` or whenever you manually execute [document#validate](https://mongoosejs.com/docs/api/document.html#Document.prototype.validate()).
*
* 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](https://mongoosejs.com/docs/api/connection.html#Connection()), passing the validation error object along.
*
* const conn = mongoose.createConnection(..);
* conn.on('error', handleError);
*
* const Product = conn.model('Product', yourSchema);
* const dvd = new Product(..);
* dvd.save(); // emits error on the `conn` above
*
* If you want to handle these errors at the Model level, add an `error`
* listener to your Model as shown below.
*
* // registering an error listener on the Model lets us handle errors more locally
* Product.on('error', handleError);
*
* @param {RegExp|Function|Object} obj validator function, or hash describing options
* @param {Function} [obj.validator] validator function. If the validator function returns `undefined` or a truthy value, validation succeeds. If it returns [falsy](https://masteringjs.io/tutorials/fundamentals/falsy) (except `undefined`) or throws an error, validation fails.
* @param {String|Function} [obj.message] optional error message. If function, should return the error message as a string
* @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.
* @param {String|Function} [errorMsg] optional error message. If function, should return the error message as a string
* @param {String} [type] optional validator type
* @return {SchemaType} this
* @api public
*/
SchemaType.prototype.validate = function(obj, message, type) {
Eif (typeof obj === 'function' || obj && utils.getFunctionName(obj.constructor) === 'RegExp') {
let properties;
Iif (typeof message === 'function') {
properties = { validator: obj, message: message };
properties.type = type || 'user defined';
} else Iif (message instanceof Object && !type) {
properties = isSimpleValidator(message) ? Object.assign({}, message) : clone(message);
if (!properties.message) {
properties.message = properties.msg;
}
properties.validator = obj;
properties.type = properties.type || 'user defined';
} else {
Iif (message == null) {
message = MongooseError.messages.general.default;
}
Eif (!type) {
type = 'user defined';
}
properties = { message: message, type: type, validator: obj };
}
this.validators.push(properties);
return this;
}
let i;
let length;
let arg;
for (i = 0, length = arguments.length; i < length; i++) {
arg = arguments[i];
if (!utils.isPOJO(arg)) {
const msg = 'Invalid validator. Received (' + typeof arg + ') '
+ arg
+ '. See https://mongoosejs.com/docs/api/schematype.html#SchemaType.prototype.validate()';
throw new Error(msg);
}
this.validate(arg.validator, arg);
}
return this;
};
/**
* Adds a required validator to this SchemaType. The validator gets added
* to the front of this SchemaType's validators array using `unshift()`.
*
* #### Example:
*
* const s = new Schema({ born: { type: Date, required: true })
*
* // or with custom error message
*
* const s = new Schema({ born: { type: Date, required: '{PATH} is required!' })
*
* // or with a function
*
* const s = new Schema({
* userId: ObjectId,
* username: {
* type: String,
* required: function() { return this.userId != null; }
* }
* })
*
* // or with a function and a custom message
* const s = new Schema({
* userId: ObjectId,
* username: {
* type: String,
* required: [
* function() { return this.userId != null; },
* 'username is required if id is specified'
* ]
* }
* })
*
* // or through the path API
*
* s.path('name').required(true);
*
* // with custom error messaging
*
* s.path('name').required(true, 'grrr :( ');
*
* // or make a path conditionally required based on a function
* const isOver18 = function() { return this.age >= 18; };
* s.path('voterRegistrationId').required(isOver18);
*
* The required validator uses the SchemaType's `checkRequired` function to
* determine whether a given value satisfies the required validator. By default,
* a value satisfies the required validator if `val != null` (that is, if
* the value is not null nor undefined). However, most built-in mongoose schema
* types override the default `checkRequired` function:
*
* @param {Boolean|Function|Object} required enable/disable the validator, or function that returns required boolean, or options object
* @param {Boolean|Function} [options.isRequired] enable/disable the validator, or function that returns required boolean
* @param {Function} [options.ErrorConstructor] custom error constructor. The constructor receives 1 parameter, an object containing the validator properties.
* @param {String} [message] optional custom error message
* @return {SchemaType} this
* @see Customized Error Messages https://mongoosejs.com/docs/api/error.html#Error.messages
* @see SchemaArray#checkRequired https://mongoosejs.com/docs/api/schemaarray.html#SchemaArray.prototype.checkRequired()
* @see SchemaBoolean#checkRequired https://mongoosejs.com/docs/api/schemaboolean.html#SchemaBoolean.prototype.checkRequired()
* @see SchemaBuffer#checkRequired https://mongoosejs.com/docs/api/schemabuffer.html#SchemaBuffer.prototype.checkRequired()
* @see SchemaNumber#checkRequired https://mongoosejs.com/docs/api/schemanumber.html#SchemaNumber.prototype.checkRequired()
* @see SchemaObjectId#checkRequired https://mongoosejs.com/docs/api/schemaobjectid.html#ObjectId.prototype.checkRequired()
* @see SchemaString#checkRequired https://mongoosejs.com/docs/api/schemastring.html#SchemaString.prototype.checkRequired()
* @api public
*/
SchemaType.prototype.required = function(required, message) {
let customOptions = {};
Iif (arguments.length > 0 && required == null) {
this.validators = this.validators.filter(function(v) {
return v.validator !== this.requiredValidator;
}, this);
this.isRequired = false;
delete this.originalRequiredValue;
return this;
}
Iif (typeof required === 'object') {
customOptions = required;
message = customOptions.message || message;
required = required.isRequired;
}
if (required === false) {
this.validators = this.validators.filter(function(v) {
return v.validator !== this.requiredValidator;
}, this);
this.isRequired = false;
delete this.originalRequiredValue;
return this;
}
const _this = this;
this.isRequired = true;
this.requiredValidator = function(v) {
const cachedRequired = this && this.$__ && this.$__.cachedRequired;
// no validation when this path wasn't selected in the query.
Iif (cachedRequired != null && !this.$__isSelected(_this.path) && !this[documentIsModified](_this.path)) {
return true;
}
// `$cachedRequired` gets set in `_evaluateRequiredFunctions()` so we
// don't call required functions multiple times in one validate call
// See gh-6801
Iif (cachedRequired != null && _this.path in cachedRequired) {
const res = cachedRequired[_this.path] ?
_this.checkRequired(v, this) :
true;
delete cachedRequired[_this.path];
return res;
} else if (typeof required === 'function') {
return required.apply(this) ? _this.checkRequired(v, this) : true;
}
return _this.checkRequired(v, this);
};
this.originalRequiredValue = required;
Iif (typeof required === 'string') {
message = required;
required = undefined;
}
const msg = message || MongooseError.messages.general.required;
this.validators.unshift(Object.assign({}, customOptions, {
validator: this.requiredValidator,
message: msg,
type: 'required'
}));
return this;
};
/**
* Set the model that this path refers to. This is the option that [populate](https://mongoosejs.com/docs/populate.html)
* looks at to determine the foreign collection it should query.
*
* #### Example:
*
* const userSchema = new Schema({ name: String });
* const User = mongoose.model('User', userSchema);
*
* const postSchema = new Schema({ user: mongoose.ObjectId });
* postSchema.path('user').ref('User'); // Can set ref to a model name
* postSchema.path('user').ref(User); // Or a model class
* postSchema.path('user').ref(() => 'User'); // Or a function that returns the model name
* postSchema.path('user').ref(() => User); // Or a function that returns the model class
*
* // Or you can just declare the `ref` inline in your schema
* const postSchema2 = new Schema({
* user: { type: mongoose.ObjectId, ref: User }
* });
*
* @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.
* @return {SchemaType} this
* @api public
*/
SchemaType.prototype.ref = function(ref) {
this.options.ref = ref;
return this;
};
/**
* Gets the default value
*
* @param {Object} scope the scope which callback are executed
* @param {Boolean} init
* @return {Any} The Stored default value.
* @api private
*/
SchemaType.prototype.getDefault = function(scope, init, options) {
let ret;
if (typeof this.defaultValue === 'function') {
Iif (
this.defaultValue === Date.now ||
this.defaultValue === Array ||
this.defaultValue.name.toLowerCase() === 'objectid'
) {
ret = this.defaultValue.call(scope);
} else {
ret = this.defaultValue.call(scope, scope);
}
} else {
ret = this.defaultValue;
}
if (ret !== null && ret !== undefined) {
Eif (typeof ret === 'object' && (!this.options || !this.options.shared)) {
ret = clone(ret);
}
Iif (options && options.skipCast) {
return this._applySetters(ret, scope);
}
const casted = this.applySetters(ret, scope, init, undefined, setOptionsForDefaults);
Iif (casted && !Array.isArray(casted) && casted.$isSingleNested) {
casted.$__parent = scope;
}
return casted;
}
return ret;
};
/**
* Applies setters without casting
*
* @param {Any} value
* @param {Any} scope
* @param {Boolean} init
* @param {Any} priorVal
* @param {Object} [options]
* @instance
* @api private
*/
SchemaType.prototype._applySetters = function(value, scope, init, priorVal, options) {
let v = value;
Iif (init) {
return v;
}
const setters = this.setters;
for (let i = setters.length - 1; i >= 0; i--) {
v = setters[i].call(scope, v, priorVal, this, options);
}
return v;
};
/*!
* ignore
*/
SchemaType.prototype._castNullish = function _castNullish(v) {
return v;
};
/**
* Applies setters
*
* @param {Object} value
* @param {Object} scope
* @param {Boolean} init
* @return {Any}
* @api private
*/
SchemaType.prototype.applySetters = function(value, scope, init, priorVal, options) {
let v = this._applySetters(value, scope, init, priorVal, options);
if (v == null) {
return this._castNullish(v);
}
// do not cast until all setters are applied #665
v = this.cast(v, scope, init, priorVal, options);
return v;
};
/**
* Applies getters to a value
*
* @param {Object} value
* @param {Object} scope
* @return {Any}
* @api private
*/
SchemaType.prototype.applyGetters = function(value, scope) {
let v = value;
const getters = this.getters;
const len = getters.length;
Eif (len === 0) {
return v;
}
for (let i = 0; i < len; ++i) {
v = getters[i].call(scope, v, this);
}
return v;
};
/**
* Sets default `select()` behavior for this path.
*
* 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.
*
* #### Example:
*
* T = db.model('T', new Schema({ x: { type: String, select: true }}));
* T.find(..); // field x will always be selected ..
* // .. unless overridden;
* T.find().select('-x').exec(callback);
*
* @param {Boolean} val
* @return {SchemaType} this
* @api public
*/
SchemaType.prototype.select = function select(val) {
this.selected = !!val;
return this;
};
/**
* Performs a validation of `value` using the validators declared for this SchemaType.
*
* @param {Any} value
* @param {Function} callback
* @param {Object} scope
* @param {Object} [options]
* @param {String} [options.path]
* @return {Any} If no validators, returns the output from calling `fn`, otherwise no return
* @api public
*/
SchemaType.prototype.doValidate = function(value, fn, scope, options) {
let err = false;
const path = this.path;
Iif (typeof fn !== 'function') {
throw new TypeError(`Must pass callback function to doValidate(), got ${typeof fn}`);
}
// Avoid non-object `validators`
const validators = this.validators.
filter(v => typeof v === 'object' && v !== null);
let count = validators.length;
if (!count) {
return fn(null);
}
for (let i = 0, len = validators.length; i < len; ++i) {
Iif (err) {
break;
}
const v = validators[i];
const validator = v.validator;
let ok;
const validatorProperties = isSimpleValidator(v) ? Object.assign({}, v) : clone(v);
validatorProperties.path = options && options.path ? options.path : path;
validatorProperties.fullPath = this.$fullPath;
validatorProperties.value = value;
Iif (validator instanceof RegExp) {
validate(validator.test(value), validatorProperties, scope);
continue;
}
Iif (typeof validator !== 'function') {
continue;
}
Iif (value === undefined && validator !== this.requiredValidator) {
validate(true, validatorProperties, scope);
continue;
}
try {
Iif (validatorProperties.propsParameter) {
ok = validator.call(scope, value, validatorProperties);
} else {
ok = validator.call(scope, value);
}
} catch (error) {
ok = false;
validatorProperties.reason = error;
if (error.message) {
validatorProperties.message = error.message;
}
}
Iif (ok != null && typeof ok.then === 'function') {
ok.then(
function(ok) { validate(ok, validatorProperties, scope); },
function(error) {
validatorProperties.reason = error;
validatorProperties.message = error.message;
ok = false;
validate(ok, validatorProperties, scope);
});
} else {
validate(ok, validatorProperties, scope);
}
}
function validate(ok, validatorProperties, scope) {
Iif (err) {
return;
}
Eif (ok === undefined || ok) {
if (--count <= 0) {
immediate(function() {
fn(null);
});
}
} else {
const ErrorConstructor = validatorProperties.ErrorConstructor || ValidatorError;
err = new ErrorConstructor(validatorProperties, scope);
err[validatorErrorSymbol] = true;
immediate(function() {
fn(err);
});
}
}
};
function _validate(ok, validatorProperties) {
if (ok !== undefined && !ok) {
const ErrorConstructor = validatorProperties.ErrorConstructor || ValidatorError;
const err = new ErrorConstructor(validatorProperties);
err[validatorErrorSymbol] = true;
return err;
}
}
/**
* Performs a validation of `value` using the validators declared for this SchemaType.
*
* #### Note:
*
* This method ignores the asynchronous validators.
*
* @param {Any} value
* @param {Object} scope
* @param {Object} [options]
* @param {Object} [options.path]
* @return {MongooseError|null}
* @api private
*/
SchemaType.prototype.doValidateSync = function(value, scope, options) {
const path = this.path;
const count = this.validators.length;
if (!count) {
return null;
}
let validators = this.validators;
if (value === void 0) {
if (this.validators.length !== 0 && this.validators[0].type === 'required') {
validators = [this.validators[0]];
} else {
return null;
}
}
let err = null;
let i = 0;
const len = validators.length;
for (i = 0; i < len; ++i) {
const v = validators[i];
if (v === null || typeof v !== 'object') {
continue;
}
const validator = v.validator;
const validatorProperties = isSimpleValidator(v) ? Object.assign({}, v) : clone(v);
validatorProperties.path = options && options.path ? options.path : path;
validatorProperties.fullPath = this.$fullPath;
validatorProperties.value = value;
let ok = false;
// Skip any explicit async validators. Validators that return a promise
// will still run, but won't trigger any errors.
if (isAsyncFunction(validator)) {
continue;
}
if (validator instanceof RegExp) {
err = _validate(validator.test(value), validatorProperties);
continue;
}
if (typeof validator !== 'function') {
continue;
}
try {
if (validatorProperties.propsParameter) {
ok = validator.call(scope, value, validatorProperties);
} else {
ok = validator.call(scope, value);
}
} catch (error) {
ok = false;
validatorProperties.reason = error;
}
// Skip any validators that return a promise, we can't handle those
// synchronously
if (ok != null && typeof ok.then === 'function') {
continue;
}
err = _validate(ok, validatorProperties);
if (err) {
break;
}
}
return err;
};
/**
* Determines if value is a valid Reference.
*
* @param {SchemaType} self
* @param {Object} value
* @param {Document} doc
* @param {Boolean} init
* @return {Boolean}
* @api private
*/
SchemaType._isRef = function(self, value, doc, init) {
// fast path
let ref = init && self.options && (self.options.ref || self.options.refPath);
if (!ref && doc && doc.$__ != null) {
// checks for
// - this populated with adhoc model and no ref was set in schema OR
// - setting / pushing values after population
const path = doc.$__fullPath(self.path, true);
const owner = doc.ownerDocument();
ref = (path != null && owner.$populated(path)) || doc.$populated(self.path);
}
if (ref) {
if (value == null) {
return true;
}
if (!Buffer.isBuffer(value) && // buffers are objects too
value._bsontype !== 'Binary' // raw binary value from the db
&& utils.isObject(value) // might have deselected _id in population query
) {
return true;
}
return init;
}
return false;
};
/*!
* ignore
*/
SchemaType.prototype._castRef = function _castRef(value, doc, init, options) {
if (value == null) {
return value;
}
if (value.$__ != null) {
value.$__.wasPopulated = value.$__.wasPopulated || { value: value._doc._id };
return value;
}
// setting a populated path
if (Buffer.isBuffer(value) || !utils.isObject(value)) {
if (init) {
return value;
}
throw new CastError(this.instance, value, this.path, null, this);
}
// Handle the case where user directly sets a populated
// path to a plain object; cast to the Model used in
// the population query.
const path = doc.$__fullPath(this.path, true);
const owner = doc.ownerDocument();
const pop = owner.$populated(path, true);
let ret = value;
if (!doc.$__.populated ||
!doc.$__.populated[path] ||
!doc.$__.populated[path].options ||
!doc.$__.populated[path].options.options ||
!doc.$__.populated[path].options.options.lean) {
const PopulatedModel = pop ? pop.options[populateModelSymbol] : doc.constructor.db.model(this.options.ref);
ret = PopulatedModel.hydrate(value, null, options);
ret.$__.wasPopulated = { value: ret._doc._id, options: { [populateModelSymbol]: PopulatedModel } };
}
return ret;
};
/*!
* ignore
*/
function handleSingle(val, context) {
return this.castForQuery(null, val, context);
}
/*!
* ignore
*/
function handleArray(val, context) {
const _this = this;
if (!Array.isArray(val)) {
return [this.castForQuery(null, val, context)];
}
return val.map(function(m) {
return _this.castForQuery(null, m, context);
});
}
/**
* Just like handleArray, except also allows `[]` because surprisingly
* `$in: [1, []]` works fine
* @api private
*/
function handle$in(val, context) {
const _this = this;
if (!Array.isArray(val)) {
return [this.castForQuery(null, val, context)];
}
return val.map(function(m) {
if (Array.isArray(m) && m.length === 0) {
return m;
}
return _this.castForQuery(null, m, context);
});
}
/*!
* ignore
*/
SchemaType.prototype.$conditionalHandlers = {
$all: handleArray,
$eq: handleSingle,
$in: handle$in,
$ne: handleSingle,
$nin: handle$in,
$exists: $exists,
$type: $type
};
/**
* Cast the given value with the given optional query operator.
*
* @param {String} [$conditional] query operator, like `$eq` or `$in`
* @param {Any} val
* @param {Query} context
* @return {Any}
* @api private
*/
SchemaType.prototype.castForQuery = function($conditional, val, context) {
let handler;
if ($conditional != null) {
handler = this.$conditionalHandlers[$conditional];
if (!handler) {
throw new Error('Can\'t use ' + $conditional);
}
return handler.call(this, val, context);
}
try {
return this.applySetters(val, context);
} catch (err) {
if (err instanceof CastError && err.path === this.path && this.$fullPath != null) {
err.path = this.$fullPath;
}
throw err;
}
};
/**
* Set & Get the `checkRequired` function
* Override the function the required validator uses to check whether a value
* passes the `required` check. Override this on the individual SchemaType.
*
* #### Example:
*
* // Use this to allow empty strings to pass the `required` validator
* mongoose.Schema.Types.String.checkRequired(v => typeof v === 'string');
*
* @param {Function} [fn] If set, will overwrite the current set function
* @return {Function} The input `fn` or the already set function
* @static
* @memberOf SchemaType
* @function checkRequired
* @api public
*/
SchemaType.checkRequired = function(fn) {
Iif (arguments.length !== 0) {
this._checkRequired = fn;
}
return this._checkRequired;
};
/**
* Default check for if this path satisfies the `required` validator.
*
* @param {Any} val
* @return {Boolean} `true` when the value is not `null`, `false` otherwise
* @api private
*/
SchemaType.prototype.checkRequired = function(val) {
return val != null;
};
/**
* Clone the current SchemaType
*
* @return {SchemaType} The cloned SchemaType instance
* @api private
*/
SchemaType.prototype.clone = function() {
const options = Object.assign({}, this.options);
const schematype = new this.constructor(this.path, options, this.instance);
schematype.validators = this.validators.slice();
if (this.requiredValidator !== undefined) schematype.requiredValidator = this.requiredValidator;
if (this.defaultValue !== undefined) schematype.defaultValue = this.defaultValue;
if (this.$immutable !== undefined && this.options.immutable === undefined) {
schematype.$immutable = this.$immutable;
handleImmutable(schematype);
}
if (this._index !== undefined) schematype._index = this._index;
if (this.selected !== undefined) schematype.selected = this.selected;
if (this.isRequired !== undefined) schematype.isRequired = this.isRequired;
if (this.originalRequiredValue !== undefined) schematype.originalRequiredValue = this.originalRequiredValue;
schematype.getters = this.getters.slice();
schematype.setters = this.setters.slice();
return schematype;
};
/**
* Returns the embedded schema type, if any. For arrays, document arrays, and maps, `getEmbeddedSchemaType()`
* returns the schema type of the array's elements (or map's elements). For other types, `getEmbeddedSchemaType()`
* returns `undefined`.
*
* #### Example:
*
* const schema = new Schema({ name: String, tags: [String] });
* schema.path('name').getEmbeddedSchemaType(); // undefined
* schema.path('tags').getEmbeddedSchemaType(); // SchemaString { path: 'tags', ... }
*
* @returns {SchemaType} embedded schematype
* @api public
*/
SchemaType.prototype.getEmbeddedSchemaType = function getEmbeddedSchemaType() {
return this.$embeddedSchemaType;
};
/*!
* If _duplicateKeyErrorMessage is a string, replace unique index errors "E11000 duplicate key error" with this string.
*
* @api private
*/
SchemaType.prototype._duplicateKeyErrorMessage = null;
/**
* Returns this schema type's representation in a JSON schema.
*
* @param [options]
* @param [options.useBsonType=false] If true, return a representation with `bsonType` for use with MongoDB's `$jsonSchema`.
* @returns {Object} JSON schema properties
*/
SchemaType.prototype.toJSONSchema = function toJSONSchema() {
throw new Error('Converting unsupported SchemaType to JSON Schema: ' + this.instance);
};
/*!
* Module exports.
*/
module.exports = exports = SchemaType;
exports.CastError = CastError;
exports.ValidatorError = ValidatorError;
|