Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended validation details. #298

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions Src/knockout.validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,38 @@
});
};

result.getDetails = function() {
var details = [];

// ensure we have latest changes
var latest = result();

if (!latest.length) {
// don't do anything if no errors occured
return [];
}

ko.utils.arrayForEach(validatables(), function (observable) {
if (!observable.isValid()) {
var data = {}; // observable data for every rule

ko.utils.arrayForEach(observable.rules(), function(ctx) {
// allowing the caller to examine rule parameters for every rule
data[ctx.rule] = ctx.params;
});

details.push({
data: data,
observable: observable,
error: observable.error(),
rule: observable.failedRule()
});
}
});

return details;
};

obj.errors = result;
obj.isValid = function () {
return obj.errors().length === 0;
Expand Down Expand Up @@ -933,6 +965,16 @@

observable.isModified = ko.observable(false);

// holds the name of the validation rule that has failed during the last validation procedure
observable.failedRule = ko.observable(null);

observable.error.subscribe(function (v) {
if(!v) {
// clearing the failed rule name when validation message is empty
observable.failedRule(null);
}
});

// we use a computed here to ensure that anytime a dependency changes, the
// validation logic evaluates
var h_obsValidationTrigger = ko.computed(function () {
Expand Down Expand Up @@ -998,6 +1040,10 @@

//not valid, so format the error message and stick it in the 'error' variable
observable.error(exports.formatMessage(ctx.message || rule.message, ctx.params));

// remembering the name of the failed rule (passing it when "getDetails" is called)
observable.failedRule(ctx.rule);

observable.__valid__(false);
return false;
} else {
Expand Down Expand Up @@ -1031,6 +1077,10 @@
if (!isValid) {
//not valid, so format the error message and stick it in the 'error' variable
observable.error(exports.formatMessage(msg || ctx.message || rule.message, ctx.params));

// remembering the name of the failed rule (passing it when "getDetails" is called)
observable.failedRule(ctx.rule);

observable.__valid__(isValid);
}

Expand Down
81 changes: 81 additions & 0 deletions Tests/validation-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1350,3 +1350,84 @@ asyncTest('Async Rule Is NOT Valid Test', function () {
});

//#endregion

//#region Extended validation details
module('Extended validation details');

test('Basic required', function() {
var o = { required: true };

var vm = {
testObj: ko.observable().extend(o)
};

vm.errors = ko.validation.group(vm);

ok(!vm.testObj.isValid(), vm.testObj.error());

var details = vm.errors.getDetails();

ok(details && details.length == 1, 'Results are available');

equal(details[0].observable, vm.testObj, 'Result references the correct observable')
equal(details[0].rule, 'required', 'Result references the correct rule');
equal(details[0].error, vm.testObj.error(), 'Result contains the correct error message');

ok(details[0].data != null, 'Result contains data container');
equal(details[0].data[details[0].rule], o[details[0].rule], 'Result references the correct extension data');
});

test('Required with options', function() {
var o = { required: { fieldId: 'some_id' } };

var vm = {
testObj: ko.observable().extend(o)
};

vm.errors = ko.validation.group(vm);

ok(!vm.testObj.isValid(), vm.testObj.error());

var details = vm.errors.getDetails();

ok(details && details.length == 1, 'Results are available');

equal(details[0].observable, vm.testObj, 'Result references the correct observable')
equal(details[0].rule, 'required', 'Result references the correct rule');
equal(details[0].error, vm.testObj.error(), 'Result contains the correct error message');

ok(details[0].data != null, 'Result contains data container');
equal(details[0].data[details[0].rule], o[details[0].rule], 'Result references the correct extension data');
equal(details[0].data[details[0].rule].fieldId, 'some_id', 'Result\'s extension data is the same');
});

test('Multiple errors', function() {
var vm = {
testObj: ko.observable().extend({ required: true }),
anotherProp: ko.observable().extend({ required: true })
};

vm.errors = ko.validation.group(vm);

ok(!vm.testObj.isValid(), vm.testObj.error());

var details = vm.errors.getDetails();

ok(details && details.length == 2, 'Results are available');

var ensureResult = function (i, p) {
var observable = p();

equal(details[i].observable, observable, 'Result references the correct observable')
equal(details[i].rule, 'required', 'Result references the correct rule');
equal(details[i].error, observable.error(), 'Result contains the correct error message');

ok(details[i].data != null, 'Result contains data container');
equal(details[i].data[details[i].rule], true, 'Result contains the same values for extension data');
}

ensureResult(0, function () { return vm.testObj; });
ensureResult(1, function () { return vm.anotherProp; });
});

//#endregion