Skip to content

Commit 5700261

Browse files
committed
Refurbished validationmessages
Number fields got "Required" insted of "Value is not a number" since the input[type="number"] stopped the value being written to the model and we had higher precedence on tv4 error messages. This is no longer the case we show some ordinary angular validation messages as well. This fixes #322 Also fixed some small bugs regarding the error messages. Often `viewValue` is what you want in the error message. Plus a start with protractor.
1 parent dbb7d17 commit 5700261

File tree

12 files changed

+152
-31
lines changed

12 files changed

+152
-31
lines changed

.jscs.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"preset": "google",
3-
"maximumLineLength": 100
3+
"maximumLineLength": 100,
4+
"disallowMultipleLineBreaks": false
45
}

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ The context variables available to you are:
260260
| error | The error code |
261261
| title | Title of the field |
262262
| value | The model value |
263+
| viewValue | The view value (probably the one you want) |
263264
| form | form definition object for this field |
264265
| schema | schema for this field |
265266

examples/bootstrap-example.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,12 @@ <h3>Schema</h3>
273273
}).error(function() {
274274
$scope.loadedData = 'dummy';
275275
$scope.error = 'Failed to load gist.';
276+
$scope.selectedTest = $scope.tests[0];
276277
});
278+
} else {
279+
$scope.selectedTest = $scope.tests[0];
277280
}
278281

279-
$scope.selectedTest = $scope.tests[0];
280-
281282
$scope.$watch('selectedTest',function(val){
282283
if (val) {
283284
$http.get(val.data).then(function(res) {setNewData(res.data);});

gulp/tasks/protractor.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var gulp = require('gulp');
2+
3+
// The protractor task
4+
var protractor = require('gulp-protractor');
5+
6+
// Start a standalone server
7+
var webdriver_standalone = protractor.webdriver_standalone;
8+
9+
// Download and update the selenium driver
10+
var webdriver_update = protractor.webdriver_update;
11+
12+
// Downloads the selenium webdriver
13+
gulp.task('webdriver-update', webdriver_update);
14+
15+
// Start the standalone selenium server
16+
// NOTE: This is not needed if you reference the
17+
// seleniumServerJar in your protractor.conf.js
18+
gulp.task('webdriver-standalone', webdriver_standalone);
19+
20+
21+
// Setting up the test task
22+
gulp.task('protractor', ['webdriver-update'], function(cb) {
23+
gulp.src(['test/protractor/specs/**/*.js']).pipe(protractor.protractor({
24+
configFile: 'test/protractor/conf.js',
25+
})).on('error', function(e) {
26+
console.log(e);
27+
}).on('end', cb);
28+
});
29+
30+
['validation-messages', 'custom-validation'].forEach(function(name) {
31+
gulp.task('protractor:' + name, ['webdriver-update'], function(cb) {
32+
gulp.src(['test/protractor/specs/' + name + '.js']).pipe(protractor.protractor({
33+
configFile: 'test/protractor/conf.js',
34+
})).on('error', function(e) {
35+
console.log(e);
36+
}).on('end', cb);
37+
});
38+
});

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"gulp-concat": "^2.2.0",
3939
"gulp-jscs": "^1.1.0",
4040
"gulp-minify-html": "^0.1.1",
41+
"gulp-protractor": "^1.0.0",
4142
"gulp-rename": "^1.2.0",
4243
"gulp-uglify": "^0.2.1",
4344
"karma": "^0.12.0",
@@ -49,6 +50,7 @@
4950
"karma-phantomjs-launcher": "^0.1.4",
5051
"mocha": "^1.18.0",
5152
"mocha-lcov-reporter": "0.0.1",
53+
"protractor": "^2.0.0",
5254
"sinon": "^1.9.0",
5355
"sinon-chai": "^2.5.0",
5456
"streamqueue": "0.0.5"

src/directives/message.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,13 @@ angular.module('schemaForm').directive('sfMessage',
2727

2828
// We only show one error.
2929
// TODO: Make that optional
30-
// tv4- errors take precedence
3130
var error = errors[0];
32-
if (errors.length > 1) {
33-
34-
error = errors.reduce(function(prev, value) {
35-
if (prev && prev.indexOf('tv4-') === 0) {
36-
return prev;
37-
}
38-
return value;
39-
});
40-
console.log('reduced',errors, error)
41-
42-
}
4331

4432
if (error) {
4533
element.html(sfErrorMessage.interpolate(
4634
error,
4735
scope.ngModel.$modelValue,
36+
scope.ngModel.$viewValue,
4837
scope.form,
4938
scope.options && scope.options.validationMessage
5039
));

src/services/decorators.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ angular.module('schemaForm').provider('schemaFormDecorators',
159159
return sfErrorMessage.interpolate(
160160
(schemaError && schemaError.code + '') || 'default',
161161
(scope.ngModel && scope.ngModel.$modelValue) || '',
162+
(scope.ngModel && scope.ngModel.$viewValue) || '',
162163
scope.form,
163164
scope.options && scope.options.validationMessage
164165
);

src/services/errors.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@ angular.module('schemaForm').provider('sfErrorMessage', function() {
1414
12: 'Data is valid against more than one schema from "oneOf"',
1515
13: 'Data matches schema from "not"',
1616
// Numeric errors
17-
100: 'Value {{value}} is not a multiple of {{schema.multipleOf}}',
18-
101: 'Value {{value}} is less than minimum {{schema.minimum}}',
19-
102: 'Value {{value}} is equal to exclusive minimum {{schema.minimum}}',
20-
103: 'Value {{value}} is greater than maximum {{schema.maximum}}',
21-
104: 'Value {{value}} is equal to exclusive maximum {{schema.maximum}}',
22-
105: 'Value {{value}} is not a valid number',
17+
100: 'Value is not a multiple of {{schema.divisibleBy}}',
18+
101: '{{viewValue}} is less than the allowed minimum of {{schema.minimum}}',
19+
102: '{{viewValue}} is equal to the exclusive minimum {{schema.minimum}}',
20+
103: '{{viewValue}} is greater than the allowed maximum of {{schema.maximum}}',
21+
104: '{{viewValue}} is equal to the exclusive maximum {{schema.maximum}}',
22+
105: 'Value is not a valid number',
2323
// String errors
24-
200: 'String is too short ({{value.length}} chars), minimum {{schema.minimum}}',
25-
201: 'String is too long ({{value.length}} chars), maximum {{schema.maximum}}',
24+
200: 'String is too short ({{viewValue.length}} chars), minimum {{schema.minLength}}',
25+
201: 'String is too long ({{viewValue.length}} chars), maximum {{schema.maxLength}}',
2626
202: 'String does not match pattern: {{schema.pattern}}',
2727
// Object errors
28-
300: 'Too few properties defined, minimum {{schema.minimum}}',
29-
301: 'Too many properties defined, maximum {{schema.maximum}}',
28+
300: 'Too few properties defined, minimum {{schema.minProperties}}',
29+
301: 'Too many properties defined, maximum {{schema.maxProperties}}',
3030
302: 'Required',
3131
303: 'Additional properties not allowed',
3232
304: 'Dependency failed - key must exist',
3333
// Array errors
34-
400: 'Array is too short ({{value.length}}), minimum {{schema.minimum}}',
35-
401: 'Array is too long ({{value.length}}), maximum {{schema.maximum}}',
34+
400: 'Array is too short ({{value.length}}), minimum {{schema.maxItems}}',
35+
401: 'Array is too long ({{value.length}}), maximum {{schema.minItems}}',
3636
402: 'Array items are not unique',
3737
403: 'Additional items not allowed',
3838
// Format errors
@@ -44,6 +44,15 @@ angular.module('schemaForm').provider('sfErrorMessage', function() {
4444
1000: 'Unknown property (not in schema)'
4545
};
4646

47+
// In some cases we get hit with an angular validation error
48+
defaultMessages.number = defaultMessages[105];
49+
defaultMessages.required = defaultMessages[302];
50+
defaultMessages.min = defaultMessages[101];
51+
defaultMessages.max = defaultMessages[103];
52+
defaultMessages.maxlength = defaultMessages[201];
53+
defaultMessages.minlength = defaultMessages[200];
54+
defaultMessages.pattern = defaultMessages[202];
55+
4756
this.setDefaultMessages = function(messages) {
4857
defaultMessages = messages;
4958
};
@@ -68,12 +77,14 @@ angular.module('schemaForm').provider('sfErrorMessage', function() {
6877
* @param {string} error the error code, i.e. tv4-xxx for tv4 errors, otherwise it's whats on
6978
* ngModel.$error for custom errors.
7079
* @param {Any} value the actual model value.
80+
* @param {Any} viewValue the viewValue
7181
* @param {Object} form a form definition object for this field
7282
* @param {Object} global the global validation messages object (even though its called global
7383
* its actually just shared in one instance of sf-schema)
7484
* @return {string} The error message.
7585
*/
76-
service.interpolate = function(error, value, form, global) {
86+
service.interpolate = function(error, value, viewValue, form, global) {
87+
console.log(error, value, viewValue)
7788
global = global || {};
7889
var validationMessage = form.validationMessage || {};
7990

@@ -99,6 +110,7 @@ angular.module('schemaForm').provider('sfErrorMessage', function() {
99110
var context = {
100111
error: error,
101112
value: value,
113+
viewValue: viewValue,
102114
form: form,
103115
schema: form.schema,
104116
title: form.title || (form.schema && form.schema.title)

test/protractor/conf.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
exports.config = {
2-
seleniumAddress: 'http://localhost:4444/wd/hub',
3-
specs: ['custom-validation.js']
2+
seleniumAddress: 'http://localhost:4444/wd/hub'
43
}
File renamed without changes.

0 commit comments

Comments
 (0)