Skip to content

Commit 4ae5846

Browse files
author
Ian Walter
committed
Adding whip-check
1 parent 80a9cf2 commit 4ae5846

File tree

8 files changed

+179
-427
lines changed

8 files changed

+179
-427
lines changed

packages/whip-check/index.js

+146-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,146 @@
1-
export { default as SchemaValidator } from './lib/SchemaValidator.js'
2-
export * from './lib/validators.js'
3-
export * from './lib/modifiers.js'
4-
export { default as validate } from './lib/middleware/validate.js'
1+
import {
2+
any,
3+
array,
4+
bigint,
5+
boolean,
6+
date,
7+
enums,
8+
func,
9+
instance,
10+
integer,
11+
intersection,
12+
literal,
13+
map,
14+
never,
15+
number,
16+
nullable,
17+
object,
18+
optional,
19+
record,
20+
regexp,
21+
set,
22+
string,
23+
tuple,
24+
type,
25+
union,
26+
unknown,
27+
28+
assert,
29+
create,
30+
is,
31+
validate,
32+
33+
empty,
34+
max,
35+
min,
36+
nonempty,
37+
pattern,
38+
size,
39+
40+
defaulted,
41+
trimmed,
42+
43+
assign,
44+
deprecated,
45+
dynamic,
46+
lazy,
47+
omit,
48+
partial,
49+
pick,
50+
51+
define,
52+
coerce,
53+
refine,
54+
55+
StructError
56+
} from 'superstruct'
57+
import isEmail from 'isemail'
58+
import zxcvbn from 'zxcvbn'
59+
import parsePhoneNumber from 'libphonenumber-js'
60+
61+
export {
62+
any,
63+
array,
64+
bigint,
65+
boolean,
66+
date,
67+
enums,
68+
func,
69+
instance,
70+
integer,
71+
intersection,
72+
literal,
73+
map,
74+
never,
75+
number,
76+
nullable,
77+
object,
78+
optional,
79+
record,
80+
regexp,
81+
set,
82+
string,
83+
tuple,
84+
type,
85+
union,
86+
unknown,
87+
88+
assert,
89+
create,
90+
is,
91+
validate,
92+
93+
empty,
94+
max,
95+
min,
96+
nonempty,
97+
pattern,
98+
size,
99+
100+
defaulted,
101+
trimmed,
102+
103+
assign,
104+
deprecated,
105+
dynamic,
106+
lazy,
107+
omit,
108+
partial,
109+
pick,
110+
111+
define,
112+
coerce,
113+
refine,
114+
115+
StructError
116+
}
117+
118+
// FIXME: options?
119+
export const defaultEmailOptions = { minDomainAtoms: 2 }
120+
export const email = define('email', function email (input) {
121+
return isEmail.validate(input, defaultEmailOptions)
122+
})
123+
124+
// FIXME: inputs?
125+
export const password = define('password', function password (input) {
126+
return zxcvbn(input)
127+
})
128+
129+
// FIXME: country?
130+
export const phone = define('phone', function phone (input) {
131+
return parsePhoneNumber(input)
132+
})
133+
134+
export function check (checker) {
135+
if (!checker) throw new Error('Missing checker for check middleware')
136+
137+
return async function checkMiddleware (req, res, next) {
138+
const logger = req.logger.ns('whip.check')
139+
logger.debug('Input', req.body)
140+
141+
const [err, input] = checker.validate(req.body)
142+
if (err) throw err
143+
req.state.input = input
144+
next()
145+
}
146+
}

packages/whip-check/lib/SchemaValidator.js

-142
This file was deleted.

packages/whip-check/lib/middleware/validate.js

-21
This file was deleted.

packages/whip-check/lib/modifiers.js

-26
This file was deleted.

0 commit comments

Comments
 (0)