Skip to content

Commit

Permalink
Adding whip-check
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Walter committed Apr 2, 2022
1 parent 80a9cf2 commit 4ae5846
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 427 deletions.
150 changes: 146 additions & 4 deletions packages/whip-check/index.js
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()
}
}
142 changes: 0 additions & 142 deletions packages/whip-check/lib/SchemaValidator.js

This file was deleted.

21 changes: 0 additions & 21 deletions packages/whip-check/lib/middleware/validate.js

This file was deleted.

26 changes: 0 additions & 26 deletions packages/whip-check/lib/modifiers.js

This file was deleted.

Loading

0 comments on commit 4ae5846

Please sign in to comment.