14 lines
368 B
TypeScript
14 lines
368 B
TypeScript
|
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;
|
||
|
};
|
||
|
}
|
||
|
|