├── .browserslistrc ├── .editorconfig ├── .gitignore ├── LICENSE ├── README.md ├── angular.json ├── e2e ├── protractor.conf.js ├── src │ ├── app.e2e-spec.ts │ └── app.po.ts └── tsconfig.json ├── karma.conf.js ├── package-lock.json ├── package.json ├── server.js ├── src ├── app │ ├── app.component.html │ ├── app.component.scss │ ├── app.component.spec.ts │ ├── app.component.ts │ ├── app.module.ts │ ├── directives │ │ ├── match-password.directive.spec.ts │ │ ├── match-password.directive.ts │ │ ├── password-pattern.directive.spec.ts │ │ ├── password-pattern.directive.ts │ │ ├── validate-user-name.directive.spec.ts │ │ └── validate-user-name.directive.ts │ ├── home │ │ ├── home.component.html │ │ ├── home.component.scss │ │ ├── home.component.spec.ts │ │ └── home.component.ts │ ├── models │ │ ├── user.ts │ │ └── userRegistration.ts │ ├── nav-bar │ │ ├── nav-bar.component.html │ │ ├── nav-bar.component.scss │ │ ├── nav-bar.component.spec.ts │ │ └── nav-bar.component.ts │ ├── reactive-form │ │ ├── reactive-form.component.html │ │ ├── reactive-form.component.scss │ │ ├── reactive-form.component.spec.ts │ │ └── reactive-form.component.ts │ ├── services │ │ ├── customvalidation.service.spec.ts │ │ ├── customvalidation.service.ts │ │ ├── user-name-validation.service.spec.ts │ │ └── user-name-validation.service.ts │ └── template-driven-form │ │ ├── template-driven-form.component.html │ │ ├── template-driven-form.component.scss │ │ ├── template-driven-form.component.spec.ts │ │ └── template-driven-form.component.ts ├── assets │ └── .gitkeep ├── environments │ ├── environment.prod.ts │ └── environment.ts ├── favicon.ico ├── index.html ├── main.ts ├── polyfills.ts ├── styles.scss └── test.ts ├── tsconfig.app.json ├── tsconfig.json ├── tsconfig.spec.json └── tslint.json /.browserslistrc: -------------------------------------------------------------------------------- 1 | # This file is used by the build system to adjust CSS and JS output to support the specified browsers below. 2 | # For additional information regarding the format and rule options, please see: 3 | # https://github.com/browserslist/browserslist#queries 4 | 5 | # You can see what browsers were selected by your queries by running: 6 | # npx browserslist 7 | 8 | > 0.5% 9 | last 2 versions 10 | Firefox ESR 11 | not dead 12 | not IE 9-11 # For IE 9-11 support, remove 'not'. -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see https://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | max_line_length = off 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | /out-tsc 7 | # Only exists if Bazel was run 8 | /bazel-out 9 | 10 | # dependencies 11 | /node_modules 12 | 13 | # profiling files 14 | chrome-profiler-events*.json 15 | speed-measure-plugin*.json 16 | 17 | # IDEs and editors 18 | /.idea 19 | .project 20 | .classpath 21 | .c9/ 22 | *.launch 23 | .settings/ 24 | *.sublime-workspace 25 | 26 | # IDE - VSCode 27 | .vscode/* 28 | !.vscode/settings.json 29 | !.vscode/tasks.json 30 | !.vscode/launch.json 31 | !.vscode/extensions.json 32 | .history/* 33 | 34 | # misc 35 | /.angular/cache 36 | /.sass-cache 37 | /connect.lock 38 | /coverage 39 | /libpeerconnection.log 40 | npm-debug.log 41 | yarn-error.log 42 | testem.log 43 | /typings 44 | 45 | # System Files 46 | .DS_Store 47 | Thumbs.db 48 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Ankit 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AngularFormsValidation 2 | 3 | This project explains about validations in Reactive and Template-driven forms in Angular. We will learn about the inbuilt as well as custom validations. 4 | 5 | # Article 6 | 7 | Read the articles at my blog 8 | - [Reactive Form Validation in Angular](https://ankitsharmablogs.com/reactive-form-validation-in-angular/) 9 | - [Template-Driven Form Validation In Angular](https://ankitsharmablogs.com/template-driven-form-validation-in-angular/) 10 | 11 | # Live Demo 12 | 13 | This app is hosted on Heroku. See the app in action at https://ng-forms-validation.web.app/ 14 | 15 | ## Development server 16 | 17 | Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files. 18 | 19 | ## Build 20 | 21 | Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build. 22 | 23 | -------------------------------------------------------------------------------- /angular.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "version": 1, 4 | "newProjectRoot": "projects", 5 | "projects": { 6 | "angular-forms-validation": { 7 | "projectType": "application", 8 | "schematics": { 9 | "@schematics/angular:component": { 10 | "style": "scss" 11 | } 12 | }, 13 | "root": "", 14 | "sourceRoot": "src", 15 | "prefix": "app", 16 | "architect": { 17 | "build": { 18 | "builder": "@angular-devkit/build-angular:browser", 19 | "options": { 20 | "outputPath": "dist/angular-forms-validation", 21 | "index": "src/index.html", 22 | "main": "src/main.ts", 23 | "polyfills": "src/polyfills.ts", 24 | "tsConfig": "tsconfig.app.json", 25 | "assets": [ 26 | "src/favicon.ico", 27 | "src/assets" 28 | ], 29 | "styles": [ 30 | "src/styles.scss" 31 | ], 32 | "scripts": [], 33 | "vendorChunk": true, 34 | "extractLicenses": false, 35 | "buildOptimizer": false, 36 | "sourceMap": true, 37 | "optimization": false, 38 | "namedChunks": true 39 | }, 40 | "configurations": { 41 | "production": { 42 | "fileReplacements": [ 43 | { 44 | "replace": "src/environments/environment.ts", 45 | "with": "src/environments/environment.prod.ts" 46 | } 47 | ], 48 | "optimization": true, 49 | "outputHashing": "all", 50 | "sourceMap": false, 51 | "namedChunks": false, 52 | "extractLicenses": true, 53 | "vendorChunk": false, 54 | "buildOptimizer": true, 55 | "budgets": [ 56 | { 57 | "type": "initial", 58 | "maximumWarning": "2mb", 59 | "maximumError": "5mb" 60 | }, 61 | { 62 | "type": "anyComponentStyle", 63 | "maximumWarning": "6kb", 64 | "maximumError": "10kb" 65 | } 66 | ] 67 | } 68 | }, 69 | "defaultConfiguration": "" 70 | }, 71 | "serve": { 72 | "builder": "@angular-devkit/build-angular:dev-server", 73 | "options": { 74 | "browserTarget": "angular-forms-validation:build" 75 | }, 76 | "configurations": { 77 | "production": { 78 | "browserTarget": "angular-forms-validation:build:production" 79 | } 80 | } 81 | }, 82 | "extract-i18n": { 83 | "builder": "@angular-devkit/build-angular:extract-i18n", 84 | "options": { 85 | "browserTarget": "angular-forms-validation:build" 86 | } 87 | }, 88 | "test": { 89 | "builder": "@angular-devkit/build-angular:karma", 90 | "options": { 91 | "main": "src/test.ts", 92 | "polyfills": "src/polyfills.ts", 93 | "tsConfig": "tsconfig.spec.json", 94 | "karmaConfig": "karma.conf.js", 95 | "assets": [ 96 | "src/favicon.ico", 97 | "src/assets" 98 | ], 99 | "styles": [ 100 | "src/styles.scss" 101 | ], 102 | "scripts": [] 103 | } 104 | }, 105 | "e2e": { 106 | "builder": "@angular-devkit/build-angular:protractor", 107 | "options": { 108 | "protractorConfig": "e2e/protractor.conf.js", 109 | "devServerTarget": "angular-forms-validation:serve" 110 | }, 111 | "configurations": { 112 | "production": { 113 | "devServerTarget": "angular-forms-validation:serve:production" 114 | } 115 | } 116 | } 117 | } 118 | } 119 | } 120 | } -------------------------------------------------------------------------------- /e2e/protractor.conf.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | // Protractor configuration file, see link for more information 3 | // https://github.com/angular/protractor/blob/master/lib/config.ts 4 | 5 | const { SpecReporter } = require('jasmine-spec-reporter'); 6 | 7 | /** 8 | * @type { import("protractor").Config } 9 | */ 10 | exports.config = { 11 | allScriptsTimeout: 11000, 12 | specs: [ 13 | './src/**/*.e2e-spec.ts' 14 | ], 15 | capabilities: { 16 | 'browserName': 'chrome' 17 | }, 18 | directConnect: true, 19 | baseUrl: 'http://localhost:4200/', 20 | framework: 'jasmine', 21 | jasmineNodeOpts: { 22 | showColors: true, 23 | defaultTimeoutInterval: 30000, 24 | print: function() {} 25 | }, 26 | onPrepare() { 27 | require('ts-node').register({ 28 | project: require('path').join(__dirname, './tsconfig.json') 29 | }); 30 | jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } })); 31 | } 32 | }; -------------------------------------------------------------------------------- /e2e/src/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { AppPage } from './app.po'; 2 | import { browser, logging } from 'protractor'; 3 | 4 | describe('workspace-project App', () => { 5 | let page: AppPage; 6 | 7 | beforeEach(() => { 8 | page = new AppPage(); 9 | }); 10 | 11 | it('should display welcome message', () => { 12 | page.navigateTo(); 13 | expect(page.getTitleText()).toEqual('angular-forms-validation app is running!'); 14 | }); 15 | 16 | afterEach(async () => { 17 | // Assert that there are no errors emitted from the browser 18 | const logs = await browser.manage().logs().get(logging.Type.BROWSER); 19 | expect(logs).not.toContain(jasmine.objectContaining({ 20 | level: logging.Level.SEVERE, 21 | } as logging.Entry)); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /e2e/src/app.po.ts: -------------------------------------------------------------------------------- 1 | import { browser, by, element } from 'protractor'; 2 | 3 | export class AppPage { 4 | navigateTo() { 5 | return browser.get(browser.baseUrl) as Promise; 6 | } 7 | 8 | getTitleText() { 9 | return element(by.css('app-root .content span')).getText() as Promise; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /e2e/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/e2e", 5 | "module": "commonjs", 6 | "target": "es2018", 7 | "types": [ 8 | "jasmine", 9 | "jasminewd2", 10 | "node" 11 | ] 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration file, see link for more information 2 | // https://karma-runner.github.io/1.0/config/configuration-file.html 3 | 4 | module.exports = function (config) { 5 | config.set({ 6 | basePath: '', 7 | frameworks: ['jasmine', '@angular-devkit/build-angular'], 8 | plugins: [ 9 | require('karma-jasmine'), 10 | require('karma-chrome-launcher'), 11 | require('karma-jasmine-html-reporter'), 12 | require('karma-coverage-istanbul-reporter'), 13 | require('@angular-devkit/build-angular/plugins/karma') 14 | ], 15 | client: { 16 | clearContext: false // leave Jasmine Spec Runner output visible in browser 17 | }, 18 | coverageIstanbulReporter: { 19 | dir: require('path').join(__dirname, './coverage/angular-forms-validation'), 20 | reports: ['html', 'lcovonly', 'text-summary'], 21 | fixWebpackSourcePaths: true 22 | }, 23 | reporters: ['progress', 'kjhtml'], 24 | port: 9876, 25 | colors: true, 26 | logLevel: config.LOG_INFO, 27 | autoWatch: true, 28 | browsers: ['Chrome'], 29 | singleRun: false, 30 | restartOnFileChange: true 31 | }); 32 | }; 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-forms-validation", 3 | "version": "0.0.0", 4 | "scripts": { 5 | "ng": "ng", 6 | "start": "node server.js", 7 | "build": "ng build", 8 | "test": "ng test", 9 | "lint": "ng lint", 10 | "e2e": "ng e2e", 11 | "postinstall": "ng build --aot --configuration production" 12 | }, 13 | "private": true, 14 | "dependencies": { 15 | "@angular/animations": "^14.2.7", 16 | "@angular/common": "^14.2.7", 17 | "@angular/compiler": "^14.2.7", 18 | "@angular/core": "^14.2.7", 19 | "@angular/forms": "^14.2.7", 20 | "@angular/platform-browser": "^14.2.7", 21 | "@angular/platform-browser-dynamic": "^14.2.7", 22 | "@angular/router": "^14.2.7", 23 | "bootstrap": "^5.2.2", 24 | "express": "^4.17.1", 25 | "rxjs": "~7.5.0", 26 | "tslib": "^2.3.0", 27 | "zone.js": "~0.11.4" 28 | }, 29 | "devDependencies": { 30 | "@angular-devkit/build-angular": "^14.2.6", 31 | "@angular/cli": "^14.2.6", 32 | "@angular/compiler-cli": "^14.2.7", 33 | "@angular/language-service": "^14.2.7", 34 | "@types/jasmine": "~3.6.0", 35 | "@types/jasminewd2": "~2.0.3", 36 | "@types/node": "^12.11.1", 37 | "codelyzer": "^6.0.0", 38 | "jasmine-core": "~3.6.0", 39 | "jasmine-spec-reporter": "~5.0.0", 40 | "karma": "~6.3.8", 41 | "karma-chrome-launcher": "~3.1.0", 42 | "karma-coverage-istanbul-reporter": "~3.0.2", 43 | "karma-jasmine": "~4.0.0", 44 | "karma-jasmine-html-reporter": "^1.5.0", 45 | "protractor": "~7.0.0", 46 | "ts-node": "~7.0.0", 47 | "tslint": "~6.1.0", 48 | "typescript": "~4.8.4" 49 | }, 50 | "engines": { 51 | "node": "12.13.0", 52 | "npm": "6.12.0" 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const path = require('path'); 3 | 4 | const ngApp = express(); 5 | 6 | ngApp.use(express.static('./dist/angular-forms-validation')); 7 | 8 | ngApp.get('/*', function (request, response) { 9 | response.sendFile(path.join(__dirname, '/dist/angular-forms-validation/index.html')); 10 | }); 11 | 12 | ngApp.listen(process.env.PORT || 8080); -------------------------------------------------------------------------------- /src/app/app.component.html: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 |
5 | -------------------------------------------------------------------------------- /src/app/app.component.scss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AnkitSharma-007/angular-forms-validation/d9b4799d89cdb97c72e6958d709124db4ab988c0/src/app/app.component.scss -------------------------------------------------------------------------------- /src/app/app.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed, waitForAsync } from '@angular/core/testing'; 2 | import { AppComponent } from './app.component'; 3 | 4 | describe('AppComponent', () => { 5 | beforeEach(waitForAsync(() => { 6 | TestBed.configureTestingModule({ 7 | declarations: [ 8 | AppComponent 9 | ], 10 | }).compileComponents(); 11 | })); 12 | 13 | it('should create the app', () => { 14 | const fixture = TestBed.createComponent(AppComponent); 15 | const app = fixture.debugElement.componentInstance; 16 | expect(app).toBeTruthy(); 17 | }); 18 | 19 | it(`should have as title 'angular-forms-validation'`, () => { 20 | const fixture = TestBed.createComponent(AppComponent); 21 | const app = fixture.debugElement.componentInstance; 22 | expect(app.title).toEqual('angular-forms-validation'); 23 | }); 24 | 25 | it('should render title', () => { 26 | const fixture = TestBed.createComponent(AppComponent); 27 | fixture.detectChanges(); 28 | const compiled = fixture.debugElement.nativeElement; 29 | expect(compiled.querySelector('.content span').textContent).toContain('angular-forms-validation app is running!'); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import { Component } from "@angular/core"; 2 | 3 | @Component({ 4 | selector: "app-root", 5 | templateUrl: "./app.component.html", 6 | styleUrls: ["./app.component.scss"], 7 | }) 8 | export class AppComponent {} 9 | -------------------------------------------------------------------------------- /src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { BrowserModule } from "@angular/platform-browser"; 2 | import { NgModule } from "@angular/core"; 3 | import { RouterModule } from "@angular/router"; 4 | import { FormsModule, ReactiveFormsModule } from "@angular/forms"; 5 | 6 | import { AppComponent } from "./app.component"; 7 | import { NavBarComponent } from "./nav-bar/nav-bar.component"; 8 | import { ReactiveFormComponent } from "./reactive-form/reactive-form.component"; 9 | import { TemplateDrivenFormComponent } from "./template-driven-form/template-driven-form.component"; 10 | import { HomeComponent } from "./home/home.component"; 11 | import { PasswordPatternDirective } from "./directives/password-pattern.directive"; 12 | import { MatchPasswordDirective } from "./directives/match-password.directive"; 13 | import { ValidateUserNameDirective } from "./directives/validate-user-name.directive"; 14 | 15 | @NgModule({ 16 | declarations: [ 17 | AppComponent, 18 | NavBarComponent, 19 | ReactiveFormComponent, 20 | TemplateDrivenFormComponent, 21 | HomeComponent, 22 | PasswordPatternDirective, 23 | MatchPasswordDirective, 24 | ValidateUserNameDirective, 25 | ], 26 | imports: [ 27 | FormsModule, 28 | ReactiveFormsModule, 29 | BrowserModule, 30 | RouterModule.forRoot([ 31 | { path: "", component: HomeComponent }, 32 | { path: "reactive-form", component: ReactiveFormComponent }, 33 | { 34 | path: "template-driven-form", 35 | component: TemplateDrivenFormComponent, 36 | }, 37 | ]), 38 | ], 39 | bootstrap: [AppComponent], 40 | }) 41 | export class AppModule {} 42 | -------------------------------------------------------------------------------- /src/app/directives/match-password.directive.spec.ts: -------------------------------------------------------------------------------- 1 | import { MatchPasswordDirective } from './match-password.directive'; 2 | 3 | describe('MatchPasswordDirective', () => { 4 | it('should create an instance', () => { 5 | const directive = new MatchPasswordDirective(); 6 | expect(directive).toBeTruthy(); 7 | }); 8 | }); 9 | -------------------------------------------------------------------------------- /src/app/directives/match-password.directive.ts: -------------------------------------------------------------------------------- 1 | import { Directive, Input } from "@angular/core"; 2 | import { 3 | NG_VALIDATORS, 4 | Validator, 5 | ValidationErrors, 6 | AbstractControl, 7 | } from "@angular/forms"; 8 | import { CustomvalidationService } from "../services/customvalidation.service"; 9 | 10 | @Directive({ 11 | selector: "[appMatchPassword]", 12 | providers: [ 13 | { 14 | provide: NG_VALIDATORS, 15 | useExisting: MatchPasswordDirective, 16 | multi: true, 17 | }, 18 | ], 19 | }) 20 | export class MatchPasswordDirective implements Validator { 21 | @Input("appMatchPassword") MatchPassword: string[] = []; 22 | 23 | constructor(private readonly customValidator: CustomvalidationService) {} 24 | 25 | validate(control: AbstractControl): ValidationErrors { 26 | return this.customValidator.matchPassword( 27 | this.MatchPassword[0], 28 | this.MatchPassword[1] 29 | )(control); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/app/directives/password-pattern.directive.spec.ts: -------------------------------------------------------------------------------- 1 | import { PasswordPatternDirective } from './password-pattern.directive'; 2 | 3 | describe('PasswordPatternDirective', () => { 4 | it('should create an instance', () => { 5 | const directive = new PasswordPatternDirective(); 6 | expect(directive).toBeTruthy(); 7 | }); 8 | }); 9 | -------------------------------------------------------------------------------- /src/app/directives/password-pattern.directive.ts: -------------------------------------------------------------------------------- 1 | import { Directive } from "@angular/core"; 2 | import { NG_VALIDATORS, Validator, AbstractControl } from "@angular/forms"; 3 | import { CustomvalidationService } from "../services/customvalidation.service"; 4 | 5 | @Directive({ 6 | selector: "[appPasswordPattern]", 7 | providers: [ 8 | { 9 | provide: NG_VALIDATORS, 10 | useExisting: PasswordPatternDirective, 11 | multi: true, 12 | }, 13 | ], 14 | }) 15 | export class PasswordPatternDirective implements Validator { 16 | constructor(private readonly customValidator: CustomvalidationService) {} 17 | 18 | validate(control: AbstractControl): { [key: string]: any } | null { 19 | return this.customValidator.patternValidator()(control); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/app/directives/validate-user-name.directive.spec.ts: -------------------------------------------------------------------------------- 1 | import { ValidateUserNameDirective } from './validate-user-name.directive'; 2 | 3 | describe('ValidateUserNameDirective', () => { 4 | it('should create an instance', () => { 5 | const directive = new ValidateUserNameDirective(); 6 | expect(directive).toBeTruthy(); 7 | }); 8 | }); 9 | -------------------------------------------------------------------------------- /src/app/directives/validate-user-name.directive.ts: -------------------------------------------------------------------------------- 1 | import { Directive, forwardRef } from "@angular/core"; 2 | import { 3 | AbstractControl, 4 | NG_ASYNC_VALIDATORS, 5 | ValidationErrors, 6 | AsyncValidator, 7 | } from "@angular/forms"; 8 | import { Observable } from "rxjs"; 9 | import { UserNameValidationService } from "../services/user-name-validation.service"; 10 | 11 | @Directive({ 12 | selector: "[appValidateUserName]", 13 | providers: [ 14 | { 15 | provide: NG_ASYNC_VALIDATORS, 16 | useExisting: forwardRef(() => ValidateUserNameDirective), 17 | multi: true, 18 | }, 19 | ], 20 | }) 21 | export class ValidateUserNameDirective implements AsyncValidator { 22 | constructor( 23 | private readonly userNameValidationService: UserNameValidationService 24 | ) {} 25 | 26 | validate(control: AbstractControl): Observable { 27 | return this.userNameValidationService.validate(control); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/app/home/home.component.html: -------------------------------------------------------------------------------- 1 |
2 |

Welcome to Angular Forms demo

3 |
4 | -------------------------------------------------------------------------------- /src/app/home/home.component.scss: -------------------------------------------------------------------------------- 1 | .title-home { 2 | height: 400px; 3 | } 4 | -------------------------------------------------------------------------------- /src/app/home/home.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; 2 | 3 | import { HomeComponent } from './home.component'; 4 | 5 | describe('HomeComponent', () => { 6 | let component: HomeComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(waitForAsync(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ HomeComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(HomeComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/home/home.component.ts: -------------------------------------------------------------------------------- 1 | import { Component } from "@angular/core"; 2 | 3 | @Component({ 4 | selector: "app-home", 5 | templateUrl: "./home.component.html", 6 | styleUrls: ["./home.component.scss"], 7 | }) 8 | export class HomeComponent {} 9 | -------------------------------------------------------------------------------- /src/app/models/user.ts: -------------------------------------------------------------------------------- 1 | export class User { 2 | public name: string; 3 | public email: string; 4 | public username: string; 5 | public password: string; 6 | public confirmPassword: string; 7 | } 8 | -------------------------------------------------------------------------------- /src/app/models/userRegistration.ts: -------------------------------------------------------------------------------- 1 | import { FormControl } from "@angular/forms"; 2 | 3 | export interface UserRegistration { 4 | name: FormControl; 5 | email: FormControl; 6 | username: FormControl; 7 | password: FormControl; 8 | confirmPassword: FormControl; 9 | } 10 | -------------------------------------------------------------------------------- /src/app/nav-bar/nav-bar.component.html: -------------------------------------------------------------------------------- 1 | 29 | -------------------------------------------------------------------------------- /src/app/nav-bar/nav-bar.component.scss: -------------------------------------------------------------------------------- 1 | .navbar { 2 | background-color: #3f51b5; 3 | } 4 | 5 | .spacer { 6 | flex-grow: 1; 7 | } 8 | -------------------------------------------------------------------------------- /src/app/nav-bar/nav-bar.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { ComponentFixture, TestBed, waitForAsync } from "@angular/core/testing"; 2 | 3 | import { NavBarComponent } from "./nav-bar.component"; 4 | 5 | describe("NavBarComponent", () => { 6 | let component: NavBarComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(waitForAsync(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [NavBarComponent], 12 | }).compileComponents(); 13 | })); 14 | 15 | beforeEach(() => { 16 | fixture = TestBed.createComponent(NavBarComponent); 17 | component = fixture.componentInstance; 18 | fixture.detectChanges(); 19 | }); 20 | 21 | it("should create", () => { 22 | expect(component).toBeTruthy(); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /src/app/nav-bar/nav-bar.component.ts: -------------------------------------------------------------------------------- 1 | import { Component } from "@angular/core"; 2 | 3 | @Component({ 4 | selector: "app-nav-bar", 5 | templateUrl: "./nav-bar.component.html", 6 | styleUrls: ["./nav-bar.component.scss"], 7 | }) 8 | export class NavBarComponent {} 9 | -------------------------------------------------------------------------------- /src/app/reactive-form/reactive-form.component.html: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 |
5 |

Reactive Form

6 |
7 |
8 |
13 |
14 | 15 | 16 | 22 | Name is required 23 | 24 |
25 |
26 | 27 | 28 | 34 | Email is required 35 | 36 | 42 | Enter a valid email address 43 | 44 |
45 |
46 | 47 | 52 | 58 | User Name is required 59 | 60 | 67 | User Name is not available 68 | 69 |
70 |
71 | 72 | 77 | 83 | Password is required 84 | 85 | 91 | Password should have minimum 8 characters, at least 1 uppercase 92 | letter, 1 lowercase letter and 1 number 93 | 94 |
95 |
96 | 99 | 104 | 110 | Confirm Password is required 111 | 112 | 118 | Passwords doesnot match 119 | 120 |
121 |
122 | 123 | 130 |
131 |
132 |
133 |
134 |
135 |
136 | -------------------------------------------------------------------------------- /src/app/reactive-form/reactive-form.component.scss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AnkitSharma-007/angular-forms-validation/d9b4799d89cdb97c72e6958d709124db4ab988c0/src/app/reactive-form/reactive-form.component.scss -------------------------------------------------------------------------------- /src/app/reactive-form/reactive-form.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; 2 | 3 | import { ReactiveFormComponent } from './reactive-form.component'; 4 | 5 | describe('ReactiveFormComponent', () => { 6 | let component: ReactiveFormComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(waitForAsync(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ ReactiveFormComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(ReactiveFormComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/reactive-form/reactive-form.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from "@angular/core"; 2 | import { 3 | Validators, 4 | FormGroup, 5 | NonNullableFormBuilder, 6 | FormControl, 7 | } from "@angular/forms"; 8 | import { UserRegistration } from "../models/userRegistration"; 9 | import { CustomvalidationService } from "../services/customvalidation.service"; 10 | import { UserNameValidationService } from "../services/user-name-validation.service"; 11 | 12 | @Component({ 13 | selector: "app-reactive-form", 14 | templateUrl: "./reactive-form.component.html", 15 | styleUrls: ["./reactive-form.component.scss"], 16 | }) 17 | export class ReactiveFormComponent implements OnInit { 18 | protected registerForm!: FormGroup; 19 | protected submitted = false; 20 | 21 | constructor( 22 | private readonly formBuilder: NonNullableFormBuilder, 23 | private readonly customValidator: CustomvalidationService, 24 | private readonly userNameValidationService: UserNameValidationService 25 | ) {} 26 | 27 | ngOnInit(): void { 28 | this.registerForm = this.formBuilder.group( 29 | { 30 | name: new FormControl("", Validators.required), 31 | email: new FormControl("", [Validators.required, Validators.email]), 32 | username: new FormControl("", { 33 | asyncValidators: [ 34 | this.userNameValidationService.validate.bind( 35 | this.userNameValidationService 36 | ), 37 | ], 38 | validators: [Validators.required], 39 | }), 40 | password: new FormControl("", [ 41 | Validators.required, 42 | this.customValidator.patternValidator(), 43 | ]), 44 | confirmPassword: new FormControl("", [Validators.required]), 45 | }, 46 | { 47 | validators: [ 48 | this.customValidator.matchPassword("password", "confirmPassword"), 49 | ], 50 | } 51 | ); 52 | } 53 | 54 | protected get registerFormControl() { 55 | return this.registerForm.controls; 56 | } 57 | 58 | protected onSubmit(): void { 59 | this.submitted = true; 60 | 61 | if (this.registerForm.valid) { 62 | alert( 63 | "Form Submitted succesfully!!!\n Check the values in browser console." 64 | ); 65 | console.table(this.registerForm.value); 66 | } 67 | } 68 | 69 | protected resetForm(): void { 70 | this.registerForm.reset(); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/app/services/customvalidation.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed } from '@angular/core/testing'; 2 | 3 | import { CustomvalidationService } from './customvalidation.service'; 4 | 5 | describe('CustomvalidationService', () => { 6 | beforeEach(() => TestBed.configureTestingModule({})); 7 | 8 | it('should be created', () => { 9 | const service: CustomvalidationService = TestBed.get(CustomvalidationService); 10 | expect(service).toBeTruthy(); 11 | }); 12 | }); 13 | -------------------------------------------------------------------------------- /src/app/services/customvalidation.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from "@angular/core"; 2 | import { ValidatorFn, AbstractControl, ValidationErrors } from "@angular/forms"; 3 | 4 | @Injectable({ 5 | providedIn: "root", 6 | }) 7 | export class CustomvalidationService { 8 | patternValidator(): ValidatorFn { 9 | return (control: AbstractControl): { [key: string]: any } => { 10 | if (!control.value) { 11 | return null; 12 | } 13 | const regex = new RegExp("^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{8,}$"); 14 | const valid = regex.test(control.value); 15 | return valid ? null : { invalidPassword: true }; 16 | }; 17 | } 18 | 19 | matchPassword(password: string, confirmPassword: string) { 20 | return (formGroup: AbstractControl): ValidationErrors | null => { 21 | const passwordControl = formGroup.get(password); 22 | const confirmPasswordControl = formGroup.get(confirmPassword); 23 | 24 | if (!passwordControl || !confirmPasswordControl) { 25 | return null; 26 | } 27 | 28 | if ( 29 | confirmPasswordControl.errors && 30 | !confirmPasswordControl.errors["passwordMismatch"] 31 | ) { 32 | return null; 33 | } 34 | 35 | if (passwordControl.value !== confirmPasswordControl.value) { 36 | confirmPasswordControl.setErrors({ passwordMismatch: true }); 37 | return { passwordMismatch: true }; 38 | } else { 39 | confirmPasswordControl.setErrors(null); 40 | return null; 41 | } 42 | }; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/app/services/user-name-validation.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed } from '@angular/core/testing'; 2 | 3 | import { UserNameValidationService } from './user-name-validation.service'; 4 | 5 | describe('UserNameValidationService', () => { 6 | let service: UserNameValidationService; 7 | 8 | beforeEach(() => { 9 | TestBed.configureTestingModule({}); 10 | service = TestBed.inject(UserNameValidationService); 11 | }); 12 | 13 | it('should be created', () => { 14 | expect(service).toBeTruthy(); 15 | }); 16 | }); 17 | -------------------------------------------------------------------------------- /src/app/services/user-name-validation.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from "@angular/core"; 2 | import { 3 | AbstractControl, 4 | AsyncValidator, 5 | ValidationErrors, 6 | } from "@angular/forms"; 7 | import { catchError, delay, map, Observable, of } from "rxjs"; 8 | 9 | @Injectable({ 10 | providedIn: "root", 11 | }) 12 | export class UserNameValidationService implements AsyncValidator { 13 | constructor() {} 14 | 15 | validate(control: AbstractControl): Observable { 16 | return this.validateUserName(control.value).pipe( 17 | map((duplicateUserAvailable) => { 18 | if (duplicateUserAvailable) { 19 | return { userNameNotAvailable: true }; 20 | } else { 21 | return null; 22 | } 23 | }), 24 | catchError(() => of(null)) 25 | ); 26 | } 27 | 28 | /* A static array is used to validate the availability of user names. 29 | * Ideally it should be a service call to the server to search the value from a database. 30 | */ 31 | private validateUserName(username: string): Observable { 32 | const UserList = ["ankit", "admin", "user", "superuser"]; 33 | return of(UserList.includes(username.toLocaleLowerCase())).pipe( 34 | delay(1000) 35 | ); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/app/template-driven-form/template-driven-form.component.html: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 |
5 |

Template-driven Form

6 |
7 |
8 |
16 |
17 | 18 | 26 | 33 | Name is required 34 | 35 |
36 |
37 | 38 | 47 | 54 | Email is required 55 | 56 | 60 | Enter a valid email address 61 | 62 |
63 |
64 | 65 | 74 | 81 | User Name is required 82 | 83 | 87 | User Name not available 88 | 89 |
90 |
91 | 92 | 101 | 108 | Password is required 109 | 110 | 114 | Password should have minimum 8 characters, at least 1 uppercase 115 | letter, 1 lowercase letter and 1 number 116 | 117 |
118 |
119 | 122 | 130 | 137 | Confirm Password is required 138 | 139 | 146 | Passwords doesnot match 147 | 148 |
149 |
150 | 151 | 158 |
159 |
160 |
161 |
162 |
163 |
164 | -------------------------------------------------------------------------------- /src/app/template-driven-form/template-driven-form.component.scss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AnkitSharma-007/angular-forms-validation/d9b4799d89cdb97c72e6958d709124db4ab988c0/src/app/template-driven-form/template-driven-form.component.scss -------------------------------------------------------------------------------- /src/app/template-driven-form/template-driven-form.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; 2 | 3 | import { TemplateDrivenFormComponent } from './template-driven-form.component'; 4 | 5 | describe('TemplateDrivenFormComponent', () => { 6 | let component: TemplateDrivenFormComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(waitForAsync(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ TemplateDrivenFormComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(TemplateDrivenFormComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/template-driven-form/template-driven-form.component.ts: -------------------------------------------------------------------------------- 1 | import { Component } from "@angular/core"; 2 | import { User } from "../models/user"; 3 | 4 | @Component({ 5 | selector: "app-template-driven-form", 6 | templateUrl: "./template-driven-form.component.html", 7 | styleUrls: ["./template-driven-form.component.scss"], 8 | }) 9 | export class TemplateDrivenFormComponent { 10 | protected userModal = new User(); 11 | 12 | protected onSubmit() { 13 | alert( 14 | "Form Submitted succesfully!!!\n Check the values in browser console." 15 | ); 16 | console.table(this.userModal); 17 | } 18 | 19 | protected resetForm(): void { 20 | this.userModal = new User(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AnkitSharma-007/angular-forms-validation/d9b4799d89cdb97c72e6958d709124db4ab988c0/src/assets/.gitkeep -------------------------------------------------------------------------------- /src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true 3 | }; 4 | -------------------------------------------------------------------------------- /src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | // This file can be replaced during build by using the `fileReplacements` array. 2 | // `ng build --prod` replaces `environment.ts` with `environment.prod.ts`. 3 | // The list of file replacements can be found in `angular.json`. 4 | 5 | export const environment = { 6 | production: false 7 | }; 8 | 9 | /* 10 | * For easier debugging in development mode, you can import the following file 11 | * to ignore zone related error stack frames such as `zone.run`, `zoneDelegate.invokeTask`. 12 | * 13 | * This import should be commented out in production mode because it will have a negative impact 14 | * on performance if an error is thrown. 15 | */ 16 | // import 'zone.js/plugins/zone-error'; // Included with Angular CLI. 17 | -------------------------------------------------------------------------------- /src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AnkitSharma-007/angular-forms-validation/d9b4799d89cdb97c72e6958d709124db4ab988c0/src/favicon.ico -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | AngularFormsValidation 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { enableProdMode } from '@angular/core'; 2 | import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 3 | 4 | import { AppModule } from './app/app.module'; 5 | import { environment } from './environments/environment'; 6 | 7 | if (environment.production) { 8 | enableProdMode(); 9 | } 10 | 11 | platformBrowserDynamic().bootstrapModule(AppModule) 12 | .catch(err => console.error(err)); 13 | -------------------------------------------------------------------------------- /src/polyfills.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This file includes polyfills needed by Angular and is loaded before the app. 3 | * You can add your own extra polyfills to this file. 4 | * 5 | * This file is divided into 2 sections: 6 | * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. 7 | * 2. Application imports. Files imported after ZoneJS that should be loaded before your main 8 | * file. 9 | * 10 | * The current setup is for so-called "evergreen" browsers; the last versions of browsers that 11 | * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), 12 | * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. 13 | * 14 | * Learn more in https://angular.io/guide/browser-support 15 | */ 16 | 17 | /*************************************************************************************************** 18 | * BROWSER POLYFILLS 19 | */ 20 | 21 | /** 22 | * By default, zone.js will patch all possible macroTask and DomEvents 23 | * user can disable parts of macroTask/DomEvents patch by setting following flags 24 | * because those flags need to be set before `zone.js` being loaded, and webpack 25 | * will put import in the top of bundle, so user need to create a separate file 26 | * in this directory (for example: zone-flags.ts), and put the following flags 27 | * into that file, and then add the following code before importing zone.js. 28 | * import './zone-flags.ts'; 29 | * 30 | * The flags allowed in zone-flags.ts are listed here. 31 | * 32 | * The following flags will work for all browsers. 33 | * 34 | * (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame 35 | * (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick 36 | * (window as any).__zone_symbol__UNPATCHED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames 37 | * 38 | * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js 39 | * with the following flag, it will bypass `zone.js` patch for IE/Edge 40 | * 41 | * (window as any).__Zone_enable_cross_context_check = true; 42 | * 43 | */ 44 | 45 | /*************************************************************************************************** 46 | * Zone JS is required by default for Angular itself. 47 | */ 48 | import 'zone.js'; // Included with Angular CLI. 49 | 50 | 51 | /*************************************************************************************************** 52 | * APPLICATION IMPORTS 53 | */ 54 | -------------------------------------------------------------------------------- /src/styles.scss: -------------------------------------------------------------------------------- 1 | /* You can add global styles to this file, and also import other style files */ 2 | 3 | @import "~bootstrap/dist/css/bootstrap.css"; 4 | 5 | body { 6 | background-color: #fafafa; 7 | font-size: 16px; 8 | font-family: Roboto, "Helvetica Neue", sans-serif; 9 | line-height: 1.5; 10 | font-kerning: normal; 11 | } 12 | -------------------------------------------------------------------------------- /src/test.ts: -------------------------------------------------------------------------------- 1 | // This file is required by karma.conf.js and loads recursively all the .spec and framework files 2 | 3 | import 'zone.js/testing'; 4 | import { getTestBed } from '@angular/core/testing'; 5 | import { 6 | BrowserDynamicTestingModule, 7 | platformBrowserDynamicTesting 8 | } from '@angular/platform-browser-dynamic/testing'; 9 | 10 | declare const require: any; 11 | 12 | // First, initialize the Angular testing environment. 13 | getTestBed().initTestEnvironment( 14 | BrowserDynamicTestingModule, 15 | platformBrowserDynamicTesting(), { 16 | teardown: { destroyAfterEach: false } 17 | } 18 | ); 19 | // Then we find all the tests. 20 | const context = require.context('./', true, /\.spec\.ts$/); 21 | // And load the modules. 22 | context.keys().map(context); 23 | -------------------------------------------------------------------------------- /tsconfig.app.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "./out-tsc/app", 5 | "types": [] 6 | }, 7 | "files": [ 8 | "src/main.ts", 9 | "src/polyfills.ts" 10 | ], 11 | "include": [ 12 | "src/**/*.d.ts" 13 | ], 14 | "exclude": [ 15 | "src/test.ts", 16 | "src/**/*.spec.ts" 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "baseUrl": "./", 5 | "outDir": "./dist/out-tsc", 6 | "sourceMap": true, 7 | "declaration": false, 8 | "downlevelIteration": true, 9 | "experimentalDecorators": true, 10 | "module": "es2020", 11 | "moduleResolution": "node", 12 | "importHelpers": true, 13 | "target": "es2020", 14 | "typeRoots": [ 15 | "node_modules/@types" 16 | ], 17 | "lib": [ 18 | "es2018", 19 | "dom" 20 | ] 21 | }, 22 | "angularCompilerOptions": { 23 | "fullTemplateTypeCheck": true, 24 | "strictInjectionParameters": true 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "./out-tsc/spec", 5 | "types": [ 6 | "jasmine", 7 | "node" 8 | ] 9 | }, 10 | "files": [ 11 | "src/test.ts", 12 | "src/polyfills.ts" 13 | ], 14 | "include": [ 15 | "src/**/*.spec.ts", 16 | "src/**/*.d.ts" 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tslint:recommended", 3 | "rules": { 4 | "align": { 5 | "options": [ 6 | "parameters", 7 | "statements" 8 | ] 9 | }, 10 | "array-type": false, 11 | "arrow-parens": false, 12 | "arrow-return-shorthand": true, 13 | "deprecation": { 14 | "severity": "warning" 15 | }, 16 | "component-class-suffix": true, 17 | "contextual-lifecycle": true, 18 | "curly": true, 19 | "directive-class-suffix": true, 20 | "directive-selector": [ 21 | true, 22 | "attribute", 23 | "app", 24 | "camelCase" 25 | ], 26 | "component-selector": [ 27 | true, 28 | "element", 29 | "app", 30 | "kebab-case" 31 | ], 32 | "eofline": true, 33 | "import-blacklist": [ 34 | true, 35 | "rxjs/Rx" 36 | ], 37 | "import-spacing": true, 38 | "indent": { 39 | "options": [ 40 | "spaces" 41 | ] 42 | }, 43 | "interface-name": false, 44 | "max-classes-per-file": false, 45 | "max-line-length": [ 46 | true, 47 | 140 48 | ], 49 | "member-access": false, 50 | "member-ordering": [ 51 | true, 52 | { 53 | "order": [ 54 | "static-field", 55 | "instance-field", 56 | "static-method", 57 | "instance-method" 58 | ] 59 | } 60 | ], 61 | "no-consecutive-blank-lines": false, 62 | "no-console": [ 63 | true, 64 | "debug", 65 | "info", 66 | "time", 67 | "timeEnd", 68 | "trace" 69 | ], 70 | "no-empty": false, 71 | "no-inferrable-types": [ 72 | true, 73 | "ignore-params" 74 | ], 75 | "no-non-null-assertion": true, 76 | "no-redundant-jsdoc": true, 77 | "no-switch-case-fall-through": true, 78 | "no-var-requires": false, 79 | "object-literal-key-quotes": [ 80 | true, 81 | "as-needed" 82 | ], 83 | "object-literal-sort-keys": false, 84 | "ordered-imports": false, 85 | "quotemark": [ 86 | true, 87 | "single" 88 | ], 89 | "trailing-comma": false, 90 | "no-conflicting-lifecycle": true, 91 | "no-host-metadata-property": true, 92 | "no-input-rename": true, 93 | "no-inputs-metadata-property": true, 94 | "no-output-native": true, 95 | "no-output-on-prefix": true, 96 | "no-output-rename": true, 97 | "semicolon": { 98 | "options": [ 99 | "always" 100 | ] 101 | }, 102 | "space-before-function-paren": { 103 | "options": { 104 | "anonymous": "never", 105 | "asyncArrow": "always", 106 | "constructor": "never", 107 | "method": "never", 108 | "named": "never" 109 | } 110 | }, 111 | "no-outputs-metadata-property": true, 112 | "template-banana-in-box": true, 113 | "template-no-negated-async": true, 114 | "typedef-whitespace": { 115 | "options": [ 116 | { 117 | "call-signature": "nospace", 118 | "index-signature": "nospace", 119 | "parameter": "nospace", 120 | "property-declaration": "nospace", 121 | "variable-declaration": "nospace" 122 | }, 123 | { 124 | "call-signature": "onespace", 125 | "index-signature": "onespace", 126 | "parameter": "onespace", 127 | "property-declaration": "onespace", 128 | "variable-declaration": "onespace" 129 | } 130 | ] 131 | }, 132 | "use-lifecycle-interface": true, 133 | "use-pipe-transform-interface": true, 134 | "variable-name": { 135 | "options": [ 136 | "ban-keywords", 137 | "check-format", 138 | "allow-pascal-case" 139 | ] 140 | }, 141 | "whitespace": { 142 | "options": [ 143 | "check-branch", 144 | "check-decl", 145 | "check-operator", 146 | "check-separator", 147 | "check-type", 148 | "check-typecast" 149 | ] 150 | } 151 | }, 152 | "rulesDirectory": [ 153 | "codelyzer" 154 | ] 155 | } --------------------------------------------------------------------------------