uuid.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*!
  2. * Module dependencies.
  3. */
  4. 'use strict';
  5. const SchemaType = require('../schemaType');
  6. const CastError = SchemaType.CastError;
  7. const castUUID = require('../cast/uuid');
  8. const createJSONSchemaTypeDefinition = require('../helpers/createJSONSchemaTypeDefinition');
  9. const utils = require('../utils');
  10. const handleBitwiseOperator = require('./operators/bitwise');
  11. const UUID_FORMAT = castUUID.UUID_FORMAT;
  12. /**
  13. * Convert binary to a uuid string
  14. * @param {Buffer|Binary|String} uuidBin The value to process
  15. * @returns {String} The completed uuid-string
  16. * @api private
  17. */
  18. function binaryToString(uuidBin) {
  19. // i(hasezoey) dont quite know why, but "uuidBin" may sometimes also be the already processed string
  20. let hex;
  21. if (typeof uuidBin !== 'string' && uuidBin != null) {
  22. hex = uuidBin.toString('hex');
  23. const uuidStr = hex.substring(0, 8) + '-' + hex.substring(8, 8 + 4) + '-' + hex.substring(12, 12 + 4) + '-' + hex.substring(16, 16 + 4) + '-' + hex.substring(20, 20 + 12);
  24. return uuidStr;
  25. }
  26. return uuidBin;
  27. }
  28. /**
  29. * UUIDv1 SchemaType constructor.
  30. *
  31. * @param {String} key
  32. * @param {Object} options
  33. * @param {Object} _schemaOptions
  34. * @param {Schema} parentSchema
  35. * @inherits SchemaType
  36. * @api public
  37. */
  38. function SchemaUUID(key, options, _schemaOptions, parentSchema) {
  39. SchemaType.call(this, key, options, 'UUID', parentSchema);
  40. }
  41. /**
  42. * This schema type's name, to defend against minifiers that mangle
  43. * function names.
  44. *
  45. * @api public
  46. */
  47. SchemaUUID.schemaName = 'UUID';
  48. SchemaUUID.defaultOptions = {};
  49. /*!
  50. * Inherits from SchemaType.
  51. */
  52. SchemaUUID.prototype = Object.create(SchemaType.prototype);
  53. SchemaUUID.prototype.constructor = SchemaUUID;
  54. /*!
  55. * ignore
  56. */
  57. SchemaUUID._cast = castUUID;
  58. /**
  59. * Attaches a getter for all UUID instances.
  60. *
  61. * #### Example:
  62. *
  63. * // Note that `v` is a string by default
  64. * mongoose.Schema.UUID.get(v => v.toUpperCase());
  65. *
  66. * const Model = mongoose.model('Test', new Schema({ test: 'UUID' }));
  67. * new Model({ test: uuid.v4() }).test; // UUID with all uppercase
  68. *
  69. * @param {Function} getter
  70. * @return {this}
  71. * @function get
  72. * @static
  73. * @api public
  74. */
  75. SchemaUUID.get = SchemaType.get;
  76. /**
  77. * Sets a default option for all UUID instances.
  78. *
  79. * #### Example:
  80. *
  81. * // Make all UUIDs have `required` of true by default.
  82. * mongoose.Schema.UUID.set('required', true);
  83. *
  84. * const User = mongoose.model('User', new Schema({ test: mongoose.UUID }));
  85. * new User({ }).validateSync().errors.test.message; // Path `test` is required.
  86. *
  87. * @param {String} option The option you'd like to set the value for
  88. * @param {Any} value value for option
  89. * @return {undefined}
  90. * @function set
  91. * @static
  92. * @api public
  93. */
  94. SchemaUUID.set = SchemaType.set;
  95. SchemaUUID.setters = [];
  96. /**
  97. * Get/set the function used to cast arbitrary values to UUIDs.
  98. *
  99. * #### Example:
  100. *
  101. * // Make Mongoose refuse to cast UUIDs with 0 length
  102. * const original = mongoose.Schema.Types.UUID.cast();
  103. * mongoose.UUID.cast(v => {
  104. * assert.ok(typeof v === "string" && v.length > 0);
  105. * return original(v);
  106. * });
  107. *
  108. * // Or disable casting entirely
  109. * mongoose.UUID.cast(false);
  110. *
  111. * @param {Function} [caster]
  112. * @return {Function}
  113. * @function get
  114. * @static
  115. * @api public
  116. */
  117. SchemaUUID.cast = function cast(caster) {
  118. if (arguments.length === 0) {
  119. return this._cast;
  120. }
  121. if (caster === false) {
  122. caster = this._defaultCaster;
  123. }
  124. this._cast = caster;
  125. return this._cast;
  126. };
  127. /*!
  128. * ignore
  129. */
  130. SchemaUUID._checkRequired = v => v != null;
  131. /**
  132. * Override the function the required validator uses to check whether a string
  133. * passes the `required` check.
  134. *
  135. * @param {Function} fn
  136. * @return {Function}
  137. * @function checkRequired
  138. * @static
  139. * @api public
  140. */
  141. SchemaUUID.checkRequired = SchemaType.checkRequired;
  142. /**
  143. * Check if the given value satisfies a required validator.
  144. *
  145. * @param {Any} value
  146. * @return {Boolean}
  147. * @api public
  148. */
  149. SchemaUUID.prototype.checkRequired = function checkRequired(value) {
  150. if (Buffer.isBuffer(value)) {
  151. value = binaryToString(value);
  152. }
  153. return value != null && UUID_FORMAT.test(value);
  154. };
  155. /**
  156. * Casts to UUID
  157. *
  158. * @param {Object} value
  159. * @param {Object} doc
  160. * @param {Boolean} init whether this is an initialization cast
  161. * @api private
  162. */
  163. SchemaUUID.prototype.cast = function(value, doc, init, prev, options) {
  164. if (utils.isNonBuiltinObject(value) &&
  165. SchemaType._isRef(this, value, doc, init)) {
  166. return this._castRef(value, doc, init, options);
  167. }
  168. let castFn;
  169. if (typeof this._castFunction === 'function') {
  170. castFn = this._castFunction;
  171. } else if (typeof this.constructor.cast === 'function') {
  172. castFn = this.constructor.cast();
  173. } else {
  174. castFn = SchemaUUID.cast();
  175. }
  176. try {
  177. return castFn(value);
  178. } catch (error) {
  179. throw new CastError(SchemaUUID.schemaName, value, this.path, error, this);
  180. }
  181. };
  182. /*!
  183. * ignore
  184. */
  185. function handleSingle(val) {
  186. return this.cast(val);
  187. }
  188. /*!
  189. * ignore
  190. */
  191. function handleArray(val) {
  192. return val.map((m) => {
  193. return this.cast(m);
  194. });
  195. }
  196. const $conditionalHandlers = {
  197. ...SchemaType.prototype.$conditionalHandlers,
  198. $bitsAllClear: handleBitwiseOperator,
  199. $bitsAnyClear: handleBitwiseOperator,
  200. $bitsAllSet: handleBitwiseOperator,
  201. $bitsAnySet: handleBitwiseOperator,
  202. $all: handleArray,
  203. $in: handleArray,
  204. $ne: handleSingle,
  205. $nin: handleArray
  206. };
  207. /**
  208. * Contains the handlers for different query operators for this schema type.
  209. * For example, `$conditionalHandlers.$exists` is the function Mongoose calls to cast `$exists` filter operators.
  210. *
  211. * @property $conditionalHandlers
  212. * @memberOf SchemaUUID
  213. * @instance
  214. * @api public
  215. */
  216. Object.defineProperty(SchemaUUID.prototype, '$conditionalHandlers', {
  217. enumerable: false,
  218. value: $conditionalHandlers
  219. });
  220. /**
  221. * Casts contents for queries.
  222. *
  223. * @param {String} $conditional
  224. * @param {any} val
  225. * @api private
  226. */
  227. SchemaUUID.prototype.castForQuery = function($conditional, val, context) {
  228. let handler;
  229. if ($conditional != null) {
  230. handler = this.$conditionalHandlers[$conditional];
  231. if (!handler)
  232. throw new Error('Can\'t use ' + $conditional + ' with UUID.');
  233. return handler.call(this, val, context);
  234. }
  235. try {
  236. return this.applySetters(val, context);
  237. } catch (err) {
  238. if (err instanceof CastError && err.path === this.path && this.$fullPath != null) {
  239. err.path = this.$fullPath;
  240. }
  241. throw err;
  242. }
  243. };
  244. /**
  245. * Returns this schema type's representation in a JSON schema.
  246. *
  247. * @param [options]
  248. * @param [options.useBsonType=false] If true, return a representation with `bsonType` for use with MongoDB's `$jsonSchema`.
  249. * @returns {Object} JSON schema properties
  250. */
  251. SchemaUUID.prototype.toJSONSchema = function toJSONSchema(options) {
  252. const isRequired = this.options.required && typeof this.options.required !== 'function';
  253. return createJSONSchemaTypeDefinition('string', 'binData', options?.useBsonType, isRequired);
  254. };
  255. SchemaUUID.prototype.autoEncryptionType = function autoEncryptionType() {
  256. return 'binData';
  257. };
  258. /*!
  259. * Module exports.
  260. */
  261. module.exports = SchemaUUID;