UNPKG

graphql-scalars

Version:

A collection of scalar types not included in base GraphQL.

38 lines 1.67 kB
"use strict"; 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"); const PHONE_NUMBER_REGEX = /^\+[1-9]\d{1,14}$/; exports.default = new definition_1.GraphQLScalarType({ name: 'PhoneNumber', description: 'A field whose value conforms to the standard E.164 format as specified in: https://en.wikipedia.org/wiki/E.164. Basically this is +17895551234.', serialize(value) { if (typeof value !== 'string') { throw new TypeError(`Value is not string: ${value}`); } if (!(PHONE_NUMBER_REGEX.test(value))) { throw new TypeError(`Value is not a valid phone number of the form +17895551234 (10-15 digits): ${value}`); } return value; }, parseValue(value) { if (typeof value !== 'string') { throw new TypeError(`Value is not string: ${value}`); } if (!(PHONE_NUMBER_REGEX.test(value))) { throw new TypeError(`Value is not a valid phone number of the form +17895551234 (10-15 digits): ${value}`); } return value; }, parseLiteral(ast) { if (ast.kind !== language_1.Kind.STRING) { throw new error_1.GraphQLError(`Can only validate strings as phone numbers but got a: ${ast.kind}`); } if (!(PHONE_NUMBER_REGEX.test(ast.value))) { throw new TypeError(`Value is not a valid phone number of the form +17895551234 (10-15 digits): ${ast.value}`); } return ast.value; }, }); //# sourceMappingURL=PhoneNumber.js.map