@saeris/graphql-scalars
Version:
A collection of scalar types using Joi for validation
27 lines (20 loc) • 938 B
JavaScript
import { GraphQLScalarType, GraphQLError, Kind } from "graphql";
import { string as yupString } from "yup";
const validate = value => yupString().strict(true).typeError(`Value is not string: ${value}`).matches(/^rgb\(\s*(-?\d+|-?\d*\.\d+(?=%))(%?)\s*,\s*(-?\d+|-?\d*\.\d+(?=%))(\2)\s*,\s*(-?\d+|-?\d*\.\d+(?=%))(\2)\s*\)$/, `Value is not a valid RGB color: ${value}`).validateSync(value);
export const RGBScalar = `scalar RGB`;
export const RGB = new GraphQLScalarType({
name: `RGB`,
description: `A field whose value is a CSS RGB color: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#rgb()_and_rgba().`,
serialize(value) {
return validate(value);
},
parseValue(value) {
return validate(value);
},
parseLiteral(ast) {
if (ast.kind !== Kind.STRING) {
throw new GraphQLError(`Can only validate strings as RGB colors but got a: ${ast.kind}`);
}
return validate(ast.value);
}
});