cast.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 'use strict';
  2. /*!
  3. * Module dependencies.
  4. */
  5. const MongooseError = require('./mongooseError');
  6. const get = require('../helpers/get');
  7. const util = require('util');
  8. /**
  9. * Casting Error constructor.
  10. *
  11. * @param {String} type
  12. * @param {String} value
  13. * @inherits MongooseError
  14. * @api private
  15. */
  16. class CastError extends MongooseError {
  17. constructor(type, value, path, reason, schemaType) {
  18. // If no args, assume we'll `init()` later.
  19. if (arguments.length > 0) {
  20. const stringValue = getStringValue(value);
  21. const messageFormat = getMessageFormat(schemaType);
  22. const msg = formatMessage(null, type, stringValue, path, messageFormat);
  23. super(msg);
  24. this.init(type, value, path, reason, schemaType);
  25. } else {
  26. super(formatMessage());
  27. }
  28. }
  29. /*!
  30. * ignore
  31. */
  32. init(type, value, path, reason, schemaType) {
  33. this.stringValue = getStringValue(value);
  34. this.messageFormat = getMessageFormat(schemaType);
  35. this.kind = type;
  36. this.value = value;
  37. this.path = path;
  38. this.reason = reason;
  39. }
  40. /*!
  41. * ignore
  42. * @param {Readonly<CastError>} other
  43. */
  44. copy(other) {
  45. this.messageFormat = other.messageFormat;
  46. this.stringValue = other.stringValue;
  47. this.kind = other.kind;
  48. this.value = other.value;
  49. this.path = other.path;
  50. this.reason = other.reason;
  51. this.message = other.message;
  52. }
  53. /*!
  54. * ignore
  55. */
  56. setModel(model) {
  57. this.model = model;
  58. this.message = formatMessage(model, this.kind, this.stringValue, this.path,
  59. this.messageFormat);
  60. }
  61. }
  62. Object.defineProperty(CastError.prototype, 'name', {
  63. value: 'CastError'
  64. });
  65. function getStringValue(value) {
  66. let stringValue = util.inspect(value);
  67. stringValue = stringValue.replace(/^'|'$/g, '"');
  68. if (!stringValue.startsWith('"')) {
  69. stringValue = '"' + stringValue + '"';
  70. }
  71. return stringValue;
  72. }
  73. function getMessageFormat(schemaType) {
  74. const messageFormat = get(schemaType, 'options.cast', null);
  75. if (typeof messageFormat === 'string') {
  76. return messageFormat;
  77. }
  78. }
  79. /*!
  80. * ignore
  81. */
  82. function formatMessage(model, kind, stringValue, path, messageFormat) {
  83. if (messageFormat != null) {
  84. let ret = messageFormat.
  85. replace('{KIND}', kind).
  86. replace('{VALUE}', stringValue).
  87. replace('{PATH}', path);
  88. if (model != null) {
  89. ret = ret.replace('{MODEL}', model.modelName);
  90. }
  91. return ret;
  92. } else {
  93. let ret = 'Cast to ' + kind + ' failed for value ' +
  94. stringValue + ' at path "' + path + '"';
  95. if (model != null) {
  96. ret += ' for model "' + model.modelName + '"';
  97. }
  98. return ret;
  99. }
  100. }
  101. /*!
  102. * exports
  103. */
  104. module.exports = CastError;