@grapi/server
Version:
Grapi Schema Generator For GraphQL Server
28 lines (27 loc) • 1.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Phone = void 0;
const graphql_1 = require("graphql");
const isPhoneOrMobileNumber = (value) => {
return /^([0-9]{1})?([0-9]{1})?([0-9]{1})([0-9]{10})|^([0-9]{7})$/.test(value);
};
const parseAndSerializeValue = (value) => {
if (isPhoneOrMobileNumber(value)) {
return value;
}
throw new Error(`Wrong "${value}" format for phone or mobile number.`);
};
const parseLiteral = (ast) => {
if (isPhoneOrMobileNumber(ast.value)) {
return ast.value;
}
throw new Error(`Wrong "${ast.value}" format for phone or mobile number.`);
};
const Phone = new graphql_1.GraphQLScalarType({
name: 'Phone',
description: `Formats from Phone number or mobile number. This data type accept digits with a standard, 7 digits for phone and 11 - 13 digits for mobile number.`,
serialize: parseAndSerializeValue,
parseValue: parseAndSerializeValue,
parseLiteral: parseLiteral
});
exports.Phone = Phone;