You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
4.2 KiB

  1. /*!
  2. * Module dependencies.
  3. */
  4. var handleBitwiseOperator = require('./operators/bitwise');
  5. var utils = require('../utils');
  6. var MongooseBuffer = require('../types').Buffer;
  7. var SchemaType = require('../schematype');
  8. var Binary = MongooseBuffer.Binary;
  9. var CastError = SchemaType.CastError;
  10. var Document;
  11. /**
  12. * Buffer SchemaType constructor
  13. *
  14. * @param {String} key
  15. * @param {Object} options
  16. * @inherits SchemaType
  17. * @api public
  18. */
  19. function SchemaBuffer(key, options) {
  20. SchemaType.call(this, key, options, 'Buffer');
  21. }
  22. /**
  23. * This schema type's name, to defend against minifiers that mangle
  24. * function names.
  25. *
  26. * @api public
  27. */
  28. SchemaBuffer.schemaName = 'Buffer';
  29. /*!
  30. * Inherits from SchemaType.
  31. */
  32. SchemaBuffer.prototype = Object.create(SchemaType.prototype);
  33. SchemaBuffer.prototype.constructor = SchemaBuffer;
  34. /**
  35. * Check if the given value satisfies a required validator. To satisfy a
  36. * required validator, a buffer must not be null or undefined and have
  37. * non-zero length.
  38. *
  39. * @param {Any} value
  40. * @param {Document} doc
  41. * @return {Boolean}
  42. * @api public
  43. */
  44. SchemaBuffer.prototype.checkRequired = function(value, doc) {
  45. if (SchemaType._isRef(this, value, doc, true)) {
  46. return !!value;
  47. }
  48. return !!(value && value.length);
  49. };
  50. /**
  51. * Casts contents
  52. *
  53. * @param {Object} value
  54. * @param {Document} doc document that triggers the casting
  55. * @param {Boolean} init
  56. * @api private
  57. */
  58. SchemaBuffer.prototype.cast = function(value, doc, init) {
  59. var ret;
  60. if (SchemaType._isRef(this, value, doc, init)) {
  61. // wait! we may need to cast this to a document
  62. if (value === null || value === undefined) {
  63. return value;
  64. }
  65. // lazy load
  66. Document || (Document = require('./../document'));
  67. if (value instanceof Document) {
  68. value.$__.wasPopulated = true;
  69. return value;
  70. }
  71. // setting a populated path
  72. if (Buffer.isBuffer(value)) {
  73. return value;
  74. } else if (!utils.isObject(value)) {
  75. throw new CastError('buffer', value, this.path);
  76. }
  77. // Handle the case where user directly sets a populated
  78. // path to a plain object; cast to the Model used in
  79. // the population query.
  80. var path = doc.$__fullPath(this.path);
  81. var owner = doc.ownerDocument ? doc.ownerDocument() : doc;
  82. var pop = owner.populated(path, true);
  83. ret = new pop.options.model(value);
  84. ret.$__.wasPopulated = true;
  85. return ret;
  86. }
  87. // documents
  88. if (value && value._id) {
  89. value = value._id;
  90. }
  91. if (value && value.isMongooseBuffer) {
  92. return value;
  93. }
  94. if (Buffer.isBuffer(value)) {
  95. if (!value || !value.isMongooseBuffer) {
  96. value = new MongooseBuffer(value, [this.path, doc]);
  97. }
  98. return value;
  99. } else if (value instanceof Binary) {
  100. ret = new MongooseBuffer(value.value(true), [this.path, doc]);
  101. if (typeof value.sub_type !== 'number') {
  102. throw new CastError('buffer', value, this.path);
  103. }
  104. ret._subtype = value.sub_type;
  105. return ret;
  106. }
  107. if (value === null) {
  108. return value;
  109. }
  110. var type = typeof value;
  111. if (type === 'string' || type === 'number' || Array.isArray(value)) {
  112. if (type === 'number') {
  113. value = [value];
  114. }
  115. ret = new MongooseBuffer(value, [this.path, doc]);
  116. return ret;
  117. }
  118. throw new CastError('buffer', value, this.path);
  119. };
  120. /*!
  121. * ignore
  122. */
  123. function handleSingle(val) {
  124. return this.castForQuery(val);
  125. }
  126. SchemaBuffer.prototype.$conditionalHandlers =
  127. utils.options(SchemaType.prototype.$conditionalHandlers, {
  128. $bitsAllClear: handleBitwiseOperator,
  129. $bitsAnyClear: handleBitwiseOperator,
  130. $bitsAllSet: handleBitwiseOperator,
  131. $bitsAnySet: handleBitwiseOperator,
  132. $gt: handleSingle,
  133. $gte: handleSingle,
  134. $lt: handleSingle,
  135. $lte: handleSingle
  136. });
  137. /**
  138. * Casts contents for queries.
  139. *
  140. * @param {String} $conditional
  141. * @param {any} [value]
  142. * @api private
  143. */
  144. SchemaBuffer.prototype.castForQuery = function($conditional, val) {
  145. var handler;
  146. if (arguments.length === 2) {
  147. handler = this.$conditionalHandlers[$conditional];
  148. if (!handler) {
  149. throw new Error('Can\'t use ' + $conditional + ' with Buffer.');
  150. }
  151. return handler.call(this, val);
  152. }
  153. val = $conditional;
  154. return this.cast(val).toObject();
  155. };
  156. /*!
  157. * Module exports.
  158. */
  159. module.exports = SchemaBuffer;