UNPKG

graphql-extra-scalars

Version:

Collection of extra GraphQL scalar types like Email, URL, Password and more

48 lines (47 loc) 1.9 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const validators = __importStar(require("../validators")); const graphql_1 = require("graphql"); const literalParser_1 = require("../literalParser"); let limitedStringCounter = 0; class GraphQLLimitedString extends graphql_1.GraphQLScalarType { constructor(min = 1, max, alphabet) { const suffix = (limitedStringCounter++ > 0) ? limitedStringCounter : ''; const name = `LimitedString${suffix}`; let description = 'A limited string.'; if (max) { description = `${description} Has to be between ${min} and ${max} characters long.`; } else { description = `${description} Has to be at least ${min} characters long.`; } if (alphabet) { description = `${description} May only contain the following characters: ${alphabet}`; } const coerceType = (value) => { if (typeof value !== 'string') { throw new TypeError(`${name} cannot represent a non string value: [${String(value)}]`); } validators.lengthValidator(name, value, min, max); if (alphabet) { validators.alphabetValidator(name, value, alphabet); } return value; }; super({ name: name, description: description, serialize: coerceType, parseValue: coerceType, parseLiteral: literalParser_1.createParseLiteral(coerceType), }); } } exports.GraphQLLimitedString = GraphQLLimitedString;