argos/dmp-frontend/src/app/library/auto-complete/auto-complete-custom-valida...

14 lines
368 B
TypeScript
Raw Normal View History

2019-01-18 18:03:45 +01:00
import { AbstractControl, ValidationErrors, ValidatorFn } from '@angular/forms';
export function ValidJsonValidator(): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
try {
if (typeof control.value !== 'object') { JSON.parse(control.value); }
} catch (e) {
return { 'invalidJson': { value: true } };
}
return null;
};
}