@codelegend/email-validator 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +29 -0
- package/README.md +103 -2
- package/disposable-domains.json +9921 -0
- package/index.d.ts +26 -0
- package/index.js +82 -0
- package/package.json +28 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 email-validator contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
Bundled disposable-domains.json data:
|
|
24
|
+
Source: https://github.com/disposable-email-domains/disposable-email-domains
|
|
25
|
+
CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
|
|
26
|
+
The upstream authors have dedicated this data to the public domain.
|
|
27
|
+
You can copy, modify, distribute and perform the work, even for commercial
|
|
28
|
+
purposes, all without asking permission.
|
|
29
|
+
Details: https://creativecommons.org/publicdomain/zero/1.0/legalcode
|
package/README.md
CHANGED
|
@@ -1,3 +1,104 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @codelegend/email-validator
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Zero-dependency Node.js email validation, bundled disposable-domain blocking, custom domain rules, and Express middleware. CommonJS with TypeScript declarations; Node.js 18 or newer.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @codelegend/email-validator
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
const { createEmailValidator } = require('@codelegend/email-validator');
|
|
13
|
+
|
|
14
|
+
// Create once and reuse across requests.
|
|
15
|
+
const validator = createEmailValidator({
|
|
16
|
+
allowedDomains: ['trusted.example.com'],
|
|
17
|
+
blockedDomains: ['unwanted.example.com'],
|
|
18
|
+
blockSubdomains: true,
|
|
19
|
+
useDefaultDomains: true
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
validator.validateEmail('Alex@example.com');
|
|
23
|
+
// { valid: true, code: 'EMAIL_ACCEPTED', message: 'Email accepted',
|
|
24
|
+
// email: 'Alex@example.com', domain: 'example.com' }
|
|
25
|
+
|
|
26
|
+
validator.validateEmail('a@unwanted.example.com');
|
|
27
|
+
// { valid: false, code: 'BLOCKED_DOMAIN', message: 'Email domain is blocked',
|
|
28
|
+
// email: 'a@unwanted.example.com', domain: 'unwanted.example.com',
|
|
29
|
+
// matchedDomain: 'unwanted.example.com' }
|
|
30
|
+
|
|
31
|
+
validator.isValid('hello@example.com'); // true
|
|
32
|
+
validator.validateMany(['hello@example.com', 'bad-address']);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
ES modules can import the CommonJS default export:
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
import emailValidator from '@codelegend/email-validator';
|
|
39
|
+
const { createEmailValidator } = emailValidator;
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Convenience exports `validateEmail(input)` and `isValidEmail(input)` use the default bundled rules. Use a factory instance for custom configuration.
|
|
43
|
+
|
|
44
|
+
## Rules and results
|
|
45
|
+
|
|
46
|
+
| Option | Default | Meaning |
|
|
47
|
+
| --- | --- | --- |
|
|
48
|
+
| `allowedDomains` | `[]` | Exact exceptions overriding custom and bundled blocks |
|
|
49
|
+
| `blockedDomains` | `[]` | Custom domain blocks in addition to bundled disposable domains |
|
|
50
|
+
| `blockSubdomains` | `true` | Blocks apply to descendant domains too |
|
|
51
|
+
| `useDefaultDomains` | `true` | Enable the bundled disposable list |
|
|
52
|
+
|
|
53
|
+
Rule lists accept arrays or Sets, normalize whitespace, casing, and Unicode domains, and are copied at creation. Invalid configuration throws `TypeError`. Recreate an instance to update its rules. Instances do not share custom configuration.
|
|
54
|
+
|
|
55
|
+
Syntax is checked first. Exact allowlist matches then override all blocks. Custom blocks are checked next, followed by bundled disposable domains. An allowlist is an exception list, not a restriction to only those domains. Allowing `example.com` does not automatically allow `sub.example.com`. Blocking `example.com` also blocks `sub.example.com` unless `blockSubdomains` is false. Matching respects label boundaries, so `notexample.com` does not match `example.com`.
|
|
56
|
+
|
|
57
|
+
Every result has `valid`, `code`, and `message`:
|
|
58
|
+
|
|
59
|
+
| Code | Meaning |
|
|
60
|
+
| --- | --- |
|
|
61
|
+
| `EMAIL_ACCEPTED` | Passed syntax and configured domain rules |
|
|
62
|
+
| `EMAIL_REQUIRED` | Empty or non-string input |
|
|
63
|
+
| `INVALID_EMAIL` | Unsupported or malformed email syntax |
|
|
64
|
+
| `BLOCKED_DOMAIN` | Custom blocking rule matched |
|
|
65
|
+
| `DISPOSABLE_EMAIL` | Bundled disposable rule matched |
|
|
66
|
+
|
|
67
|
+
Accepted and blocked results include normalized `email` and `domain`; blocked results also include `matchedDomain`. The local part keeps its original casing. Unicode domains become ASCII via Node's IDNA conversion. `validateMany` preserves input order and duplicates; it throws for a non-array argument. Non-string email values produce rejection results rather than throwing.
|
|
68
|
+
|
|
69
|
+
This is a practical signup policy: ASCII unquoted local parts and dotted domains. It rejects domain literals, numeric final labels, quoted local parts, and Unicode local parts. It does not check public-suffix registration, DNS/MX records, mailbox existence, or deliverability.
|
|
70
|
+
|
|
71
|
+
## Express
|
|
72
|
+
|
|
73
|
+
Install Express separately and mount its JSON parser first:
|
|
74
|
+
|
|
75
|
+
```js
|
|
76
|
+
const express = require('express');
|
|
77
|
+
const { createEmailValidator } = require('@codelegend/email-validator');
|
|
78
|
+
const app = express();
|
|
79
|
+
const validator = createEmailValidator({ blockedDomains: ['unwanted.example.com'] });
|
|
80
|
+
|
|
81
|
+
app.use(express.json());
|
|
82
|
+
app.post('/signup', validator.middleware, (req, res) => {
|
|
83
|
+
// req.body.email is normalized. Rejected requests already received HTTP 400.
|
|
84
|
+
res.json({ email: req.body.email });
|
|
85
|
+
});
|
|
86
|
+
app.listen(3000);
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Middleware validates `req.body.email`, returns HTTP 400 with the rejection result, or replaces the email with its normalized value and calls `next()` once. Express is not a package dependency.
|
|
90
|
+
|
|
91
|
+
## Domain data
|
|
92
|
+
|
|
93
|
+
Update `disposable-domains.json` directly to maintain the bundled blocklist. No build step is required. The library uses Node's `domainToASCII` for Unicode domain normalization.
|
|
94
|
+
|
|
95
|
+
## Publishing
|
|
96
|
+
|
|
97
|
+
1. Run `npm pack --dry-run` to inspect the package files.
|
|
98
|
+
2. Run `npm login`, then `npm publish --access public`.
|
|
99
|
+
|
|
100
|
+
The package includes only the library, declarations, bundled domain JSON, README, and MIT license. Local credentials are excluded.
|
|
101
|
+
|
|
102
|
+
## License
|
|
103
|
+
|
|
104
|
+
Code: MIT. Bundled domain data: CC0 1.0, from [disposable-email-domains](https://github.com/disposable-email-domains/disposable-email-domains). The bundled 9,919 entries were matched against the upstream list on 2026-10-23. See the [upstream license](https://github.com/disposable-email-domains/disposable-email-domains/blob/main/LICENSE.txt).
|