@saeris/graphql-scalars
Version:
A collection of scalar types using Joi for validation
27 lines (20 loc) • 911 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(/^hsl\(\s*(-?\d+|-?\d*.\d+)\s*,\s*(-?\d+|-?\d*.\d+)%\s*,\s*(-?\d+|-?\d*.\d+)%\s*\)$/i, `Value is not a valid HSL color: ${value}`).validateSync(value);
export const HSLScalar = `scalar HSL`;
export const HSL = new GraphQLScalarType({
name: `HSL`,
description: `A field whose value is a CSS HSL color: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#hsl()_and_hsla().`,
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 HSL colors but got a: ${ast.kind}`);
}
return validate(ast.value);
}
});