@depup/mongoose 9.1.3-depup.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +22 -0
- package/README.md +397 -0
- package/SECURITY.md +1 -0
- package/eslint.config.mjs +198 -0
- package/index.js +64 -0
- package/lib/aggregate.js +1189 -0
- package/lib/cast/bigint.js +46 -0
- package/lib/cast/boolean.js +32 -0
- package/lib/cast/date.js +41 -0
- package/lib/cast/decimal128.js +39 -0
- package/lib/cast/double.js +50 -0
- package/lib/cast/int32.js +36 -0
- package/lib/cast/number.js +42 -0
- package/lib/cast/objectid.js +29 -0
- package/lib/cast/string.js +37 -0
- package/lib/cast/uuid.js +35 -0
- package/lib/cast.js +436 -0
- package/lib/collection.js +321 -0
- package/lib/connection.js +1855 -0
- package/lib/connectionState.js +26 -0
- package/lib/constants.js +73 -0
- package/lib/cursor/aggregationCursor.js +466 -0
- package/lib/cursor/changeStream.js +198 -0
- package/lib/cursor/queryCursor.js +622 -0
- package/lib/document.js +5521 -0
- package/lib/driver.js +15 -0
- package/lib/drivers/SPEC.md +4 -0
- package/lib/drivers/node-mongodb-native/bulkWriteResult.js +5 -0
- package/lib/drivers/node-mongodb-native/collection.js +393 -0
- package/lib/drivers/node-mongodb-native/connection.js +506 -0
- package/lib/drivers/node-mongodb-native/index.js +10 -0
- package/lib/error/browserMissingSchema.js +29 -0
- package/lib/error/bulkSaveIncompleteError.js +44 -0
- package/lib/error/bulkWriteError.js +41 -0
- package/lib/error/cast.js +158 -0
- package/lib/error/createCollectionsError.js +26 -0
- package/lib/error/divergentArray.js +40 -0
- package/lib/error/eachAsyncMultiError.js +41 -0
- package/lib/error/index.js +237 -0
- package/lib/error/invalidSchemaOption.js +32 -0
- package/lib/error/messages.js +47 -0
- package/lib/error/missingSchema.js +33 -0
- package/lib/error/mongooseError.js +13 -0
- package/lib/error/notFound.js +47 -0
- package/lib/error/objectExpected.js +31 -0
- package/lib/error/objectParameter.js +31 -0
- package/lib/error/overwriteModel.js +31 -0
- package/lib/error/parallelSave.js +33 -0
- package/lib/error/parallelValidate.js +33 -0
- package/lib/error/serverSelection.js +62 -0
- package/lib/error/setOptionError.js +103 -0
- package/lib/error/strict.js +35 -0
- package/lib/error/strictPopulate.js +31 -0
- package/lib/error/syncIndexes.js +30 -0
- package/lib/error/validation.js +97 -0
- package/lib/error/validator.js +100 -0
- package/lib/error/version.js +38 -0
- package/lib/helpers/aggregate/prepareDiscriminatorPipeline.js +39 -0
- package/lib/helpers/aggregate/stringifyFunctionOperators.js +50 -0
- package/lib/helpers/arrayDepth.js +33 -0
- package/lib/helpers/clone.js +204 -0
- package/lib/helpers/common.js +127 -0
- package/lib/helpers/createJSONSchemaTypeDefinition.js +24 -0
- package/lib/helpers/cursor/eachAsync.js +225 -0
- package/lib/helpers/discriminator/applyEmbeddedDiscriminators.js +36 -0
- package/lib/helpers/discriminator/areDiscriminatorValuesEqual.js +16 -0
- package/lib/helpers/discriminator/checkEmbeddedDiscriminatorKeyProjection.js +12 -0
- package/lib/helpers/discriminator/getConstructor.js +29 -0
- package/lib/helpers/discriminator/getDiscriminatorByValue.js +28 -0
- package/lib/helpers/discriminator/getSchemaDiscriminatorByValue.js +27 -0
- package/lib/helpers/discriminator/mergeDiscriminatorSchema.js +91 -0
- package/lib/helpers/document/applyDefaults.js +132 -0
- package/lib/helpers/document/applyTimestamps.js +106 -0
- package/lib/helpers/document/applyVirtuals.js +147 -0
- package/lib/helpers/document/cleanModifiedSubpaths.js +45 -0
- package/lib/helpers/document/compile.js +238 -0
- package/lib/helpers/document/getDeepestSubdocumentForPath.js +38 -0
- package/lib/helpers/document/getEmbeddedDiscriminatorPath.js +53 -0
- package/lib/helpers/document/handleSpreadDoc.js +35 -0
- package/lib/helpers/each.js +25 -0
- package/lib/helpers/error/combinePathErrors.js +22 -0
- package/lib/helpers/firstKey.js +8 -0
- package/lib/helpers/get.js +65 -0
- package/lib/helpers/getConstructorName.js +16 -0
- package/lib/helpers/getDefaultBulkwriteResult.js +18 -0
- package/lib/helpers/getFunctionName.js +10 -0
- package/lib/helpers/immediate.js +16 -0
- package/lib/helpers/indexes/applySchemaCollation.js +13 -0
- package/lib/helpers/indexes/decorateDiscriminatorIndexOptions.js +14 -0
- package/lib/helpers/indexes/getRelatedIndexes.js +63 -0
- package/lib/helpers/indexes/isDefaultIdIndex.js +18 -0
- package/lib/helpers/indexes/isIndexEqual.js +95 -0
- package/lib/helpers/indexes/isIndexSpecEqual.js +32 -0
- package/lib/helpers/indexes/isTextIndex.js +16 -0
- package/lib/helpers/indexes/isTimeseriesIndex.js +16 -0
- package/lib/helpers/isAsyncFunction.js +9 -0
- package/lib/helpers/isBsonType.js +15 -0
- package/lib/helpers/isMongooseObject.js +22 -0
- package/lib/helpers/isObject.js +16 -0
- package/lib/helpers/isPOJO.js +12 -0
- package/lib/helpers/isPromise.js +6 -0
- package/lib/helpers/isSimpleValidator.js +22 -0
- package/lib/helpers/minimize.js +41 -0
- package/lib/helpers/model/applyDefaultsToPOJO.js +52 -0
- package/lib/helpers/model/applyHooks.js +140 -0
- package/lib/helpers/model/applyMethods.js +70 -0
- package/lib/helpers/model/applyStaticHooks.js +33 -0
- package/lib/helpers/model/applyStatics.js +13 -0
- package/lib/helpers/model/castBulkWrite.js +316 -0
- package/lib/helpers/model/decorateBulkWriteResult.js +8 -0
- package/lib/helpers/model/discriminator.js +265 -0
- package/lib/helpers/model/pushNestedArrayPaths.js +15 -0
- package/lib/helpers/omitUndefined.js +20 -0
- package/lib/helpers/once.js +12 -0
- package/lib/helpers/parallelLimit.js +37 -0
- package/lib/helpers/path/parentPaths.js +18 -0
- package/lib/helpers/path/setDottedPath.js +33 -0
- package/lib/helpers/pluralize.js +95 -0
- package/lib/helpers/populate/assignRawDocsToIdStructure.js +129 -0
- package/lib/helpers/populate/assignVals.js +360 -0
- package/lib/helpers/populate/createPopulateQueryFilter.js +97 -0
- package/lib/helpers/populate/getModelsMapForPopulate.js +776 -0
- package/lib/helpers/populate/getSchemaTypes.js +228 -0
- package/lib/helpers/populate/getVirtual.js +103 -0
- package/lib/helpers/populate/leanPopulateMap.js +7 -0
- package/lib/helpers/populate/lookupLocalFields.js +40 -0
- package/lib/helpers/populate/markArraySubdocsPopulated.js +49 -0
- package/lib/helpers/populate/modelNamesFromRefPath.js +66 -0
- package/lib/helpers/populate/removeDeselectedForeignField.js +31 -0
- package/lib/helpers/populate/setPopulatedVirtualValue.js +33 -0
- package/lib/helpers/populate/skipPopulateValue.js +10 -0
- package/lib/helpers/populate/validateRef.js +19 -0
- package/lib/helpers/printJestWarning.js +21 -0
- package/lib/helpers/processConnectionOptions.js +65 -0
- package/lib/helpers/projection/applyProjection.js +83 -0
- package/lib/helpers/projection/hasIncludedChildren.js +41 -0
- package/lib/helpers/projection/isDefiningProjection.js +18 -0
- package/lib/helpers/projection/isExclusive.js +37 -0
- package/lib/helpers/projection/isInclusive.js +39 -0
- package/lib/helpers/projection/isNestedProjection.js +8 -0
- package/lib/helpers/projection/isPathExcluded.js +40 -0
- package/lib/helpers/projection/isPathSelectedInclusive.js +28 -0
- package/lib/helpers/projection/isSubpath.js +14 -0
- package/lib/helpers/projection/parseProjection.js +33 -0
- package/lib/helpers/query/applyGlobalOption.js +29 -0
- package/lib/helpers/query/cast$expr.js +287 -0
- package/lib/helpers/query/castFilterPath.js +54 -0
- package/lib/helpers/query/castUpdate.js +643 -0
- package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +103 -0
- package/lib/helpers/query/handleImmutable.js +44 -0
- package/lib/helpers/query/handleReadPreferenceAliases.js +23 -0
- package/lib/helpers/query/hasDollarKeys.js +23 -0
- package/lib/helpers/query/isOperator.js +14 -0
- package/lib/helpers/query/sanitizeFilter.js +38 -0
- package/lib/helpers/query/sanitizeProjection.js +14 -0
- package/lib/helpers/query/selectPopulatedFields.js +62 -0
- package/lib/helpers/query/trusted.js +13 -0
- package/lib/helpers/query/validOps.js +3 -0
- package/lib/helpers/schema/addAutoId.js +7 -0
- package/lib/helpers/schema/applyBuiltinPlugins.js +12 -0
- package/lib/helpers/schema/applyPlugins.js +55 -0
- package/lib/helpers/schema/applyReadConcern.js +20 -0
- package/lib/helpers/schema/applyWriteConcern.js +39 -0
- package/lib/helpers/schema/cleanPositionalOperators.js +12 -0
- package/lib/helpers/schema/getIndexes.js +171 -0
- package/lib/helpers/schema/getKeysInSchemaOrder.js +28 -0
- package/lib/helpers/schema/getPath.js +43 -0
- package/lib/helpers/schema/getSubdocumentStrictValue.js +32 -0
- package/lib/helpers/schema/handleIdOption.js +20 -0
- package/lib/helpers/schema/handleTimestampOption.js +24 -0
- package/lib/helpers/schema/idGetter.js +34 -0
- package/lib/helpers/schema/merge.js +36 -0
- package/lib/helpers/schematype/handleImmutable.js +50 -0
- package/lib/helpers/setDefaultsOnInsert.js +158 -0
- package/lib/helpers/specialProperties.js +3 -0
- package/lib/helpers/symbols.js +20 -0
- package/lib/helpers/timers.js +3 -0
- package/lib/helpers/timestamps/setDocumentTimestamps.js +26 -0
- package/lib/helpers/timestamps/setupTimestamps.js +116 -0
- package/lib/helpers/topology/allServersUnknown.js +12 -0
- package/lib/helpers/topology/isAtlas.js +31 -0
- package/lib/helpers/topology/isSSLError.js +16 -0
- package/lib/helpers/update/applyTimestampsToChildren.js +193 -0
- package/lib/helpers/update/applyTimestampsToUpdate.js +131 -0
- package/lib/helpers/update/castArrayFilters.js +113 -0
- package/lib/helpers/update/decorateUpdateWithVersionKey.js +35 -0
- package/lib/helpers/update/modifiedPaths.js +33 -0
- package/lib/helpers/update/moveImmutableProperties.js +53 -0
- package/lib/helpers/update/removeUnusedArrayFilters.js +32 -0
- package/lib/helpers/update/updatedPathsByArrayFilter.js +27 -0
- package/lib/helpers/updateValidators.js +193 -0
- package/lib/index.js +17 -0
- package/lib/internal.js +46 -0
- package/lib/model.js +5010 -0
- package/lib/modifiedPathsSnapshot.js +9 -0
- package/lib/mongoose.js +1411 -0
- package/lib/options/populateOptions.js +36 -0
- package/lib/options/propertyOptions.js +8 -0
- package/lib/options/saveOptions.js +16 -0
- package/lib/options/schemaArrayOptions.js +78 -0
- package/lib/options/schemaBufferOptions.js +38 -0
- package/lib/options/schemaDateOptions.js +71 -0
- package/lib/options/schemaDocumentArrayOptions.js +68 -0
- package/lib/options/schemaMapOptions.js +43 -0
- package/lib/options/schemaNumberOptions.js +101 -0
- package/lib/options/schemaObjectIdOptions.js +64 -0
- package/lib/options/schemaStringOptions.js +138 -0
- package/lib/options/schemaSubdocumentOptions.js +66 -0
- package/lib/options/schemaTypeOptions.js +244 -0
- package/lib/options/schemaUnionOptions.js +32 -0
- package/lib/options/virtualOptions.js +164 -0
- package/lib/options.js +17 -0
- package/lib/plugins/index.js +6 -0
- package/lib/plugins/saveSubdocs.js +76 -0
- package/lib/plugins/sharding.js +84 -0
- package/lib/plugins/trackTransaction.js +84 -0
- package/lib/plugins/validateBeforeSave.js +41 -0
- package/lib/query.js +5673 -0
- package/lib/queryHelpers.js +387 -0
- package/lib/schema/array.js +699 -0
- package/lib/schema/bigint.js +282 -0
- package/lib/schema/boolean.js +332 -0
- package/lib/schema/buffer.js +343 -0
- package/lib/schema/date.js +467 -0
- package/lib/schema/decimal128.js +263 -0
- package/lib/schema/documentArray.js +656 -0
- package/lib/schema/documentArrayElement.js +137 -0
- package/lib/schema/double.js +246 -0
- package/lib/schema/index.js +32 -0
- package/lib/schema/int32.js +289 -0
- package/lib/schema/map.js +201 -0
- package/lib/schema/mixed.js +146 -0
- package/lib/schema/number.js +510 -0
- package/lib/schema/objectId.js +333 -0
- package/lib/schema/operators/bitwise.js +38 -0
- package/lib/schema/operators/exists.js +12 -0
- package/lib/schema/operators/geospatial.js +107 -0
- package/lib/schema/operators/helpers.js +32 -0
- package/lib/schema/operators/text.js +39 -0
- package/lib/schema/operators/type.js +20 -0
- package/lib/schema/string.js +733 -0
- package/lib/schema/subdocument.js +436 -0
- package/lib/schema/symbols.js +5 -0
- package/lib/schema/union.js +113 -0
- package/lib/schema/uuid.js +305 -0
- package/lib/schema.js +3226 -0
- package/lib/schemaType.js +1835 -0
- package/lib/stateMachine.js +232 -0
- package/lib/types/array/index.js +119 -0
- package/lib/types/array/isMongooseArray.js +5 -0
- package/lib/types/array/methods/index.js +1095 -0
- package/lib/types/arraySubdocument.js +207 -0
- package/lib/types/buffer.js +294 -0
- package/lib/types/decimal128.js +13 -0
- package/lib/types/documentArray/index.js +113 -0
- package/lib/types/documentArray/isMongooseDocumentArray.js +5 -0
- package/lib/types/documentArray/methods/index.js +415 -0
- package/lib/types/double.js +13 -0
- package/lib/types/index.js +23 -0
- package/lib/types/map.js +419 -0
- package/lib/types/objectid.js +41 -0
- package/lib/types/subdocument.js +464 -0
- package/lib/types/uuid.js +13 -0
- package/lib/utils.js +1054 -0
- package/lib/validOptions.js +42 -0
- package/lib/virtualType.js +204 -0
- package/package.json +148 -0
- package/types/aggregate.d.ts +180 -0
- package/types/augmentations.d.ts +9 -0
- package/types/callback.d.ts +8 -0
- package/types/collection.d.ts +49 -0
- package/types/connection.d.ts +297 -0
- package/types/cursor.d.ts +67 -0
- package/types/document.d.ts +374 -0
- package/types/error.d.ts +143 -0
- package/types/expressions.d.ts +3053 -0
- package/types/helpers.d.ts +32 -0
- package/types/index.d.ts +1056 -0
- package/types/indexes.d.ts +97 -0
- package/types/inferhydrateddoctype.d.ts +115 -0
- package/types/inferrawdoctype.d.ts +135 -0
- package/types/inferschematype.d.ts +337 -0
- package/types/middlewares.d.ts +59 -0
- package/types/models.d.ts +1306 -0
- package/types/mongooseoptions.d.ts +228 -0
- package/types/pipelinestage.d.ts +333 -0
- package/types/populate.d.ts +53 -0
- package/types/query.d.ts +934 -0
- package/types/schemaoptions.d.ts +282 -0
- package/types/schematypes.d.ts +654 -0
- package/types/session.d.ts +32 -0
- package/types/types.d.ts +109 -0
- package/types/utility.d.ts +175 -0
- package/types/validation.d.ts +39 -0
- package/types/virtuals.d.ts +14 -0
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*!
|
|
4
|
+
* Module requirements.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const MongooseError = require('../error/index');
|
|
8
|
+
const SchemaNumberOptions = require('../options/schemaNumberOptions');
|
|
9
|
+
const SchemaType = require('../schemaType');
|
|
10
|
+
const castNumber = require('../cast/number');
|
|
11
|
+
const createJSONSchemaTypeDefinition = require('../helpers/createJSONSchemaTypeDefinition');
|
|
12
|
+
const handleBitwiseOperator = require('./operators/bitwise');
|
|
13
|
+
const utils = require('../utils');
|
|
14
|
+
|
|
15
|
+
const CastError = SchemaType.CastError;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Number SchemaType constructor.
|
|
19
|
+
*
|
|
20
|
+
* @param {String} key
|
|
21
|
+
* @param {Object} options
|
|
22
|
+
* @param {Object} schemaOptions
|
|
23
|
+
* @param {Schema} parentSchema
|
|
24
|
+
* @inherits SchemaType
|
|
25
|
+
* @api public
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
function SchemaNumber(key, options, _schemaOptions, parentSchema) {
|
|
29
|
+
this.enumValues = [];
|
|
30
|
+
SchemaType.call(this, key, options, 'Number', parentSchema);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Attaches a getter for all Number instances.
|
|
35
|
+
*
|
|
36
|
+
* #### Example:
|
|
37
|
+
*
|
|
38
|
+
* // Make all numbers round down
|
|
39
|
+
* mongoose.Number.get(function(v) { return Math.floor(v); });
|
|
40
|
+
*
|
|
41
|
+
* const Model = mongoose.model('Test', new Schema({ test: Number }));
|
|
42
|
+
* new Model({ test: 3.14 }).test; // 3
|
|
43
|
+
*
|
|
44
|
+
* @param {Function} getter
|
|
45
|
+
* @return {this}
|
|
46
|
+
* @function get
|
|
47
|
+
* @static
|
|
48
|
+
* @api public
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
SchemaNumber.get = SchemaType.get;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Sets a default option for all Number instances.
|
|
55
|
+
*
|
|
56
|
+
* #### Example:
|
|
57
|
+
*
|
|
58
|
+
* // Make all numbers have option `min` equal to 0.
|
|
59
|
+
* mongoose.Schema.Types.Number.set('min', 0);
|
|
60
|
+
*
|
|
61
|
+
* const Order = mongoose.model('Order', new Schema({ amount: Number }));
|
|
62
|
+
* new Order({ amount: -10 }).validateSync().errors.amount.message; // Path `amount` must be larger than 0.
|
|
63
|
+
*
|
|
64
|
+
* @param {String} option The option you'd like to set the value for
|
|
65
|
+
* @param {Any} value value for option
|
|
66
|
+
* @return {undefined}
|
|
67
|
+
* @function set
|
|
68
|
+
* @static
|
|
69
|
+
* @api public
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
SchemaNumber.set = SchemaType.set;
|
|
73
|
+
|
|
74
|
+
SchemaNumber.setters = [];
|
|
75
|
+
|
|
76
|
+
/*!
|
|
77
|
+
* ignore
|
|
78
|
+
*/
|
|
79
|
+
|
|
80
|
+
SchemaNumber._cast = castNumber;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Get/set the function used to cast arbitrary values to numbers.
|
|
84
|
+
*
|
|
85
|
+
* #### Example:
|
|
86
|
+
*
|
|
87
|
+
* // Make Mongoose cast empty strings '' to 0 for paths declared as numbers
|
|
88
|
+
* const original = mongoose.Number.cast();
|
|
89
|
+
* mongoose.Number.cast(v => {
|
|
90
|
+
* if (v === '') { return 0; }
|
|
91
|
+
* return original(v);
|
|
92
|
+
* });
|
|
93
|
+
*
|
|
94
|
+
* // Or disable casting entirely
|
|
95
|
+
* mongoose.Number.cast(false);
|
|
96
|
+
*
|
|
97
|
+
* @param {Function} caster
|
|
98
|
+
* @return {Function}
|
|
99
|
+
* @function cast
|
|
100
|
+
* @static
|
|
101
|
+
* @api public
|
|
102
|
+
*/
|
|
103
|
+
|
|
104
|
+
SchemaNumber.cast = function cast(caster) {
|
|
105
|
+
if (arguments.length === 0) {
|
|
106
|
+
return this._cast;
|
|
107
|
+
}
|
|
108
|
+
if (caster === false) {
|
|
109
|
+
caster = this._defaultCaster;
|
|
110
|
+
}
|
|
111
|
+
this._cast = caster;
|
|
112
|
+
|
|
113
|
+
return this._cast;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/*!
|
|
117
|
+
* ignore
|
|
118
|
+
*/
|
|
119
|
+
|
|
120
|
+
SchemaNumber._defaultCaster = v => {
|
|
121
|
+
if (typeof v !== 'number') {
|
|
122
|
+
throw new Error();
|
|
123
|
+
}
|
|
124
|
+
return v;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* This schema type's name, to defend against minifiers that mangle
|
|
129
|
+
* function names.
|
|
130
|
+
*
|
|
131
|
+
* @api public
|
|
132
|
+
*/
|
|
133
|
+
SchemaNumber.schemaName = 'Number';
|
|
134
|
+
|
|
135
|
+
SchemaNumber.defaultOptions = {};
|
|
136
|
+
|
|
137
|
+
/*!
|
|
138
|
+
* Inherits from SchemaType.
|
|
139
|
+
*/
|
|
140
|
+
SchemaNumber.prototype = Object.create(SchemaType.prototype);
|
|
141
|
+
SchemaNumber.prototype.constructor = SchemaNumber;
|
|
142
|
+
SchemaNumber.prototype.OptionsConstructor = SchemaNumberOptions;
|
|
143
|
+
|
|
144
|
+
/*!
|
|
145
|
+
* ignore
|
|
146
|
+
*/
|
|
147
|
+
|
|
148
|
+
SchemaNumber._checkRequired = v => typeof v === 'number' || v instanceof Number;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Override the function the required validator uses to check whether a string
|
|
152
|
+
* passes the `required` check.
|
|
153
|
+
*
|
|
154
|
+
* @param {Function} fn
|
|
155
|
+
* @return {Function}
|
|
156
|
+
* @function checkRequired
|
|
157
|
+
* @static
|
|
158
|
+
* @api public
|
|
159
|
+
*/
|
|
160
|
+
|
|
161
|
+
SchemaNumber.checkRequired = SchemaType.checkRequired;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Check if the given value satisfies a required validator.
|
|
165
|
+
*
|
|
166
|
+
* @param {Any} value
|
|
167
|
+
* @param {Document} doc
|
|
168
|
+
* @return {Boolean}
|
|
169
|
+
* @api public
|
|
170
|
+
*/
|
|
171
|
+
|
|
172
|
+
SchemaNumber.prototype.checkRequired = function checkRequired(value, doc) {
|
|
173
|
+
if (typeof value === 'object' && SchemaType._isRef(this, value, doc, true)) {
|
|
174
|
+
return value != null;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// `require('util').inherits()` does **not** copy static properties, and
|
|
178
|
+
// plugins like mongoose-float use `inherits()` for pre-ES6.
|
|
179
|
+
const _checkRequired = typeof this.constructor.checkRequired === 'function' ?
|
|
180
|
+
this.constructor.checkRequired() :
|
|
181
|
+
SchemaNumber.checkRequired();
|
|
182
|
+
|
|
183
|
+
return _checkRequired(value);
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Sets a minimum number validator.
|
|
188
|
+
*
|
|
189
|
+
* #### Example:
|
|
190
|
+
*
|
|
191
|
+
* const s = new Schema({ n: { type: Number, min: 10 })
|
|
192
|
+
* const M = db.model('M', s)
|
|
193
|
+
* const m = new M({ n: 9 })
|
|
194
|
+
* m.save(function (err) {
|
|
195
|
+
* console.error(err) // validator error
|
|
196
|
+
* m.n = 10;
|
|
197
|
+
* m.save() // success
|
|
198
|
+
* })
|
|
199
|
+
*
|
|
200
|
+
* // custom error messages
|
|
201
|
+
* // We can also use the special {MIN} token which will be replaced with the invalid value
|
|
202
|
+
* const min = [10, 'The value of path `{PATH}` ({VALUE}) is beneath the limit ({MIN}).'];
|
|
203
|
+
* const schema = new Schema({ n: { type: Number, min: min })
|
|
204
|
+
* const M = mongoose.model('Measurement', schema);
|
|
205
|
+
* const s= new M({ n: 4 });
|
|
206
|
+
* s.validate(function (err) {
|
|
207
|
+
* console.log(String(err)) // ValidationError: The value of path `n` (4) is beneath the limit (10).
|
|
208
|
+
* })
|
|
209
|
+
*
|
|
210
|
+
* @param {Number} value minimum number
|
|
211
|
+
* @param {String} [message] optional custom error message
|
|
212
|
+
* @return {SchemaType} this
|
|
213
|
+
* @see Customized Error Messages https://mongoosejs.com/docs/api/error.html#Error.messages
|
|
214
|
+
* @api public
|
|
215
|
+
*/
|
|
216
|
+
|
|
217
|
+
SchemaNumber.prototype.min = function(value, message) {
|
|
218
|
+
if (this.minValidator) {
|
|
219
|
+
this.validators = this.validators.filter(function(v) {
|
|
220
|
+
return v.validator !== this.minValidator;
|
|
221
|
+
}, this);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (value != null) {
|
|
225
|
+
let msg = message || MongooseError.messages.Number.min;
|
|
226
|
+
msg = msg.replace(/{MIN}/, value);
|
|
227
|
+
this.validators.push({
|
|
228
|
+
validator: this.minValidator = function(v) {
|
|
229
|
+
return v == null || v >= value;
|
|
230
|
+
},
|
|
231
|
+
message: msg,
|
|
232
|
+
type: 'min',
|
|
233
|
+
min: value
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return this;
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Sets a maximum number validator.
|
|
242
|
+
*
|
|
243
|
+
* #### Example:
|
|
244
|
+
*
|
|
245
|
+
* const s = new Schema({ n: { type: Number, max: 10 })
|
|
246
|
+
* const M = db.model('M', s)
|
|
247
|
+
* const m = new M({ n: 11 })
|
|
248
|
+
* m.save(function (err) {
|
|
249
|
+
* console.error(err) // validator error
|
|
250
|
+
* m.n = 10;
|
|
251
|
+
* m.save() // success
|
|
252
|
+
* })
|
|
253
|
+
*
|
|
254
|
+
* // custom error messages
|
|
255
|
+
* // We can also use the special {MAX} token which will be replaced with the invalid value
|
|
256
|
+
* const max = [10, 'The value of path `{PATH}` ({VALUE}) exceeds the limit ({MAX}).'];
|
|
257
|
+
* const schema = new Schema({ n: { type: Number, max: max })
|
|
258
|
+
* const M = mongoose.model('Measurement', schema);
|
|
259
|
+
* const s= new M({ n: 4 });
|
|
260
|
+
* s.validate(function (err) {
|
|
261
|
+
* console.log(String(err)) // ValidationError: The value of path `n` (4) exceeds the limit (10).
|
|
262
|
+
* })
|
|
263
|
+
*
|
|
264
|
+
* @param {Number} maximum number
|
|
265
|
+
* @param {String} [message] optional custom error message
|
|
266
|
+
* @return {SchemaType} this
|
|
267
|
+
* @see Customized Error Messages https://mongoosejs.com/docs/api/error.html#Error.messages
|
|
268
|
+
* @api public
|
|
269
|
+
*/
|
|
270
|
+
|
|
271
|
+
SchemaNumber.prototype.max = function(value, message) {
|
|
272
|
+
if (this.maxValidator) {
|
|
273
|
+
this.validators = this.validators.filter(function(v) {
|
|
274
|
+
return v.validator !== this.maxValidator;
|
|
275
|
+
}, this);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (value != null) {
|
|
279
|
+
let msg = message || MongooseError.messages.Number.max;
|
|
280
|
+
msg = msg.replace(/{MAX}/, value);
|
|
281
|
+
this.validators.push({
|
|
282
|
+
validator: this.maxValidator = function(v) {
|
|
283
|
+
return v == null || v <= value;
|
|
284
|
+
},
|
|
285
|
+
message: msg,
|
|
286
|
+
type: 'max',
|
|
287
|
+
max: value
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
return this;
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Sets a enum validator
|
|
296
|
+
*
|
|
297
|
+
* #### Example:
|
|
298
|
+
*
|
|
299
|
+
* const s = new Schema({ n: { type: Number, enum: [1, 2, 3] });
|
|
300
|
+
* const M = db.model('M', s);
|
|
301
|
+
*
|
|
302
|
+
* const m = new M({ n: 4 });
|
|
303
|
+
* await m.save(); // throws validation error
|
|
304
|
+
*
|
|
305
|
+
* m.n = 3;
|
|
306
|
+
* await m.save(); // succeeds
|
|
307
|
+
*
|
|
308
|
+
* @param {Array} values allowed values
|
|
309
|
+
* @param {String} [message] optional custom error message
|
|
310
|
+
* @return {SchemaType} this
|
|
311
|
+
* @see Customized Error Messages https://mongoosejs.com/docs/api/error.html#Error.messages
|
|
312
|
+
* @api public
|
|
313
|
+
*/
|
|
314
|
+
|
|
315
|
+
SchemaNumber.prototype.enum = function(values, message) {
|
|
316
|
+
if (this.enumValidator) {
|
|
317
|
+
this.validators = this.validators.filter(function(v) {
|
|
318
|
+
return v.validator !== this.enumValidator;
|
|
319
|
+
}, this);
|
|
320
|
+
this.enumValidator = false;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
if (values === void 0 || values === false) {
|
|
324
|
+
return this;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (!Array.isArray(values)) {
|
|
328
|
+
const isObjectSyntax = utils.isPOJO(values) && values.values != null;
|
|
329
|
+
if (isObjectSyntax) {
|
|
330
|
+
message = values.message;
|
|
331
|
+
values = values.values;
|
|
332
|
+
} else if (typeof values === 'number') {
|
|
333
|
+
values = Array.prototype.slice.call(arguments);
|
|
334
|
+
message = null;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
if (utils.isPOJO(values)) {
|
|
338
|
+
// TypeScript numeric enums produce objects with reverse
|
|
339
|
+
// mappings, e.g. { 0: 'Zero', 1: 'One', Zero: 0, One: 1 }.
|
|
340
|
+
// Object.values on that will yield ['Zero','One',0,1].
|
|
341
|
+
// For Number schema enums we only want the numeric values,
|
|
342
|
+
// otherwise casting the name strings to Number will throw.
|
|
343
|
+
const keys = Object.keys(values).sort();
|
|
344
|
+
const objVals = Object.values(values).sort();
|
|
345
|
+
// If keys and values are equal and half of values are numbers,
|
|
346
|
+
// this is likely a TS enum with reverse mapping, so use only the numbers.
|
|
347
|
+
if (
|
|
348
|
+
keys.length === objVals.length &&
|
|
349
|
+
keys.every((k, i) => k === String(objVals[i])) &&
|
|
350
|
+
objVals.filter(v => typeof v === 'number').length === Math.floor(objVals.length / 2)
|
|
351
|
+
) {
|
|
352
|
+
values = objVals.filter(v => typeof v === 'number');
|
|
353
|
+
} else {
|
|
354
|
+
// Avoid sorting the values to preserve user-specified order
|
|
355
|
+
values = Object.values(values);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
message = message || MongooseError.messages.Number.enum;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
message = message == null ? MongooseError.messages.Number.enum : message;
|
|
362
|
+
|
|
363
|
+
for (const value of values) {
|
|
364
|
+
if (value !== undefined) {
|
|
365
|
+
this.enumValues.push(this.cast(value));
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const vals = this.enumValues;
|
|
370
|
+
this.enumValidator = v => v == null || vals.indexOf(v) !== -1;
|
|
371
|
+
this.validators.push({
|
|
372
|
+
validator: this.enumValidator,
|
|
373
|
+
message: message,
|
|
374
|
+
type: 'enum',
|
|
375
|
+
enumValues: vals
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
return this;
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Casts to number
|
|
383
|
+
*
|
|
384
|
+
* @param {Object} value value to cast
|
|
385
|
+
* @param {Document} doc document that triggers the casting
|
|
386
|
+
* @param {Boolean} init
|
|
387
|
+
* @api private
|
|
388
|
+
*/
|
|
389
|
+
|
|
390
|
+
SchemaNumber.prototype.cast = function(value, doc, init, prev, options) {
|
|
391
|
+
if (typeof value !== 'number' && SchemaType._isRef(this, value, doc, init)) {
|
|
392
|
+
if (value == null || utils.isNonBuiltinObject(value)) {
|
|
393
|
+
return this._castRef(value, doc, init, options);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const val = value?._id !== undefined ?
|
|
398
|
+
value._id : // documents
|
|
399
|
+
value;
|
|
400
|
+
|
|
401
|
+
let castNumber;
|
|
402
|
+
if (typeof this._castFunction === 'function') {
|
|
403
|
+
castNumber = this._castFunction;
|
|
404
|
+
} else if (typeof this.constructor.cast === 'function') {
|
|
405
|
+
castNumber = this.constructor.cast();
|
|
406
|
+
} else {
|
|
407
|
+
castNumber = SchemaNumber.cast();
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
try {
|
|
411
|
+
return castNumber(val);
|
|
412
|
+
} catch (err) {
|
|
413
|
+
throw new CastError('Number', val, this.path, err, this);
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
/*!
|
|
418
|
+
* ignore
|
|
419
|
+
*/
|
|
420
|
+
|
|
421
|
+
function handleSingle(val) {
|
|
422
|
+
return this.cast(val);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
function handleArray(val) {
|
|
426
|
+
const _this = this;
|
|
427
|
+
if (!Array.isArray(val)) {
|
|
428
|
+
return [this.cast(val)];
|
|
429
|
+
}
|
|
430
|
+
return val.map(function(m) {
|
|
431
|
+
return _this.cast(m);
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const $conditionalHandlers = {
|
|
436
|
+
...SchemaType.prototype.$conditionalHandlers,
|
|
437
|
+
$bitsAllClear: handleBitwiseOperator,
|
|
438
|
+
$bitsAnyClear: handleBitwiseOperator,
|
|
439
|
+
$bitsAllSet: handleBitwiseOperator,
|
|
440
|
+
$bitsAnySet: handleBitwiseOperator,
|
|
441
|
+
$gt: handleSingle,
|
|
442
|
+
$gte: handleSingle,
|
|
443
|
+
$lt: handleSingle,
|
|
444
|
+
$lte: handleSingle,
|
|
445
|
+
$mod: handleArray
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Contains the handlers for different query operators for this schema type.
|
|
450
|
+
* For example, `$conditionalHandlers.$gte` is the function Mongoose calls to cast `$gte` filter operators.
|
|
451
|
+
*
|
|
452
|
+
* @property $conditionalHandlers
|
|
453
|
+
* @memberOf SchemaNumber
|
|
454
|
+
* @instance
|
|
455
|
+
* @api public
|
|
456
|
+
*/
|
|
457
|
+
|
|
458
|
+
Object.defineProperty(SchemaNumber.prototype, '$conditionalHandlers', {
|
|
459
|
+
enumerable: false,
|
|
460
|
+
value: $conditionalHandlers
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Casts contents for queries.
|
|
465
|
+
*
|
|
466
|
+
* @param {String} $conditional
|
|
467
|
+
* @param {any} [value]
|
|
468
|
+
* @api private
|
|
469
|
+
*/
|
|
470
|
+
|
|
471
|
+
SchemaNumber.prototype.castForQuery = function($conditional, val, context) {
|
|
472
|
+
let handler;
|
|
473
|
+
if ($conditional != null) {
|
|
474
|
+
handler = this.$conditionalHandlers[$conditional];
|
|
475
|
+
if (!handler) {
|
|
476
|
+
throw new CastError('number', val, this.path, null, this);
|
|
477
|
+
}
|
|
478
|
+
return handler.call(this, val, context);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
try {
|
|
482
|
+
val = this.applySetters(val, context);
|
|
483
|
+
} catch (err) {
|
|
484
|
+
if (err instanceof CastError && err.path === this.path && this.$fullPath != null) {
|
|
485
|
+
err.path = this.$fullPath;
|
|
486
|
+
}
|
|
487
|
+
throw err;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
return val;
|
|
491
|
+
};
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* Returns this schema type's representation in a JSON schema.
|
|
495
|
+
*
|
|
496
|
+
* @param [options]
|
|
497
|
+
* @param [options.useBsonType=false] If true, return a representation with `bsonType` for use with MongoDB's `$jsonSchema`.
|
|
498
|
+
* @returns {Object} JSON schema properties
|
|
499
|
+
*/
|
|
500
|
+
|
|
501
|
+
SchemaNumber.prototype.toJSONSchema = function toJSONSchema(options) {
|
|
502
|
+
const isRequired = (this.options.required && typeof this.options.required !== 'function') || this.path === '_id';
|
|
503
|
+
return createJSONSchemaTypeDefinition('number', 'number', options?.useBsonType, isRequired);
|
|
504
|
+
};
|
|
505
|
+
|
|
506
|
+
/*!
|
|
507
|
+
* Module exports.
|
|
508
|
+
*/
|
|
509
|
+
|
|
510
|
+
module.exports = SchemaNumber;
|