UNPKG

@backland/schema

Version:

TypeScript schema declaration and validation library with static type inference

109 lines (108 loc) 8.35 kB
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } 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 = options.regionCode, numberType = options.numberType; if (typeof input !== 'string') { throw createFieldTypeError('unexpectedType', { expected: 'VALID_PHONE_NUMBER', found: input }); } var _CircularDeps$parsePh = CircularDeps.parsePhoneNumberServerSide.parsePhoneNumber(input, { regionCode: regionCode }), valid = _CircularDeps$parsePh.valid, type = _CircularDeps$parsePh.type; 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 = function () { try { var _CircularDeps$parsePh2; return !!((_CircularDeps$parsePh2 = CircularDeps.parsePhoneNumberServerSide) !== null && _CircularDeps$parsePh2 !== void 0 && _CircularDeps$parsePh2.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 var PhoneField = /*#__PURE__*/function (_FieldType) { _inherits(PhoneField, _FieldType); var _super = _createSuper(PhoneField); function PhoneField(def) { var _this; _classCallCheck(this, PhoneField); _this = _super.call(this, { def: def, name: 'phone' }); _defineProperty(_assertThisInitialized(_this), "parse", void 0); _this.parse = _this.applyParser({ parse: function parse(input) { return validatePhoneNumber(input); } }); return _this; } _createClass(PhoneField, null, [{ key: "is", value: function is(input) { return (input === null || input === void 0 ? void 0 : input.__isFieldType) && (input === null || input === void 0 ? void 0 : input.type) === 'phone'; } }, { key: "assert", value: function assert(input) { assertEqual(this.is(input), true, 'NOT_PHONE_FIELD'); } }]); return PhoneField; }(FieldType); _defineProperty(PhoneField, "create", function (def) { return new PhoneField(def); }); //# sourceMappingURL=PhoneField.js.map