@backland/schema
Version:
TypeScript schema declaration and validation library with static type inference
90 lines (89 loc) • 4.64 kB
JavaScript
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import { assertEqual, isProduction } from '@backland/utils';
//
import { CircularDeps } from '../CircularDeps';
import { FieldType } from './FieldType';
import { createFieldTypeError } from './FieldTypeErrors';
// https://stackoverflow.com/a/47949522
export var E164_PHONE_REGEX = /^(\+|00)(297|93|244|1264|358|355|376|971|54|374|1684|1268|61|43|994|257|32|229|226|880|359|973|1242|387|590|375|501|1441|591|55|1246|673|975|267|236|1|61|41|56|86|225|237|243|242|682|57|269|238|506|53|5999|61|1345|357|420|49|253|1767|45|1809|1829|1849|213|593|20|291|212|34|372|251|358|679|500|33|298|691|241|44|995|44|233|350|224|590|220|245|240|30|1473|299|502|594|1671|592|852|504|385|509|36|62|44|91|246|353|98|964|354|972|39|1876|44|962|81|76|77|254|996|855|686|1869|82|383|965|856|961|231|218|1758|423|94|266|370|352|371|853|590|212|377|373|261|960|52|692|389|223|356|95|382|976|1670|258|222|1664|596|230|265|60|262|264|687|227|672|234|505|683|31|47|977|674|64|968|92|507|64|51|63|680|675|48|1787|1939|850|351|595|970|689|974|262|40|7|250|966|249|221|65|500|4779|677|232|503|378|252|508|381|211|239|597|421|386|46|268|1721|248|963|1649|235|228|66|992|690|993|670|676|1868|216|90|688|886|255|256|380|598|1|998|3906698|379|1784|58|1284|1340|84|678|681|685|967|27|260|263)(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)\d{4,20}$/;
export function _backendValidatePhoneNumber(input) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var {
regionCode,
numberType
} = options;
if (typeof input !== 'string') {
throw createFieldTypeError('unexpectedType', {
expected: 'VALID_PHONE_NUMBER',
found: input
});
}
var {
valid,
type
} = CircularDeps.parsePhoneNumberServerSide.parsePhoneNumber(input, {
regionCode: regionCode
});
if (valid && numberType) {
valid = type === numberType;
}
if (!valid) {
throw createFieldTypeError('invalidPhone', {
expected: 'VALID_PHONE_NUMBER',
found: input
});
}
return input;
}
var customPhoneValidator = undefined;
export function setCustomPhoneValidator(validator) {
return customPhoneValidator = validator;
}
export function validatePhoneNumber(input, options) {
if (customPhoneValidator) {
return customPhoneValidator(input, options);
}
var hasAPV = (() => {
try {
var _CircularDeps$parsePh;
return !!((_CircularDeps$parsePh = CircularDeps.parsePhoneNumberServerSide) !== null && _CircularDeps$parsePh !== void 0 && _CircularDeps$parsePh.parsePhoneNumber);
} catch (e) {
return false;
}
})();
if (hasAPV) return _backendValidatePhoneNumber(input, options);
if (typeof input !== 'string') {
throw new Error('Expected phone number as string.');
}
if (options && !isProduction()) {
console.warn('validatePhoneNumber: using backup function for phone validation, since phone validator was not defined and' + ' "awesome-phonenumber" package is not available at the current environment.' + 'Use setCustomPhoneValidator to set a new function.');
}
if (!E164_PHONE_REGEX.test(input)) throw Error('Expected phone number.');
return input;
}
export class PhoneField extends FieldType {
static is(input) {
return (input === null || input === void 0 ? void 0 : input.__isFieldType) && (input === null || input === void 0 ? void 0 : input.type) === 'phone';
}
static assert(input) {
assertEqual(this.is(input), true, 'NOT_PHONE_FIELD');
}
constructor(def) {
super({
def,
name: 'phone'
});
_defineProperty(this, "parse", void 0);
this.parse = this.applyParser({
parse(input) {
return validatePhoneNumber(input);
}
});
}
}
_defineProperty(PhoneField, "create", def => {
return new PhoneField(def);
});
//# sourceMappingURL=PhoneField.js.map