import { AfterViewInit , Directive , Inject , Injector , forwardRef } from '@angular/core' ; import { FormControl , NG_VALIDATORS , NgControl , ValidationErrors , Validator } from '@angular/forms' ; import { ObSchemaValidationDirective } from './schema-validation.directive' ; import { WINDOW } from '../utilities' ; @ Directive ( { selector : '[obSchemaValidate][ngModel],[obSchemaValidate][formControlName]' , providers : [ { provide : NG_VALIDATORS , useExisting : forwardRef ( ( ) => ObSchemaValidateDirective ) , multi : true } ] } ) export class ObSchemaValidateDirective implements AfterViewInit , Validator { private propertyName : string ; constructor ( private readonly schemaDirective , private readonly injector , @ Inject ( WINDOW ) private readonly window ) { } ngAfterViewInit ( ) { const ngControl = this . injector . get ( NgControl ) ; this . propertyName = ngControl . path . join ( '.' ) ; this . window . setTimeout ( ( ) => ngControl . control . updateValueAndValidity ( ) ) ; } validate ( formControl ) { return this . schemaDirective . validate ( this . propertyName , formControl . value ) ; } }