UNPKG

@backland/schema

Version:

TypeScript schema declaration and validation library with static type inference

40 lines 2.21 kB
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 { expectedType } from '@backland/utils'; import { FieldType } from './FieldType'; // https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/web_tests/fast/forms/resources/ValidityState-typeMismatch-email.js?q=ValidityState-typeMismatch-email.js&ss=chromium // https://stackoverflow.com/questions/46155/whats-the-best-way-to-validate-an-email-address-in-javascript/46181#46181 var emailRegex = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i; export class EmailField extends FieldType { constructor() { var def = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; super({ def: def, name: 'email' }); _defineProperty(this, "parse", void 0); var { regex: _regex = emailRegex } = def; if (def.regex && !Array.isArray(def.regex)) { throw new Error("Invalid regex definition received. Expected [string] | [string, string]."); } var regex = Array.isArray(_regex) ? new RegExp(_regex[0], _regex[1]) : _regex; this.parse = this.applyParser({ parse: input => { expectedType({ value: input }, 'string'); if (!regex.test(input)) { throw new Error("Invalid email received."); } return input; } }); } } _defineProperty(EmailField, "create", def => { return new EmailField(def); }); //# sourceMappingURL=EmailField.js.map