|
7 | 7 | const MongooseBuffer = require('../types/buffer');
|
8 | 8 | const SchemaType = require('../schemaType');
|
9 | 9 | const CastError = SchemaType.CastError;
|
| 10 | +const castUUID = require('../cast/uuid'); |
10 | 11 | const utils = require('../utils');
|
11 | 12 | const handleBitwiseOperator = require('./operators/bitwise');
|
12 | 13 |
|
13 |
| -const UUID_FORMAT = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i; |
| 14 | +const UUID_FORMAT = castUUID.UUID_FORMAT; |
14 | 15 | const Binary = MongooseBuffer.Binary;
|
15 | 16 |
|
16 |
| -/** |
17 |
| - * Helper function to convert the input hex-string to a buffer |
18 |
| - * @param {String} hex The hex string to convert |
19 |
| - * @returns {Buffer} The hex as buffer |
20 |
| - * @api private |
21 |
| - */ |
22 |
| - |
23 |
| -function hex2buffer(hex) { |
24 |
| - // use buffer built-in function to convert from hex-string to buffer |
25 |
| - const buff = hex != null && Buffer.from(hex, 'hex'); |
26 |
| - return buff; |
27 |
| -} |
28 |
| - |
29 |
| -/** |
30 |
| - * Convert a String to Binary |
31 |
| - * @param {String} uuidStr The value to process |
32 |
| - * @returns {MongooseBuffer} The binary to store |
33 |
| - * @api private |
34 |
| - */ |
35 |
| - |
36 |
| -function stringToBinary(uuidStr) { |
37 |
| - // Protect against undefined & throwing err |
38 |
| - if (typeof uuidStr !== 'string') uuidStr = ''; |
39 |
| - const hex = uuidStr.replace(/[{}-]/g, ''); // remove extra characters |
40 |
| - const bytes = hex2buffer(hex); |
41 |
| - const buff = new MongooseBuffer(bytes); |
42 |
| - buff._subtype = 4; |
43 |
| - |
44 |
| - return buff; |
45 |
| -} |
46 |
| - |
47 | 17 | /**
|
48 | 18 | * Convert binary to a uuid string
|
49 | 19 | * @param {Buffer|Binary|String} uuidBin The value to process
|
@@ -109,44 +79,7 @@ SchemaUUID.prototype.constructor = SchemaUUID;
|
109 | 79 | * ignore
|
110 | 80 | */
|
111 | 81 |
|
112 |
| -SchemaUUID._cast = function(value) { |
113 |
| - if (value == null) { |
114 |
| - return value; |
115 |
| - } |
116 |
| - |
117 |
| - function newBuffer(initbuff) { |
118 |
| - const buff = new MongooseBuffer(initbuff); |
119 |
| - buff._subtype = 4; |
120 |
| - return buff; |
121 |
| - } |
122 |
| - |
123 |
| - if (typeof value === 'string') { |
124 |
| - if (UUID_FORMAT.test(value)) { |
125 |
| - return stringToBinary(value); |
126 |
| - } else { |
127 |
| - throw new CastError(SchemaUUID.schemaName, value, this.path); |
128 |
| - } |
129 |
| - } |
130 |
| - |
131 |
| - if (Buffer.isBuffer(value)) { |
132 |
| - return newBuffer(value); |
133 |
| - } |
134 |
| - |
135 |
| - if (value instanceof Binary) { |
136 |
| - return newBuffer(value.value(true)); |
137 |
| - } |
138 |
| - |
139 |
| - // Re: gh-647 and gh-3030, we're ok with casting using `toString()` |
140 |
| - // **unless** its the default Object.toString, because "[object Object]" |
141 |
| - // doesn't really qualify as useful data |
142 |
| - if (value.toString && value.toString !== Object.prototype.toString) { |
143 |
| - if (UUID_FORMAT.test(value.toString())) { |
144 |
| - return stringToBinary(value.toString()); |
145 |
| - } |
146 |
| - } |
147 |
| - |
148 |
| - throw new CastError(SchemaUUID.schemaName, value, this.path); |
149 |
| -}; |
| 82 | +SchemaUUID._cast = castUUID; |
150 | 83 |
|
151 | 84 | /**
|
152 | 85 | * Attaches a getter for all UUID instances.
|
|
0 commit comments