Skip to content

Instantly share code, notes, and snippets.

@arantesxyz
Created March 3, 2021 00:15
Show Gist options
  • Select an option

  • Save arantesxyz/ec4bfdb53ad2440d27e8eccf82ef4a7b to your computer and use it in GitHub Desktop.

Select an option

Save arantesxyz/ec4bfdb53ad2440d27e8eccf82ef4a7b to your computer and use it in GitHub Desktop.
Custom error creator for checking empty strings. Depends on 'validator'
import isEmpty, { IsEmptyOptions } from 'validator/lib/isEmpty'
const checkEmpty = (
error: (message: string) => unknown,
options?: IsEmptyOptions
) => (value: string, fieldName: string): void => {
if (isEmpty(value, options)) {
throw error(`Field ${fieldName} cannot be empty!`)
}
}
export default checkEmpty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment