Skip to content

Commit 2de54e2

Browse files
committed
fix output format
1 parent ba50125 commit 2de54e2

File tree

6 files changed

+20
-52
lines changed

6 files changed

+20
-52
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "deep-email-validator",
3-
"version": "0.1.7",
3+
"version": "0.1.8",
44
"files": [
55
"dist/**/*"
66
],

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export async function validate(
1010
recipient: string,
1111
sender: string = '[email protected]'
1212
): Promise<OutputFormat> {
13+
console.log('recipient', recipient)
1314
if (!isEmail(recipient)) return createOutput('regex', 'Invalid regex')
1415

1516
const typoResponse = await checkTypo(recipient)

src/output/output.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,31 @@ export type SubOutputFormat = {
77
reason?: string
88
}
99

10-
const NullOutputFormat: OutputFormat = {
11-
valid: true,
12-
validators: {
13-
regex: { valid: true },
14-
typo: { valid: true },
15-
disposable: { valid: true },
16-
mx: { valid: true },
17-
smtp: { valid: true },
18-
},
19-
}
2010
type Level = ElementType<typeof OrderedLevels>
2111
export type OutputFormat = SubOutputFormat & {
2212
validators: {
23-
[K in Level]: SubOutputFormat
13+
[K in Level]?: SubOutputFormat
2414
}
2515
}
2616

2717
export const createOutput = (
2818
failLevel?: Level,
2919
failReason?: string
3020
): OutputFormat => {
31-
const out = NullOutputFormat
21+
const out: OutputFormat = { valid: true, validators: {} }
22+
if (failLevel) {
23+
out.reason = failLevel
24+
out.valid = false
25+
}
3226
let valid = true
3327
for (let i = 0; i < OrderedLevels.length; i++) {
3428
const level = OrderedLevels[i]
35-
let reason
29+
const levelOut: SubOutputFormat = { valid }
3630
if (level === failLevel) {
3731
valid = false
38-
out.valid = valid
39-
reason = failReason
40-
out.reason = failLevel
32+
levelOut.reason = failReason
4133
}
42-
out.validators[level] = { valid: valid, reason }
34+
out.validators[level] = levelOut
4335
}
4436
return out
4537
}

src/smtp/smtp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ export const checkSMTP = async (
3131
})
3232

3333
socket.on('success', () => {
34-
r(createOutput())
3534
socket.write(`quit\r\n`)
3635
socket.end()
3736
socket.destroy()
37+
r(createOutput())
3838
})
3939

4040
const commands = [

test/__snapshots__/index.test.ts.snap

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,19 @@ Object {
66
"valid": false,
77
"validators": Object {
88
"disposable": Object {
9-
"reason": undefined,
109
"valid": true,
1110
},
1211
"mx": Object {
1312
"reason": "MX record not found",
14-
"valid": false,
13+
"valid": true,
1514
},
1615
"regex": Object {
17-
"reason": undefined,
1816
"valid": true,
1917
},
2018
"smtp": Object {
21-
"reason": undefined,
2219
"valid": false,
2320
},
2421
"typo": Object {
25-
"reason": undefined,
2622
"valid": true,
2723
},
2824
},
@@ -35,23 +31,19 @@ Object {
3531
"valid": false,
3632
"validators": Object {
3733
"disposable": Object {
38-
"reason": undefined,
3934
"valid": true,
4035
},
4136
"mx": Object {
42-
"reason": undefined,
4337
"valid": true,
4438
},
4539
"regex": Object {
46-
"reason": undefined,
4740
"valid": true,
4841
},
4942
"smtp": Object {
5043
"reason": "Invalid Mailbox",
51-
"valid": false,
44+
"valid": true,
5245
},
5346
"typo": Object {
54-
"reason": undefined,
5547
"valid": true,
5648
},
5749
},
@@ -64,23 +56,19 @@ Object {
6456
"valid": false,
6557
"validators": Object {
6658
"disposable": Object {
67-
"reason": undefined,
6859
"valid": false,
6960
},
7061
"mx": Object {
71-
"reason": undefined,
7262
"valid": false,
7363
},
7464
"regex": Object {
7565
"reason": "Invalid regex",
76-
"valid": false,
66+
"valid": true,
7767
},
7868
"smtp": Object {
79-
"reason": undefined,
8069
"valid": false,
8170
},
8271
"typo": Object {
83-
"reason": undefined,
8472
"valid": false,
8573
},
8674
},
@@ -93,24 +81,20 @@ Object {
9381
"valid": false,
9482
"validators": Object {
9583
"disposable": Object {
96-
"reason": undefined,
9784
"valid": false,
9885
},
9986
"mx": Object {
100-
"reason": undefined,
10187
"valid": false,
10288
},
10389
"regex": Object {
104-
"reason": undefined,
10590
"valid": true,
10691
},
10792
"smtp": Object {
108-
"reason": undefined,
10993
"valid": false,
11094
},
11195
"typo": Object {
11296
"reason": "Likely typo, suggested email: [email protected]",
113-
"valid": false,
97+
"valid": true,
11498
},
11599
},
116100
}
@@ -123,22 +107,18 @@ Object {
123107
"validators": Object {
124108
"disposable": Object {
125109
"reason": "Email was created using a disposable email service",
126-
"valid": false,
110+
"valid": true,
127111
},
128112
"mx": Object {
129-
"reason": undefined,
130113
"valid": false,
131114
},
132115
"regex": Object {
133-
"reason": undefined,
134116
"valid": true,
135117
},
136118
"smtp": Object {
137-
"reason": undefined,
138119
"valid": false,
139120
},
140121
"typo": Object {
141-
"reason": undefined,
142122
"valid": true,
143123
},
144124
},
@@ -147,27 +127,21 @@ Object {
147127

148128
exports[`validation tests passes 1`] = `
149129
Object {
150-
"reason": "smtp",
151-
"valid": false,
130+
"valid": true,
152131
"validators": Object {
153132
"disposable": Object {
154-
"reason": undefined,
155133
"valid": true,
156134
},
157135
"mx": Object {
158-
"reason": undefined,
159136
"valid": true,
160137
},
161138
"regex": Object {
162-
"reason": undefined,
163139
"valid": true,
164140
},
165141
"smtp": Object {
166-
"reason": undefined,
167142
"valid": true,
168143
},
169144
"typo": Object {
170-
"reason": undefined,
171145
"valid": true,
172146
},
173147
},

test/index.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ describe('validation tests', () => {
2727
})
2828

2929
it('passes', async () => {
30-
const res = await validate('[email protected]')
30+
const res = await validate('[email protected]')
31+
console.log('res', res)
3132
expect(res).toMatchSnapshot()
3233
})
3334
})

0 commit comments

Comments
 (0)