UNPKG

@snowtop/ent-email

Version:

snowtop ent email datatype

55 lines (54 loc) 1.79 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EmailListType = exports.EmailType = exports.Email = void 0; const ent_1 = require("@snowtop/ent"); const email_addresses_1 = __importDefault(require("email-addresses")); function isParsedMailbox(mailboxOrGroup) { return mailboxOrGroup.type === "mailbox"; } class Email extends ent_1.BaseField { constructor() { super(...arguments); this.type = { dbType: ent_1.DBType.String }; } // restrict to just this domain domain(domain) { this._domain = domain; return this; } valid(val) { const address = email_addresses_1.default.parseOneAddress(val); if (!address) { return false; } // don't support groups at the moment // not even sure how to do that if (!isParsedMailbox(address)) { return false; } if (this._domain && address.domain !== this._domain) { return false; } // address with name is not valid for storing. that's for email // can make this optional in the future return address.name === null; } format(val) { // always trim and store in lowercase const type = (0, ent_1.StringType)().toLowerCase().trim(); return type.format(val); } } exports.Email = Email; function EmailType(options) { let result = new Email(); return Object.assign(result, options); } exports.EmailType = EmailType; function EmailListType(options) { return new ent_1.ListField(EmailType(options), options); } exports.EmailListType = EmailListType;