I created a custom validation for one of my models properties the following way:
Model.validateAsync('minOsVersion', validateMinimumOsVersion, {message: 'Minimum OS Version incorrect'});
function validateMinimumOsVersion(err, done) {
var requiredVersion = "some version";
var givenVersion = this.minOsVersion;
if (validator.validateSemanticVersionString(givenVersion, requiredVersion) < 0) {
err('too_low');
}
done();
}
This validation works fine, except the validation gets called multiple times (still no problem) but then adds the "minimum os version too low" error multiple times to the returned error object ob loopback.
Am i missing something or why is this error populated 12 times? if the validation for this specific case fails it should only be added once.
Any ideas? Thanks in advance.
Edit: As per request, here is the other code that gets called after my upload method
Model.observe('before save', function(ctx, next) {
if (ctx.instance) {
analyseMetadata(ctx.instance, function(error, model) {
if (error) { return next(error); }
next();
});
} else {
next();
}
});
function analyseMetadata(model, cb) {
extractMetadata(model, function(error, data){
if (error) { return cb(error); }
if (data && data.provisioning && data.metadata && data.entitlements) {
model.updateAttribute("expires", data.provisioning.ExpirationDate);
model.updateAttribute("created", data.provisioning.CreationDate);
model.updateAttribute("appIdentifier", data.entitlements['application-identifier']);
model.updateAttribute("teamIdentifier", data.entitlements['com.apple.developer.team-identifier']);
model.updateAttribute("bundleIdentifier", data.metadata.CFBundleIdentifier);
model.updateAttribute("displayName", data.metadata.CFBundleDisplayName);
model.updateAttribute("bundleName", data.metadata.CFBundleName);
model.updateAttribute("shortVersion", data.metadata.CFBundleShortVersionString);
model.updateAttribute("bundleVersion", data.metadata.CFBundleVersion);
model.updateAttribute("minOsVersion", data.metadata.MinimumOSVersion);
model.updateAttribute("builtOsVersion", data.metadata.DTPlatformVersion);
}
cb(null, model);
});
}
what would be a better way to update attributes while uploading but not generating the validations multiple times
As I mentioned in the comment, the reason you're getting multiple validation errors is that you're calling updateAttribute()
multiple times. Instead, consider using updateAttributes()
(plural form) which should only invoke the validation method(s) once:
function analyseMetadata(model, cb) {
extractMetadata(model, function(error, data){
if (error) { return cb(error); }
if (data && data.provisioning && data.metadata && data.entitlements) {
model.updateAttributes({
"expires": data.provisioning.ExpirationDate,
"created": data.provisioning.CreationDate,
"appIdentifier": data.entitlements['application-identifier'],
// ...
}, function updateCallback(updateErr, updatedModel) {
if (updateErr) { return cb(updateErr); }
cb(null, updatedModel);
});
}
});
}
Note also that I added the callback for updating attributes. This is an asynchronous function and actually performs the update, so you need to execute your cb()
callback after it is complete.