import { ValidatorFn , AbstractControl } from '@angular/forms' ; const _isCountry = ( currentCountry , countries ) => { return countries . indexOf ( currentCountry ) > - 1 ; } ; export function requiredFor ( countryCodes , { ignore = false } = { } ) { return ( control ) : { [ key ] : any ; } => { if ( ignore ) { return null ; } const country = control . root . get ( 'country' ) ; if ( ! country ) { return { required : true } ; } const selected = country . value ; if ( ! _isCountry ( selected , countryCodes ) ) { return null ; } return ! control . value ? { required : true } : null ; } ; } export function optionalFor ( countryCodes , { ignore = false } = { } ) { return ( control ) : { [ key ] : any ; } => { if ( ignore ) { return null ; } const country = control . root . get ( 'country' ) ; if ( ! country ) { return { required : true } ; } const selected = country . value ; if ( _isCountry ( selected , countryCodes ) ) { return null ; } return ! control . value ? { required : true } : null ; } ; }