From 80f151146201b2f97024178a53a3a39a852b151c Mon Sep 17 00:00:00 2001 From: BoykoAlex Date: Wed, 30 Aug 2017 21:36:25 -0400 Subject: [PATCH] Form control value validators --- src/lib/src/shared/flo.properties.ts | 35 +++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/lib/src/shared/flo.properties.ts b/src/lib/src/shared/flo.properties.ts index dadfca1..f21b68a 100644 --- a/src/lib/src/shared/flo.properties.ts +++ b/src/lib/src/shared/flo.properties.ts @@ -1,7 +1,8 @@ import { dia } from 'jointjs'; -import { ValidatorFn, AsyncValidatorFn } from '@angular/forms' +import { ValidatorFn, AsyncValidatorFn, AbstractControl, ValidationErrors } from '@angular/forms' import { Flo } from './../shared/flo.common'; import { Subject } from 'rxjs/Subject' +import { Observable } from 'rxjs/Observable'; export namespace Properties { @@ -177,4 +178,36 @@ export namespace Properties { } + export namespace Validators { + + export function uniqueResource(service : (value : any) => Observable, debounce : number): AsyncValidatorFn { + return (control: AbstractControl): Observable => { + return new Observable(obs => { + if (control.valueChanges && control.value) { + control.valueChanges + .debounceTime(debounce) + .flatMap(value => service(value)) + .subscribe(() => { + obs.next({uniqueResource: true}); + obs.complete(); + }, () => { + obs.next(null); + obs.complete(); + }) + } else { + obs.next(null); + obs.complete(); + } + }); + } + } + + export function noneOf(excluded : Array) : ValidatorFn { + return (control: AbstractControl) : {[key: string]: any} => { + return excluded.find(e => e === control.value) ? {'noneOf': {value: control.value}} : null; + }; + } + + } + }