Skip to content

Commit f26a6a9

Browse files
Enable strict tsc compilation
1 parent 3b08867 commit f26a6a9

13 files changed

+79
-53
lines changed
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Directive, OnInit } from '@angular/core';
1+
import { Directive } from '@angular/core';
22
import { FormGroupDirective } from '@angular/forms';
33
import { Observable } from 'rxjs/Observable';
44

@@ -9,16 +9,13 @@ import { Observable } from 'rxjs/Observable';
99
/**
1010
* Encapsulates properties and events of the form and makes them available for child components.
1111
*/
12-
export class FormDirective implements OnInit {
12+
export class FormDirective {
1313
/**
1414
* Observable which emits when the form is submitted.
1515
*/
1616
submitted: Observable<{}>;
1717

18-
constructor(private formGroupDirective: FormGroupDirective) {
19-
}
20-
21-
ngOnInit() {
22-
this.submitted = this.formGroupDirective.ngSubmit.asObservable();
18+
constructor(formGroupDirective: FormGroupDirective) {
19+
this.submitted = formGroupDirective.ngSubmit.asObservable();
2320
}
2421
}

angular-reactive-validation/src/get-control-path.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ export function getControlPath(control: AbstractControl): string {
1414
path += '.';
1515
}
1616
return path + Object.keys(control.parent.controls).find(key => {
17-
return control.parent.controls[key] === control;
17+
const controls = control.parent.controls;
18+
if (Array.isArray(controls)) {
19+
return controls[Number(key)] === control;
20+
} else {
21+
return controls[key] === control;
22+
}
1823
});
1924
}
2025

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { FormGroup, FormControl, ControlContainer } from '@angular/forms';
22

3-
export function getFormControlFromContainer(name: string, controlContainer: ControlContainer): FormControl {
3+
export function getFormControlFromContainer(name: string, controlContainer: ControlContainer | undefined): FormControl {
44
if (controlContainer) {
55
const control = (<FormGroup>controlContainer.control).controls[name];
66
if (!control) {
77
throw new Error(`There is no control named '${name}'` +
8-
(controlContainer.path.length > 0 ? ` within '${controlContainer.path.join('.')}'` : '') + '.');
8+
(getPath(controlContainer).length > 0 ? ` within '${getPath(controlContainer).join('.')}'` : '') + '.');
99
}
1010
if (!(control instanceof FormControl)) {
1111
throw new Error(`The control named '${name}' ` +
12-
(controlContainer.path.length > 0 ? `within '${controlContainer.path.join('.')}' ` : '') +
12+
(getPath(controlContainer).length > 0 ? `within '${getPath(controlContainer).join('.')}' ` : '') +
1313
`is not a FormControl. Maybe you accidentally referenced a FormGroup or FormArray?`);
1414
}
1515

@@ -19,3 +19,7 @@ export function getFormControlFromContainer(name: string, controlContainer: Cont
1919
`arv-validation-messages element is not a child of an element with a formGroupName or formGroup declaration.`);
2020
}
2121
}
22+
23+
function getPath(controlContainer: ControlContainer): string[] {
24+
return controlContainer.path || [];
25+
}

angular-reactive-validation/src/observable-container.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ export class ObservableContainer<T> {
2222
* @param callOnSubscribe Determines if the nextFunction should be called immediately after subscribing, even
2323
* if the observable hasn't emitted anything. Defaults to false.
2424
*/
25-
subscribe<TObservable>(item: T, getObservable: (item: T) => Observable<TObservable>, callOnSubscribe?: boolean);
25+
subscribe<TObservable>(item: T, getObservable: (item: T) => Observable<TObservable>, callOnSubscribe?: boolean): void;
2626
/**
2727
* Subscribe the given items to the given Observable.
2828
* @param items The items which should be used when calling the nextFunction provided through the constructor.
2929
* @param getObservable Function that gets an Observable from the items provided.
3030
* @param callOnSubscribe Determines if the nextFunction should be called immediately after subscribing, even
3131
* if the observable hasn't emitted anything. Defaults to false.
3232
*/
33-
subscribe<TObservable>(items: T[], getObservable: (item: T) => Observable<TObservable>, callOnSubscribe?: boolean);
34-
subscribe<TObservable>(items: T | T[], getObservable: (item: T) => Observable<TObservable>, callOnSubscribe: boolean = false) {
33+
subscribe<TObservable>(items: T[], getObservable: (item: T) => Observable<TObservable>, callOnSubscribe?: boolean): void;
34+
subscribe<TObservable>(items: T | T[], getObservable: (item: T) => Observable<TObservable>, callOnSubscribe: boolean = false): void {
3535
if (!items) {
3636
return;
3737
}

angular-reactive-validation/src/validation-error.spec.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ describe('ValidationError', () => {
1111
};
1212
const error = ValidationError.fromFirstError(control);
1313

14-
expect(error.control).toEqual(control);
15-
expect(error.key).toEqual('required');
16-
expect(error.errorObject).toEqual(requiredErrorObject);
14+
expect(error).not.toBeUndefined();
15+
expect((<ValidationError>error).control).toEqual(control);
16+
expect((<ValidationError>error).key).toEqual('required');
17+
expect((<ValidationError>error).errorObject).toEqual(requiredErrorObject);
1718
});
1819

1920
it(`fromFirstError returns undefined when the FormControl has no errors`, () => {
@@ -34,7 +35,9 @@ describe('ValidationError', () => {
3435
}
3536
};
3637
const error = ValidationError.fromFirstError(control);
37-
expect(error.hasMessage()).toEqual(true);
38+
39+
expect(error).not.toBeUndefined();
40+
expect((<ValidationError>error).hasMessage()).toEqual(true);
3841
});
3942

4043
it(`hasMessage returns false when the errorObject doesn't contain a message`, () => {
@@ -44,7 +47,9 @@ describe('ValidationError', () => {
4447
}
4548
};
4649
const error = ValidationError.fromFirstError(control);
47-
expect(error.hasMessage()).toEqual(false);
50+
51+
expect(error).not.toBeUndefined();
52+
expect((<ValidationError>error).hasMessage()).toEqual(false);
4853
});
4954

5055
it(`getMessage returns the message from the errorObject`, () => {
@@ -57,6 +62,8 @@ describe('ValidationError', () => {
5762
}
5863
};
5964
const error = ValidationError.fromFirstError(control);
60-
expect(error.getMessage()).toEqual(expected);
65+
66+
expect(error).not.toBeUndefined();
67+
expect((<ValidationError>error).getMessage()).toEqual(expected);
6168
});
6269
});

angular-reactive-validation/src/validation-error.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ export class ValidationError {
55
key: string;
66
errorObject: ValidationErrors;
77

8+
constructor(control: FormControl, key: string, errorObject: ValidationErrors) {
9+
this.control = control;
10+
this.key = key;
11+
this.errorObject = errorObject;
12+
}
13+
814
static fromFirstError(control: FormControl): ValidationError | undefined {
915
if (!control.errors) {
1016
return undefined;
1117
}
1218

13-
const error = new ValidationError();
14-
error.control = control;
15-
error.key = Object.keys(control.errors)[0];
16-
error.errorObject = control.errors[Object.keys(control.errors)[0]];
17-
18-
return error;
19+
return new ValidationError(control, Object.keys(control.errors)[0], control.errors[Object.keys(control.errors)[0]]);
1920
}
2021

2122
hasMessage(): boolean {

angular-reactive-validation/src/validation-message/validation-message.component.spec.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { TestBed, ComponentFixture } from '@angular/core/testing';
22
import { ValidationMessageComponent } from './validation-message.component';
33
import { ValidationError } from '../validation-error';
44
import { Component, ViewChild } from '@angular/core';
5+
import { ControlContainer } from '@angular/forms';
56

67
describe('ValidationMessageComponent', () => {
78
describe('canHandle', () => {
@@ -15,8 +16,9 @@ describe('ValidationMessageComponent', () => {
1516
required: true
1617
}
1718
};
18-
component = new ValidationMessageComponent(undefined);
19-
error = ValidationError.fromFirstError(control);
19+
20+
component = new ValidationMessageComponent(<any>undefined);
21+
error = <ValidationError>ValidationError.fromFirstError(control);
2022
});
2123

2224
it(`returns true when the error key and component key are equal (without for)`, () => {
@@ -67,7 +69,7 @@ describe('ValidationMessageComponent', () => {
6769
});
6870

6971
it(`are displayed by the show function`, () => {
70-
const error = ValidationError.fromFirstError(TestHostComponent.getFormControl());
72+
const error = <ValidationError>ValidationError.fromFirstError(TestHostComponent.getFormControl());
7173

7274
validationMessageComponent.show(error);
7375

@@ -79,7 +81,7 @@ describe('ValidationMessageComponent', () => {
7981
});
8082

8183
it(`are hidden by the reset function`, () => {
82-
const error = ValidationError.fromFirstError(TestHostComponent.getFormControl());
84+
const error = <ValidationError>ValidationError.fromFirstError(TestHostComponent.getFormControl());
8385

8486
validationMessageComponent.show(error);
8587
fixture.detectChanges();
@@ -89,7 +91,7 @@ describe('ValidationMessageComponent', () => {
8991
});
9092

9193
it(`and their context is set by the show function`, () => {
92-
const error = ValidationError.fromFirstError(TestHostComponent.getFormControl());
94+
const error = <ValidationError>ValidationError.fromFirstError(TestHostComponent.getFormControl());
9395

9496
validationMessageComponent.show(error);
9597
expect(validationMessageComponent.context).toEqual(error.errorObject);

angular-reactive-validation/src/validation-message/validation-message.component.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import { getFormControlFromContainer } from '../get-form-control-from-container'
1717
* TODO: Trigger revalidation by parent whenever [for] changes.
1818
*/
1919
export class ValidationMessageComponent {
20-
private _context: ValidationErrors;
21-
private _for: FormControl;
20+
private _context: ValidationErrors | undefined;
21+
private _for: FormControl | undefined;
2222

2323
constructor(@Optional() private controlContainer: ControlContainer) { }
2424

@@ -27,18 +27,18 @@ export class ValidationMessageComponent {
2727
* The FormControl for which a custom validation message should be shown. This is only required when the parent
2828
* ValidationMessagesComponent has multiple FormControls specified.
2929
*/
30-
set for(control: FormControl | string) {
30+
set for(control: FormControl | string | undefined) {
3131
this._for = typeof control === 'string' ? getFormControlFromContainer(control, this.controlContainer) : control;
3232
}
33-
get for(): FormControl | string {
33+
get for(): FormControl | string | undefined {
3434
return this._for;
3535
}
3636

3737
@Input()
3838
/**
3939
* The name of the returned validation object property for which the custom validation message should be shown.
4040
*/
41-
key: string;
41+
key: string | undefined;
4242

4343
@Input()
4444
/**

angular-reactive-validation/src/validation-messages/validation-messages.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class ValidationMessagesComponent implements OnInit, AfterContentInit, On
9292
}
9393

9494
private getFirstErrorPerControl() {
95-
return this._for.filter(control => this.configuration && this.configuration.displayValidationMessageWhen ?
95+
return <ValidationError[]>this._for.filter(control => this.configuration && this.configuration.displayValidationMessageWhen ?
9696
this.configuration.displayValidationMessageWhen(control, this.formSubmitDirective ? this.formSubmitted : undefined) :
9797
control.touched || this.formSubmitted
9898
).map(ValidationError.fromFirstError).filter(value => value !== undefined);

angular-reactive-validation/src/validator-declaration.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,23 @@ export class ValidatorDeclaration {
6868

6969
private static validateAndSetMessageIfInvalid(control: AbstractControl,
7070
validatorFactoryFn: (...args: any[]) => ValidatorFn, resultKey: string,
71-
message?: string | ((...args: any[]) => string), ...args: any[]): ValidationErrors {
71+
message?: string | ((...args: any[]) => string), ...args: any[]): ValidationErrors | null {
7272

7373
const validationResult = ValidatorDeclaration.validate(control, validatorFactoryFn, ...args);
7474
ValidatorDeclaration.setMessageIfInvalid(control, resultKey, validationResult, message, ...args);
7575

7676
return validationResult;
7777
}
7878

79-
private static validate(control: AbstractControl, validatorFactoryFn: (...args: any[]) => ValidatorFn, ...args: any[]): ValidationErrors {
80-
const wrappedValidatorFn = validatorFactoryFn(...args);
81-
return wrappedValidatorFn(control);
79+
private static validate(control: AbstractControl, validatorFactoryFn: (...args: any[]) => ValidatorFn, ...args: any[]):
80+
ValidationErrors | null {
81+
82+
const wrappedValidatorFn = validatorFactoryFn(...args);
83+
return wrappedValidatorFn(control);
8284
}
8385

8486
private static setMessageIfInvalid(control: AbstractControl, resultKey: string,
85-
validationResult: ValidationErrors, message?: string | ((...args: any[]) => string), ...args: any[]) {
87+
validationResult: ValidationErrors | null, message?: string | ((...args: any[]) => string), ...args: any[]) {
8688
if (message) {
8789
if (validationResult && validationResult[resultKey]) {
8890
if (typeof message === 'function') {

0 commit comments

Comments
 (0)