Skip to content

Commit 69ebab3

Browse files
committed
Merge branch 'feature/ni-NaN-ni-NaN-NaN-na' into development
2 parents ef421a4 + 5700261 commit 69ebab3

File tree

12 files changed

+228
-29
lines changed

12 files changed

+228
-29
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
@@ -275,11 +275,12 @@ <h3>Schema</h3>
275275
}).error(function() {
276276
$scope.loadedData = 'dummy';
277277
$scope.error = 'Failed to load gist.';
278+
$scope.selectedTest = $scope.tests[0];
278279
});
280+
} else {
281+
$scope.selectedTest = $scope.tests[0];
279282
}
280283

281-
$scope.selectedTest = $scope.tests[0];
282-
283284
$scope.$watch('selectedTest',function(val){
284285
if (val && val.data !== undefined) {
285286
$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
"gulp-umd": "^0.1.3",
@@ -50,6 +51,7 @@
5051
"karma-phantomjs-launcher": "^0.1.4",
5152
"mocha": "^1.18.0",
5253
"mocha-lcov-reporter": "0.0.1",
54+
"protractor": "^2.0.0",
5355
"sinon": "^1.9.0",
5456
"sinon-chai": "^2.5.0",
5557
"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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exports.config = {
2+
seleniumAddress: 'http://localhost:4444/wd/hub'
3+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
describe('Schema Form custom validators', function() {
2+
it('should have a form with content', function() {
3+
browser.get('http://localhost:8080/examples/custom-validators.html');
4+
5+
expect(element(by.css('form')).getInnerHtml()).not.toEqual('');
6+
});
7+
8+
describe('#name', function() {
9+
it('should not complain if it gets a normal name', function() {
10+
browser.get('http://localhost:8080/examples/custom-validators.html');
11+
var input = element.all(by.css('form input')).first();
12+
input.sendKeys('Joe Schmoe');
13+
14+
expect(input.getAttribute('value')).toEqual('Joe Schmoe');
15+
expect(input.evaluate('ngModel.$valid')).toEqual(true);
16+
17+
});
18+
19+
it('should complain if it gets a "Bob" as a name', function() {
20+
browser.get('http://localhost:8080/examples/custom-validators.html');
21+
var input = element.all(by.css('form input')).first();
22+
input.sendKeys('Bob');
23+
24+
expect(input.getAttribute('value')).toEqual('Bob');
25+
expect(input.evaluate('ngModel.$valid')).toEqual(false);
26+
});
27+
});
28+
29+
describe('#email', function() {
30+
it('should not complain if it gets a normal email', function() {
31+
browser.get('http://localhost:8080/examples/custom-validators.html');
32+
var input = element.all(by.css('form input')).get(1);
33+
input.sendKeys('foo@mailinator.com');
34+
35+
expect(input.getAttribute('value')).toEqual('foo@mailinator.com');
36+
expect(input.evaluate('ngModel.$valid')).toEqual(true);
37+
38+
});
39+
40+
it('should complain if it gets a my email', function() {
41+
browser.get('http://localhost:8080/examples/custom-validators.html');
42+
var input = element.all(by.css('form input')).get(1);
43+
input.sendKeys('david.lgj@gmail.com');
44+
45+
expect(input.getAttribute('value')).toEqual('david.lgj@gmail.com');
46+
expect(input.evaluate('ngModel.$valid')).toEqual(false);
47+
});
48+
});
49+
50+
describe('#comment', function() {
51+
it('should not complain if it gets a normal email', function() {
52+
browser.get('http://localhost:8080/examples/custom-validators.html');
53+
var input = element.all(by.css('form input')).get(1);
54+
input.sendKeys('foo@mailinator.com');
55+
56+
expect(input.getAttribute('value')).toEqual('foo@mailinator.com');
57+
expect(input.evaluate('ngModel.$valid')).toEqual(true);
58+
59+
});
60+
61+
it('should complain if it gets a my email', function() {
62+
browser.get('http://localhost:8080/examples/custom-validators.html');
63+
var input = element.all(by.css('form input')).get(1);
64+
input.sendKeys('david.lgj@gmail.com');
65+
66+
expect(input.getAttribute('value')).toEqual('david.lgj@gmail.com');
67+
expect(input.evaluate('ngModel.$valid')).toEqual(false);
68+
});
69+
});
70+
71+
72+
73+
74+
});

0 commit comments

Comments
 (0)