ajv#DefinedError TypeScript Examples

The following examples show how to use ajv#DefinedError. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: validate.ts    From UsTaxes with GNU Affero General Public License v3.0 6 votes vote down vote up
checkType = <A>(data: A, validate: ValidateFunction<A>): A => {
  validate(data)
  if ((validate.errors ?? undefined) !== undefined) {
    // Taken from doc example: The type cast is needed to allow user-defined keywords and errors
    // You can extend this type to include your error types as needed.

    const errs = validate.errors as DefinedError[]

    for (const err of errs) {
      log.error(err.message)
    }

    log.error(validate.errors)
    log.error(data)

    const errorMessage =
      validate.errors
        ?.map((e) => `${e.instancePath}: ${e.message ?? ''}`)
        .join('\n') ?? 'Unknown error'

    validate.errors?.forEach(console.error)
    throw new Error(`Validation Failed: ${errorMessage}`)
  }

  return data
}