graphql-extra-scalars
Version:
Collection of extra GraphQL scalar types like Email, URL, Password and more
35 lines (34 loc) • 1.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const lengthValidator = (name, value, min, max) => {
if (value.length < min) {
throw new TypeError(`${name} not long enough`);
}
if (max && value.length > max) {
throw new TypeError(`${name} too long`);
}
};
exports.lengthValidator = lengthValidator;
const alphabetValidator = (name, value, alphabet) => {
for (const char of value) {
if (!alphabet.includes(char)) {
throw new TypeError(`${name} has a not allowed character`);
}
}
};
exports.alphabetValidator = alphabetValidator;
const complexityValidator = (name, value, options = {}) => {
const alphaNumericRe = /^(?=.*[0-9])(?=.*[a-zA-Z])(.+)$/u;
const mixedCaseRe = /^(?=.*[a-z])(?=.*[A-Z])(.+)$/u;
const specialCharsRe = /^(?=.*[^a-zA-Z0-9])(.+)$/u;
if (options.alphaNumeric && !alphaNumericRe.test(value)) {
throw new TypeError(`${name} must contain at least one number and one letter`);
}
if (options.mixedCase && !mixedCaseRe.test(value)) {
throw new TypeError(`${name} must contain at least one upper and one lower case letter`);
}
if (options.specialChars && !specialCharsRe.test(value)) {
throw new TypeError(`${name} must contain at least one special character`);
}
};
exports.complexityValidator = complexityValidator;