@graphql-mesh/transport-rest
Version:
21 lines (20 loc) • 623 B
JavaScript
export function processRegExpAnnotations(scalar, pattern) {
function coerceString(value) {
if (value != null) {
const vStr = value.toString();
const regexp = new RegExp(pattern);
if (!regexp.test(vStr)) {
throw new Error(`${scalar.name} must match ${pattern} but given ${vStr}`);
}
return vStr;
}
}
scalar.serialize = coerceString;
scalar.parseValue = coerceString;
scalar.parseLiteral = ast => {
if ('value' in ast) {
return coerceString(ast.value);
}
return null;
};
}