is-json-value
Version:
Check if a value is valid JSON
36 lines (22 loc) • 822 B
JavaScript
import safeJsonValue from"safe-json-value";
import{getDescription,isWarning}from"./description.js";
import{getPrefix}from"./prefix.js";
const isJsonValue=(input)=>
safeJsonValue(input).changes.filter(isWarning).map(getWarning);
export default isJsonValue;
const getWarning=({path,oldValue:value,reason,error})=>{
const message=getMessage(path,reason,error);
return{message,path,value,reason}
};
const getMessage=(path,reason,error)=>{
const prefix=getPrefix(path);
const description=getDescription(reason);
const errorStack=getErrorStack(error);
return`${prefix} ${description}${errorStack}`
};
const getErrorStack=(error)=>
error===undefined?"":`\n${stringifyError(error)}`;
const stringifyError=({name,message,stack})=>
stack.includes(name)&&stack.includes(message)?
stack:
`${name}: ${message}\n${stack}`;