graphql-scalars
Version:
A collection of scalar types not included in base GraphQL.
40 lines • 1.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const definition_1 = require("graphql/type/definition");
const error_1 = require("graphql/error");
const language_1 = require("graphql/language");
/* eslint-disable no-useless-escape */
const EMAIL_ADDRESS_REGEX = new RegExp(/^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/);
/* eslint-enable */
exports.default = new definition_1.GraphQLScalarType({
name: 'EmailAddress',
description: 'A field whose value conforms to the standard internet email address format as specified in RFC822: https://www.w3.org/Protocols/rfc822/.',
serialize(value) {
if (typeof value !== 'string') {
throw new TypeError(`Value is not string: ${value}`);
}
if (!EMAIL_ADDRESS_REGEX.test(value)) {
throw new TypeError(`Value is not a valid email address: ${value}`);
}
return value;
},
parseValue(value) {
if (typeof value !== 'string') {
throw new TypeError('Value is not string');
}
if (!EMAIL_ADDRESS_REGEX.test(value)) {
throw new TypeError(`Value is not a valid email address: ${value}`);
}
return value;
},
parseLiteral(ast) {
if (ast.kind !== language_1.Kind.STRING) {
throw new error_1.GraphQLError(`Can only validate strings as email addresses but got a: ${ast.kind}`);
}
if (!EMAIL_ADDRESS_REGEX.test(ast.value)) {
throw new TypeError(`Value is not a valid email address: ${ast.value}`);
}
return ast.value;
},
});
//# sourceMappingURL=EmailAddress.js.map