* Prepare and invoke a custom validator. * * @private * * @param {Function} validator The custom validator. * @param {string} validatorType the custom validator type (name). * @param {boolean} optAttrDefined Set to true if custom validator was defined from the attribute * @param
(validator, validatorType, optAttrDefined, optValue, optField)
| 246 | * @returns {Promise} A promise. |
| 247 | */ |
| 248 | async _invokeCustomValidator(validator, validatorType, optAttrDefined, optValue, optField) { |
| 249 | let isAsync = false; |
| 250 | |
| 251 | const validatorArity = validator.length; |
| 252 | class="cm">// check if validator is async and requires a callback |
| 253 | let asyncArity = 1; |
| 254 | let errorKey = validatorType; |
| 255 | let invokeArgs; |
| 256 | if (optAttrDefined) { |
| 257 | asyncArity = 2; |
| 258 | invokeArgs = optValue; |
| 259 | errorKey = optField; |
| 260 | } |
| 261 | if (validatorArity === asyncArity) { |
| 262 | isAsync = true; |
| 263 | } |
| 264 | |
| 265 | if (isAsync) { |
| 266 | try { |
| 267 | if (optAttrDefined) { |
| 268 | return await promisify(validator.bind(this.modelInstance, invokeArgs))(); |
| 269 | } |
| 270 | return await promisify(validator.bind(this.modelInstance))(); |
| 271 | } catch (e) { |
| 272 | return this._pushError(false, errorKey, e, optValue, validatorType); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | try { |
| 277 | return await validator.call(this.modelInstance, invokeArgs); |
| 278 | } catch (e) { |
| 279 | return this._pushError(false, errorKey, e, optValue, validatorType); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Prepare and invoke a build-in validator. |
no test coverage detected