(cb)
| 48 | } |
| 49 | }; |
| 50 | var handleValidations = function (cb) { |
| 51 | var pending = [], errors = [], required, alwaysValidate; |
| 52 | |
| 53 | Hook.wait(instance, opts.hooks.beforeValidation, function (err) { |
| 54 | var k, i; |
| 55 | if (err) { |
| 56 | return saveError(cb, err); |
| 57 | } |
| 58 | |
| 59 | var checks = new enforce.Enforce({ |
| 60 | returnAllErrors : Model.settings.get("instance.returnAllErrors") |
| 61 | }); |
| 62 | |
| 63 | for (k in opts.validations) { |
| 64 | required = false; |
| 65 | |
| 66 | if (Model.allProperties[k]) { |
| 67 | required = Model.allProperties[k].required; |
| 68 | alwaysValidate = Model.allProperties[k].alwaysValidate; |
| 69 | } else { |
| 70 | for (i = 0; i < opts.one_associations.length; i++) { |
| 71 | if (opts.one_associations[i].field === k) { |
| 72 | required = opts.one_associations[i].required; |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | if (!alwaysValidate && !required && instance[k] == null) { |
| 78 | continue; // avoid validating if property is not required and is "empty" |
| 79 | } |
| 80 | for (i = 0; i < opts.validations[k].length; i++) { |
| 81 | checks.add(k, opts.validations[k][i]); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | checks.context("instance", instance); |
| 86 | checks.context("model", Model); |
| 87 | checks.context("driver", opts.driver); |
| 88 | |
| 89 | return checks.check(instance, cb); |
| 90 | }); |
| 91 | }; |
| 92 | var saveError = function (cb, err) { |
| 93 | instance_saving = false; |
| 94 |
no test coverage detected