graphql-scalars
Version:
A collection of scalar types not included in base GraphQL.
30 lines • 1.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const graphql_1 = require("graphql");
const HSL_REGEX = /^hsl\(\s*(-?\d+|-?\d*.\d+)\s*,\s*(-?\d+|-?\d*.\d+)%\s*,\s*(-?\d+|-?\d*.\d+)%\s*\)$/;
const validate = (value) => {
if (typeof value !== 'string') {
throw new TypeError(`Value is not string: ${value}`);
}
if (!(HSL_REGEX.test(value))) {
throw new TypeError(`Value is not a valid HSL color: ${value}`);
}
return value;
};
exports.default = new graphql_1.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 !== graphql_1.Kind.STRING) {
throw new graphql_1.GraphQLError(`Can only validate strings as HSL colors but got a: ${ast.kind}`);
}
return validate(ast.value);
}
});
//# sourceMappingURL=HSL.js.map