-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ian Walter
committed
Apr 2, 2022
1 parent
80a9cf2
commit 4ae5846
Showing
8 changed files
with
179 additions
and
427 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,146 @@ | ||
export { default as SchemaValidator } from './lib/SchemaValidator.js' | ||
export * from './lib/validators.js' | ||
export * from './lib/modifiers.js' | ||
export { default as validate } from './lib/middleware/validate.js' | ||
import { | ||
any, | ||
array, | ||
bigint, | ||
boolean, | ||
date, | ||
enums, | ||
func, | ||
instance, | ||
integer, | ||
intersection, | ||
literal, | ||
map, | ||
never, | ||
number, | ||
nullable, | ||
object, | ||
optional, | ||
record, | ||
regexp, | ||
set, | ||
string, | ||
tuple, | ||
type, | ||
union, | ||
unknown, | ||
|
||
assert, | ||
create, | ||
is, | ||
validate, | ||
|
||
empty, | ||
max, | ||
min, | ||
nonempty, | ||
pattern, | ||
size, | ||
|
||
defaulted, | ||
trimmed, | ||
|
||
assign, | ||
deprecated, | ||
dynamic, | ||
lazy, | ||
omit, | ||
partial, | ||
pick, | ||
|
||
define, | ||
coerce, | ||
refine, | ||
|
||
StructError | ||
} from 'superstruct' | ||
import isEmail from 'isemail' | ||
import zxcvbn from 'zxcvbn' | ||
import parsePhoneNumber from 'libphonenumber-js' | ||
|
||
export { | ||
any, | ||
array, | ||
bigint, | ||
boolean, | ||
date, | ||
enums, | ||
func, | ||
instance, | ||
integer, | ||
intersection, | ||
literal, | ||
map, | ||
never, | ||
number, | ||
nullable, | ||
object, | ||
optional, | ||
record, | ||
regexp, | ||
set, | ||
string, | ||
tuple, | ||
type, | ||
union, | ||
unknown, | ||
|
||
assert, | ||
create, | ||
is, | ||
validate, | ||
|
||
empty, | ||
max, | ||
min, | ||
nonempty, | ||
pattern, | ||
size, | ||
|
||
defaulted, | ||
trimmed, | ||
|
||
assign, | ||
deprecated, | ||
dynamic, | ||
lazy, | ||
omit, | ||
partial, | ||
pick, | ||
|
||
define, | ||
coerce, | ||
refine, | ||
|
||
StructError | ||
} | ||
|
||
// FIXME: options? | ||
export const defaultEmailOptions = { minDomainAtoms: 2 } | ||
export const email = define('email', function email (input) { | ||
return isEmail.validate(input, defaultEmailOptions) | ||
}) | ||
|
||
// FIXME: inputs? | ||
export const password = define('password', function password (input) { | ||
return zxcvbn(input) | ||
}) | ||
|
||
// FIXME: country? | ||
export const phone = define('phone', function phone (input) { | ||
return parsePhoneNumber(input) | ||
}) | ||
|
||
export function check (checker) { | ||
if (!checker) throw new Error('Missing checker for check middleware') | ||
|
||
return async function checkMiddleware (req, res, next) { | ||
const logger = req.logger.ns('whip.check') | ||
logger.debug('Input', req.body) | ||
|
||
const [err, input] = checker.validate(req.body) | ||
if (err) throw err | ||
req.state.input = input | ||
next() | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.