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

Commit 05a5724

Browse files
author
Kent C. Dodds
committed
fix(types): Deprecate validateOptions
Because we have , we no longer need and it is not the recommended approach to validating your options anymore. closes #403
1 parent 25d640c commit 05a5724

File tree

4 files changed

+82
-56
lines changed

4 files changed

+82
-56
lines changed

other/ERRORS_AND_WARNINGS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ the apiCheck checkers are never even created. Not much we can do about the coupl
142142
a big issue. For more info, see [#334](https://github.com/formly-js/angular-formly/issues/334). Note, this will be
143143
removed in a major release.
144144

145+
# validateOptions deprecated
146+
147+
Because angular-formly already has a dependency on `api-check` and this is just a better way to validate your options,
148+
you should use this method instead. In an effort to simplify things. This has been deprecated in favor of the `apiCheck`
149+
property.
150+
145151
# skipNgModelAttrsManipulator moved
146152

147153
This property has been moved from the `data` property to the `extras` property.

src/directives/formly-field.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,16 @@ describe('formly-field', function() {
104104
describe('api check', () => {
105105
let validateOptions;
106106
beforeEach(() => {
107+
/* eslint no-console:0 */
108+
const originalWarn = console.warn;
109+
console.warn = () => {};
107110
validateOptions = sinon.spy();
108111
formlyConfig.setType({
109112
name: 'text', template: `<input name="{{id}}" ng-model="model[options.key]" />`,
110113
validateOptions
111114
});
112115
scope.model = {};
116+
console.warn = originalWarn;
113117
});
114118

115119
it('should throw an error when a field has extra properties', () => {

src/providers/formlyConfig.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function formlyConfig(formlyUsabilityProvider, formlyErrorAndWarningsUrlPrefix,
6161
prefix: 'formlyConfig.setType',
6262
url: 'settype-validation-failed'
6363
});
64-
checkApiCheck(options);
64+
checkDeprecatedOptions(options);
6565
if (!options.overwriteOk) {
6666
checkOverwrite(options.name, typeMap, options, 'types');
6767
} else {
@@ -241,7 +241,7 @@ function formlyConfig(formlyUsabilityProvider, formlyErrorAndWarningsUrlPrefix,
241241
if (options.template) {
242242
formlyUsabilityProvider.checkWrapperTemplate(options.template, options);
243243
}
244-
checkApiCheck(options);
244+
checkDeprecatedOptions(options);
245245
if (!options.overwriteOk) {
246246
checkOverwrite(options.name, templateWrappersMap, options, 'templateWrappers');
247247
} else {
@@ -267,11 +267,19 @@ function formlyConfig(formlyUsabilityProvider, formlyErrorAndWarningsUrlPrefix,
267267
}
268268
}
269269

270-
function checkApiCheck(options) {
270+
function checkDeprecatedOptions(options) {
271271
if (options.apiCheck && !angular.isFunction(options.apiCheck)) {
272272
warn(
273273
'apicheck-as-an-object-deprecated',
274-
'apiCheck as an object has been deprecated',
274+
'apiCheck as an object has been deprecated.',
275+
`Attempted for type: ${options.name}`,
276+
options
277+
);
278+
}
279+
if (options.validateOptions) {
280+
warn(
281+
'validateoptions-deprecated',
282+
'the `validateOptions` property has been deprecated.',
275283
`Attempted for type: ${options.name}`,
276284
options
277285
);

src/providers/formlyConfig.test.js

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -437,45 +437,70 @@ describe('formlyConfig', () => {
437437
childFn = sinon.spy();
438438
});
439439

440-
it(`should call the parent validateOptions function when there is no child function`, () => {
441-
setterFn([
442-
{name, template, validateOptions: parentFn},
443-
{name: 'type2', extends: name}
444-
]);
445-
getterFn('type2').validateOptions(options);
446-
expect(parentFn).to.have.been.calledWith(options);
440+
it(`should give a deprecation warning when specified as part of a type`, () => {
441+
shouldWarn(
442+
/Formly Warning: the `validateOptions` property has been deprecated. Attempted for type: foobar/,
443+
function() {
444+
setterFn({
445+
name: 'foobar',
446+
validateOptions() {}
447+
});
448+
}
449+
);
447450
});
448451

449-
it(`should call the child validateOptions function when there is no parent function`, () => {
450-
setterFn([
451-
{name, template},
452-
{name: 'type2', extends: name, validateOptions: childFn}
453-
]);
454-
getterFn('type2').validateOptions(options);
455-
expect(childFn).to.have.been.calledWith(options);
456-
});
452+
describe(`old functionality`, () => {
453+
let originalWarn;
454+
beforeEach(() => {
455+
originalWarn = console.warn;
456+
console.warn = () => {};
457+
});
457458

458-
it(`should call the child validateOptions function and the parent validateOptions function when they are both present`, () => {
459-
setterFn([
460-
{name, template, validateOptions: parentFn},
461-
{name: 'type2', extends: name, validateOptions: childFn}
462-
]);
463-
getterFn('type2').validateOptions(options);
464-
expect(childFn).to.have.been.calledWith(options);
465-
expect(parentFn).to.have.been.calledWith(options);
466-
});
459+
it(`should call the parent validateOptions function when there is no child function`, () => {
460+
setterFn([
461+
{name, template, validateOptions: parentFn},
462+
{name: 'type2', extends: name}
463+
]);
464+
getterFn('type2').validateOptions(options);
465+
expect(parentFn).to.have.been.calledWith(options);
466+
});
467467

468-
it(`should pass the result of the child's defaultOptions with the given options to the parent's validateOptions function`, () => {
469-
const defaultOptions = {data: {f: 'g'}};
470-
const combinedOptions = {data: {a: 'b', c: {d: 'e'}, f: 'g'}};
471-
setterFn([
472-
{name, template, validateOptions: parentFn},
473-
{name: 'type2', extends: name, validateOptions: childFn, defaultOptions}
474-
]);
475-
getterFn('type2').validateOptions(options);
476-
expect(childFn).to.have.been.calledWith(options);
477-
expect(parentFn).to.have.been.calledWith(combinedOptions);
468+
it(`should call the child validateOptions function when there is no parent function`, () => {
469+
setterFn([
470+
{name, template},
471+
{name: 'type2', extends: name, validateOptions: childFn}
472+
]);
473+
getterFn('type2').validateOptions(options);
474+
expect(childFn).to.have.been.calledWith(options);
475+
});
476+
477+
it(`should call the child validateOptions function and the parent validateOptions function when they are both present`, () => {
478+
setterFn([
479+
{name, template, validateOptions: parentFn},
480+
{name: 'type2', extends: name, validateOptions: childFn}
481+
]);
482+
getterFn('type2').validateOptions(options);
483+
expect(childFn).to.have.been.calledWith(options);
484+
expect(parentFn).to.have.been.calledWith(options);
485+
});
486+
487+
it(`should pass the result of the child's defaultOptions with the given options to the parent's validateOptions function`, () => {
488+
const defaultOptions = {data: {f: 'g'}};
489+
const combinedOptions = {data: {a: 'b', c: {d: 'e'}, f: 'g'}};
490+
setterFn([
491+
{name, template, validateOptions: parentFn},
492+
{name: 'type2', extends: name, validateOptions: childFn, defaultOptions}
493+
]);
494+
getterFn('type2').validateOptions(options);
495+
expect(childFn).to.have.been.calledWith(options);
496+
expect(parentFn).to.have.been.calledWith(combinedOptions);
497+
});
498+
499+
afterEach(() => {
500+
console.warn = originalWarn;
501+
});
478502
});
503+
479504
});
480505

481506
describe(`controller functions`, () => {
@@ -519,23 +544,6 @@ describe('formlyConfig', () => {
519544

520545
});
521546
});
522-
523-
describe(`validateOptions`, () => {
524-
it(`should allow you to specify this as a property of a type`, () => {
525-
expect(() => {
526-
setterFn({
527-
name,
528-
validateOptions,
529-
template
530-
});
531-
}).to.not.throw();
532-
expect(getterFn(name).validateOptions).to.be.a('function');
533-
534-
function validateOptions() {
535-
}
536-
});
537-
});
538-
539547
});
540548

541549
describe('(◞‸◟;) path', () => {
@@ -601,7 +609,7 @@ describe('formlyConfig', () => {
601609

602610
it(`should give a deprecation warning when providing apiCheck as an object rather than a function`, () => {
603611
shouldWarn(
604-
/Formly Warning: apiCheck as an object has been deprecated Attempted for type: input/,
612+
/Formly Warning: apiCheck as an object has been deprecated. Attempted for type: input/,
605613
function() {
606614
setterFn({
607615
name,

0 commit comments

Comments
 (0)