All files / is / is.ts

100.00% Branches 39/39
100.00% Lines 135/135
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
 
 
 
x4
x4
x4
 
 
 
x4
x5
x10
x5
x5
 
 
x4
x8
x28
x39
x39
x49
x49
x39
x38
x8
x8
 
 
x4
x5
x5
 
 
x4
x5
x9
x9
x9
x11
x11
x9
x5
x5
 
 
x4
x6
x12
x45
x15
x15
x6
x6
 
 
x4
x6
x17
x27
x27
x27
x27
x27
x206
x209
x209
x209
x209
x206
x209
x209
x209
x206
x215
x215
x215
x206
x212
x212
x212
x206
x212
x212
x212
x212
x212
x212
x358
x358
x27
x30
x30
x30
x34
x34
x34
x27
x27
x18
x6
x6
 
 
x4
x5
x5
x14
x22
x22
x22
x22
x22
x22
x23
x23
x22
x22
x15
x5
x5
 
 
x4
x5
x10
x13
x13
x14
x14
x13
x14
x5
x5
 
 
x4
x5
x103
x103
x103
x1770
x200
x200
x200
x200
x200
x200
x103
x104
x5
x5
 
 
x40
 
 
 
 
x16
 
 
x4
 
 
x12
 
 
x12






































































































































































// Imports
// deno-lint-ignore-file no-explicit-any ban-types
import type { ParseOptions as ParseArgsOptions } from "@std/cli/parse-args"
import { parseArgs } from "@std/cli/parse-args"
import * as is from "@zod/zod"
export * as is from "@zod/zod"
export type * from "@zod/zod"

/** Coalesces a parsed value to `undefined` so schema defaults can be applied. */
export function coalesce<T extends is.ZodType>(schema: T): is.ZodPipe<is.ZodTransform<{} | undefined, unknown>, T> {
  return is.preprocess((value) => {
    return value ?? undefined
  }, schema)
}

/** Coerces a parsed value into `number` type when possible. */
export function coerce<T extends is.ZodType>(schema: T): is.ZodPipe<is.ZodTransform<unknown, unknown>, T> {
  return is.preprocess((value) => {
    if (typeof value === "string") {
      const coerced = Number(value)
      if ((!Number.isNaN(coerced)) || (value === "NaN")) {
        return coerced
      }
    }
    return value
  }, schema)
}

/** Allows the `null` value and makes it the default value in schemas. */
export function nullable<T extends is.ZodType>(schema: T): is.ZodDefault<is.ZodNullable<T>> {
  return schema.nullable().default(null)
}

/** Ensures a parsed value is compatible with `structuredClone` algorithm. */
export function clonable<T extends is.ZodType>(schema: T): is.ZodPipe<is.ZodTransform<any, unknown>, T> {
  return is.preprocess((value, context) => {
    try {
      structuredClone(value)
    } catch (error) {
      context.addIssue(`Invalid input: Value must be compatible with structuredClone algorithm: ${error}`)
    }
    return value
  }, schema)
}

/** Transforms a parsed value into an array if it is not already one. */
export function arrayable<T extends is.ZodType>(schema: T): is.ZodPipe<is.ZodTransform<any[], unknown>, T> {
  return is.preprocess((value) => {
    if (!Array.isArray(value)) {
      return [value]
    }
    return value
  }, schema)
}

/** Transforms a CLI arguments string into an object or array. */
export function cliable<T extends is.ZodType>(schema: T, options?: ParseArgsOptions): is.ZodPipe<is.ZodTransform<any, unknown>, T> {
  return is.preprocess((value, context) => {
    if (typeof value === "string") {
      const args = []
      let current = ""
      let quoted = ""
      let escape = false
      for (const char of value) {
        if (escape) {
          current += char
          escape = false
          continue
        }
        if ((quoted === '"') && (char === "\\")) {
          escape = true
          continue
        }
        if ((!quoted) && ((char === '"') || (char === "'"))) {
          quoted = char
          continue
        }
        if (quoted === char) {
          quoted = ""
          continue
        }
        if ((!quoted) && (char === " ")) {
          if (current) {
            args.push(current)
            current = ""
          }
          continue
        }
        current += char
      }
      if (quoted) {
        context.addIssue(`Unclosed quote: ${quoted}${current}`)
        return value
      }
      if (current) {
        args.push(current)
      }
      return options ? parseArgs(args, options) : args
    }
    return value
  }, schema)
}

/** Parses a regular expression back into a `RegExp`. */
export function regex<T extends is.ZodType>(schema: T): is.ZodPipe<is.ZodTransform<unknown, unknown>, T> {
  const definition = /^\/(?<pattern>.*?)\/(?<flags>[dgimsuvy]*)$/
  return is.preprocess((value) => {
    if ((typeof value === "string") && (definition.test(value))) {
      const groups = value.match(definition)?.groups
      if (groups) {
        const { pattern, flags } = groups
        try {
          return new RegExp(pattern, flags)
        } catch {
          return value
        }
      }
    }
    return value
  }, schema)
}

/** Parses a date string back into a `Date`. */
export function date<T extends is.ZodType>(schema: T): is.ZodPipe<is.ZodTransform<unknown, unknown>, T> {
  return is.preprocess((value) => {
    if (typeof value === "string") {
      const date = new Date(value)
      if (!Number.isNaN(date.getTime())) {
        return date
      }
    }
    return value
  }, schema)
}

/** Parses a duration string into milliseconds. */
export function duration<T extends is.ZodType>(schema: T): is.ZodPipe<is.ZodTransform<any, unknown>, T> {
  return is.preprocess((value) => {
    if (typeof value === "string") {
      const groups = value.match(/^\s*(?:(?<days>\d+)d(?:ays?)?)?\s*(?:(?<hours>\d+)h(?:(?:ou)?rs?)?)?\s*(?:(?<minutes>\d+)m(?:in(?:ute)?s?)?)?\s*(?:(?<seconds>\d+)s(?:ec(?:ond)?s?)?)?\s*(?:(?<milliseconds>\d+)(?:m(?:illi)?s(?:ec(?:ond)?s?)?)?)?\s*$/)?.groups
      if (groups) {
        let { days = 0, hours = 0, minutes = 0, seconds = 0, milliseconds = 0 } = Object.fromEntries(Object.entries(groups).map(([key, value]) => [key, Number(value)]).filter(([_, value]) => Number.isFinite(value)))
        hours += days * 24
        minutes += hours * 60
        seconds += minutes * 60
        milliseconds += seconds * 1000
        return milliseconds
      }
    }
    return value
  }, schema)
}

/** Type alias for primitive values. */
export const primitive = is.union([is.string(), is.number(), is.bigint(), is.boolean(), is.undefined(), is.null(), is.date(), is.instanceof(Error)]) as is.ZodUnion<
  readonly [is.ZodString, is.ZodNumber, is.ZodBigInt, is.ZodBoolean, is.ZodUndefined, is.ZodNull, is.ZodDate, is.ZodCustom<Error, Error>]
>

/** Type alias for URLs with supported protocols. */
export const url = is.url({ protocol: /^wasm|file|https?|data|blob|jsr|npm$/, hostname: /.*/ }) as is.ZodURL

/** Type alias for expression strings. */
export const expression = is.string().regex(/\$\{.*\}/) as is.ZodString

/** Type alias for callable values. */
export const callable = is.unknown().refine((value) => typeof value === "function", { message: "Invalid input: Value must be callable" }) as is.ZodUnknown

/** Type alias for Zod schemas. */
export const parser = is.custom((value) => (value instanceof is.ZodType) || (value && (typeof value === "object") && (typeof (value as Record<PropertyKey, unknown>).parse === "function")), { message: "Invalid input: Value must be a Zod schema" }) as is.ZodCustom<unknown, unknown>