All files / is / is.ts

100.00% Branches 2/2
100.00% Lines 55/55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
x1
x5
x5
x5
 
x5
x5
 
x5
x5
x5
x5
x5
x5
x5
x5
x5
x5
x5
x5
x5
x5
x5
x5
x5
x5
x5
x5
x5
x5
x5
 
x5
x5
x7
x7
x7
x8
x8
x8
x8
x8
x8
x5
 
x5
x5
x124
x124
x124
x133
x133
x133
x133
x133
x133
x5
 
x5
x5

























































// Imports
import { z as is } from "zod"
import { type ErrorMessageOptions, generateErrorMessage } from "zod-error"
import { zodToJsonSchema as schema } from "zod-to-json-schema"

/** Original methods. */
const { parse, parseAsync } = is.ZodType.prototype

/** Error formatter. */
const format = {
  prefix: "Validation failed\n  - ",
  code: {
    enabled: true,
    label: null,
    transform: ({ value }) => `[${value.toLocaleUpperCase()}]`,
  },
  delimiter: {
    component: " ",
    error: "\n  - ",
  },
  message: {
    enabled: true,
    label: null,
    transform: ({ value }) => value.toLocaleLowerCase(),
  },
  path: {
    enabled: true,
    label: null,
    type: "objectNotation",
  },
} as ErrorMessageOptions

/** Zod parse override. */
is.ZodType.prototype.parse = function (...args: Parameters<typeof parse>) {
  try {
    return parse.apply(this, args)
  } catch (error) {
    if (error instanceof is.ZodError) {
      // deno-lint-ignore no-ex-assign
      error = new TypeError(generateErrorMessage(error.issues, format))
    }
    throw error
  }
}

/** Zod parseAsync override. */
is.ZodType.prototype.parseAsync = async function (...args: Parameters<typeof parseAsync>) {
  try {
    return await parseAsync.apply(this, args)
  } catch (error) {
    if (error instanceof is.ZodError) {
      // deno-lint-ignore no-ex-assign
      error = new TypeError(generateErrorMessage(error.issues, format))
    }
    throw error
  }
}

/** Input validation. */
export { is, schema }