All files / src/components/common/CustomEditor/CodeEditor validators.ts

18.18% Statements 2/11
0% Branches 0/6
0% Functions 0/2
22.22% Lines 2/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28    4x                         4x                        
import { safeParse } from "@utils/index";
 
const codeValidator = (
  language: string,
  value: any,
  onError?: (error?: Error) => void,
) => {
  return JSONValidator(value, onError);
};
 
export type TValidator = (
  value: any,
  onError?: (error?: Error) => void,
) => boolean;
 
const JSONValidator: TValidator = (value, onError) => {
  const parsed = safeParse(value);
  if (parsed !== null) {
    if (onError) onError(undefined);
    return true;
  } else {
    if (onError) onError(new Error("Invalid JSON"));
    return false;
  }
};
 
export default codeValidator;