MCPcopy
hub / github.com/sequelize/sequelize / _singleAttrValidate

Method _singleAttrValidate

lib/instance-validator.js:189–233  ·  lib/instance-validator.js::InstanceValidator._singleAttrValidate

* Validate a single attribute with all the defined built-in validators and custom validators. * * @private * * @param {*} value Anything. * @param {string} field The field name. * @param {boolean} allowNull Whether or not the schema allows null values * * @returns {Promise} A

(value, field, allowNull)

Source from the content-addressed store, hash-verified

187 * @returns {Promise} A promise, will always resolve, auto populates error on this.error local object.
188 */
189 async _singleAttrValidate(value, field, allowNull) {
190 class="cm">// If value is null and allowNull is false, no validators should run (see #9143)
191 if ((value === null || value === undefined) && !allowNull) {
192 class="cm">// The schema validator (_validateSchema) has already generated the validation error. Nothing to do here.
193 return;
194 }
195
196 class="cm">// Promisify each validator
197 const validators = [];
198 _.forIn(this.modelInstance.validators[field], (test, validatorType) => {
199
200 if ([class="st">'isUrl', class="st">'isURL', class="st">'isEmail'].includes(validatorType)) {
201 class="cm">// Preserve backwards compat. Validator.js now expects the second param to isURL and isEmail to be an object
202 if (typeof test === class="st">'object' && test !== null && test.msg) {
203 test = {
204 msg: test.msg
205 };
206 } else if (test === true) {
207 test = {};
208 }
209 }
210
211 class="cm">// Custom validators should always run, except if value is null and allowNull is false (see #9143)
212 if (typeof test === class="st">'function') {
213 validators.push(this._invokeCustomValidator(test, validatorType, true, value, field));
214 return;
215 }
216
217 class="cm">// If value is null, built-in validators should not run (only custom validators have to run) (see #9134).
218 if (value === null || value === undefined) {
219 return;
220 }
221
222 const validatorPromise = this._invokeBuiltinValidator(value, test, validatorType, field);
223 class="cm">// errors are handled in settling, stub this
224 validatorPromise.catch(() => {});
225 validators.push(validatorPromise);
226 });
227
228 return Promise
229 .all(validators.map(validator => validator.catch(rejection => {
230 const isBuiltIn = !!rejection.validatorName;
231 this._pushError(isBuiltIn, field, rejection, value, rejection.validatorName, rejection.validatorArgs);
232 })));
233 }
234
235 /**
236 * Prepare and invoke a custom validator.

Callers 1

Calls 4

_pushErrorMethod · 0.95
allMethod · 0.80

Tested by

no test coverage detected