Skip to content
This repository was archived by the owner on Apr 30, 2018. It is now read-only.

Commit 4813d63

Browse files
author
Kent C. Dodds
committed
feat(validation): Remove deprecated async magic ✨
You now must explicitely use the asyncValidators or validators as they should be used BREAKING CHANGE: If you were not heeding the deprecation warning before, you're going to need to move any async validation you have going on to the object
1 parent aaa767c commit 4813d63

File tree

3 files changed

+4
-100
lines changed

3 files changed

+4
-100
lines changed

other/ERRORS_AND_WARNINGS.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,6 @@ of the expression, the scope you're passed wont have all the properties you may
127127
See documentation [here](http://docs.angular-formly.com/docs/field-configuration-object#hideexpression-string--function)
128128
and an example [here](http://angular-formly.com/#/example/field-options/hide-fields)
129129

130-
# Validators returning promises should use asyncValidators
131-
132-
Due to some issues with treating all function validators as async validators, the functionality has been split into
133-
simply `validators` and `asyncValidators`. The ability to return a promise from a validator has been deprecated and you
134-
should use `asyncValidators` for those now. For more info, see
135-
[#369](https://github.com/formly-js/angular-formly/issues/369).
136-
137130
# apiCheck as an object deprecated
138131

139132
As a performance optimization, the `apiCheck` property has been changed to a function. This is good because when

src/directives/formly-custom-validation.js

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ import angular from 'angular-fix';
22
export default formlyCustomValidation;
33

44
// @ngInject
5-
function formlyCustomValidation(formlyConfig, formlyUtil, $q, formlyWarn) {
5+
function formlyCustomValidation(formlyUtil) {
66
return {
77
restrict: 'A',
88
require: 'ngModel',
99
link: function formlyCustomValidationLink(scope, el, attrs, ctrl) {
1010
const opts = scope.options;
11-
const warnedValidators = [];
1211
opts.validation.messages = opts.validation.messages || {};
1312
angular.forEach(opts.validation.messages, (message, key) => {
1413
opts.validation.messages[key] = () => {
@@ -41,45 +40,18 @@ function formlyCustomValidation(formlyConfig, formlyUtil, $q, formlyWarn) {
4140
}
4241

4342
function setupWithValidators(validator, name, isAsync) {
44-
const isPossiblyAsync = !angular.isString(validator);
45-
let validatorCollection = (isPossiblyAsync || isAsync) ? '$asyncValidators' : '$validators';
46-
47-
// UPDATE IN 7.0.0
48-
// this is temporary until we can have a breaking change. Allow people to get the wins of the explicitAsync api
49-
if (formlyConfig.extras.explicitAsync && !isAsync) {
50-
validatorCollection = '$validators';
51-
}
43+
const validatorCollection = isAsync ? '$asyncValidators' : '$validators';
5244

5345
ctrl[validatorCollection][name] = function evalValidity(modelValue, viewValue) {
54-
const value = formlyUtil.formlyEval(scope, validator, modelValue, viewValue);
55-
// UPDATE IN 7.0.0
56-
// In the next breaking change, this code should simply return the value
57-
if (isAsync) {
58-
return value;
59-
} else if (isPossiblyAsync && !formlyConfig.extras.explicitAsync) {
60-
if (isPromiseLike(value)) {
61-
logAsyncValidatorsDeprecationNotice(validator, opts);
62-
return value;
63-
} else {
64-
return value ? $q.when(value) : $q.reject(value);
65-
}
66-
} else {
67-
return value;
68-
}
46+
return formlyUtil.formlyEval(scope, validator, modelValue, viewValue);
6947
};
7048
}
7149

7250
function setupWithParsers(validator, name, isAsync) {
7351
let inFlightValidator;
7452
ctrl.$parsers.unshift(function evalValidityOfParser(viewValue) {
7553
const isValid = formlyUtil.formlyEval(scope, validator, ctrl.$modelValue, viewValue);
76-
// UPDATE IN 7.0.0
77-
// In the next breaking change, rather than checking for isPromiseLike, it should just check for isAsync.
78-
79-
if (isAsync || isPromiseLike(isValid)) {
80-
if (!isAsync) {
81-
logAsyncValidatorsDeprecationNotice(validator, opts);
82-
}
54+
if (isAsync) {
8355
ctrl.$pending = ctrl.$pending || {};
8456
ctrl.$pending[name] = true;
8557
inFlightValidator = isValid;
@@ -105,24 +77,6 @@ function formlyCustomValidation(formlyConfig, formlyUtil, $q, formlyWarn) {
10577
return viewValue;
10678
});
10779
}
108-
109-
function logAsyncValidatorsDeprecationNotice(validator, options) {
110-
if (warnedValidators.indexOf(validator) !== -1) {
111-
// we've warned about this one before. No spam necessary...
112-
return;
113-
}
114-
warnedValidators.push(validator);
115-
formlyWarn(
116-
'validators-returning-promises-should-use-asyncvalidators',
117-
'Validators returning promises should use asyncValidators instead of validators.',
118-
options
119-
);
120-
}
12180
}
12281
};
123-
124-
125-
function isPromiseLike(obj) {
126-
return obj && angular.isFunction(obj.then);
127-
}
12882
}

src/directives/formly-custom-validation.test.js

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,6 @@ describe(`formly-custom-validation`, function() {
3030
checkApi(formTemplate.replace(
3131
`TEMPLATE`, `<input ng-model="input" name="field" formly-custom-validation />`
3232
));
33-
34-
describe(`validators that are functions placement`, () => {
35-
it(`should be placed in $asyncValidators because it can return a promise`, () => {
36-
scope.options.validators.isHello = viewValue => viewValue === 'hello';
37-
$compile(
38-
`<form name="myForm"><input ng-model="input" name="field" formly-custom-validation /></form>`
39-
)(scope);
40-
scope.$digest();
41-
const field = scope.myForm.field;
42-
expect(field.$validators.isHello).to.not.exist;
43-
expect(field.$asyncValidators.isHello).to.exist;
44-
});
45-
46-
it(`should be placed in $validators if formlyConfig.extras.explicitAsync`, () => {
47-
formlyConfig.extras.explicitAsync = true;
48-
scope.options.validators.isHello = viewValue => viewValue === 'hello';
49-
$compile(
50-
`<form name="myForm"><input ng-model="input" name="field" formly-custom-validation /></form>`
51-
)(scope);
52-
scope.$digest();
53-
const field = scope.myForm.field;
54-
expect(field.$validators.isHello).to.exist;
55-
expect(field.$asyncValidators.isHello).to.not.exist;
56-
});
57-
58-
it(`should validate properly when explicitAsync is true`, () => {
59-
formlyConfig.extras.explicitAsync = true;
60-
const template = `<form name="myForm"><input ng-model="input" name="field" formly-custom-validation /></form>`;
61-
doValidation(template, 'hello', false, viewValue => viewValue !== 'hello', false);
62-
});
63-
});
6433
});
6534

6635
describe(`options.validation.messages`, () => {
@@ -100,18 +69,6 @@ describe(`formly-custom-validation`, function() {
10069
it(`should fail if it's a function that fails`, () => {
10170
validate(viewValue => viewValue !== value, false);
10271
});
103-
104-
it(`should warn if it's a function that returns a promise for a regular validator (should use asyncValidators instead)`, () => {
105-
const logArgs = [
106-
'Formly Warning:',
107-
'Validators returning promises should use asyncValidators instead of validators.',
108-
scope.options,
109-
/validators-returning-promises-should-use-asyncvalidators/
110-
];
111-
shouldWarnWithLog($log, logArgs, () => {
112-
validate(() => $q.when(), true);
113-
});
114-
});
11572
});
11673

11774
describe(`asyncValidators`, () => {

0 commit comments

Comments
 (0)