All files / is / is.ts

96.61% Branches 114/118
94.12% Functions 16/17
90.42% Lines 236/261
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
 
 
 
x4
x4
x4
 
 
 
x4
x1
x5
x1
x1
 
 
x4
x4
x20
x11
x11
x10
x10
x11
x10
x4
x4
 
 
x4
x1
x1
 
 
x4
x1
x4
x4
x4
x2
x2
x4
x1
x1
 
 
x4
x2
x6
x3
x3
x3
x2
x2
 
 
x4
x2
x11
x10
x10
x10
x10
x10
x179
x3
x3
x3
x3
x179
x3
x3
x3
x179
x9
x9
x9
x179
x6
x6
x6
x179
x6
x6
x6
x6
x6
x6
x152
x152
x10
x3
x3
x3
x7
x7
x7
x10
x10
x1
x2
x2
 
 
x4
x1
x1
x9
x8
x8
x8
x8
x8
x8
x1
x1
x8
x8
x1
x1
x1
 
 
x4
x1
x5
x3
x3
x1
x1
x3
x4
x1
x1
 
 
x4
x1
x98
x98
x98
x485
x97
x97
x97
x97
x97
x97
x98
x1
x1
x1
 
 
x4
 
 
 
 
x4
x4
x4
x4
x4
x4
x4
x4
x4
x4
x4
x4
x4
x4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x4
x4
x1
x1
x4
 
 
 
 
 
x4
x4
x4
x4
x4
x4
x4
x4
x4
x4
x4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x4
 
 
x4
x32
x77
x13
x13
x77
x4
x4
x77
x55
x40
x40
x55
x77
x33
x33
x64
x32
x32
 
 
 
 
 
x4
x4
x4
x4
x4
x4
x4
x4
x4
x4
x18
 
 
x4
x2
x2
x2
x21
x18
x4
x21
x1
x1
x21
x24
x3
x14
x2
x4
 
 
x4
 
 
x4
 
 
x4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x4
x2
 
 
 
x2
x2
x2
 
 
 
 
 
 
 
 
 
x2
x2
x2
 
 
 
 
 
 
 
 
 
 
 
 
 
x4
x6
x6
x6
x2
x1
x1
x1
x6
x6
x6
x2
x1
x1
x1
x1
x6































































































































































































































































































































I





I


I





































// 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 typed arrays. */
export const typedArray = is.union([
  is.instanceof(Int8Array),
  is.instanceof(Uint8Array),
  is.instanceof(Uint8ClampedArray),
  is.instanceof(Int16Array),
  is.instanceof(Uint16Array),
  is.instanceof(Int32Array),
  is.instanceof(Uint32Array),
  is.instanceof(Float16Array),
  is.instanceof(Float32Array),
  is.instanceof(Float64Array),
  is.instanceof(BigInt64Array),
  is.instanceof(BigUint64Array),
]) as is.ZodUnion<
  readonly [
    is.ZodCustom<Int8Array, Int8Array>,
    is.ZodCustom<Uint8Array, Uint8Array>,
    is.ZodCustom<Uint8ClampedArray, Uint8ClampedArray>,
    is.ZodCustom<Int16Array, Int16Array>,
    is.ZodCustom<Uint16Array, Uint16Array>,
    is.ZodCustom<Int32Array, Int32Array>,
    is.ZodCustom<Uint32Array, Uint32Array>,
    is.ZodCustom<Float16Array, Float16Array>,
    is.ZodCustom<Float32Array, Float32Array>,
    is.ZodCustom<Float64Array, Float64Array>,
    is.ZodCustom<BigInt64Array, BigInt64Array>,
    is.ZodCustom<BigUint64Array, BigUint64Array>,
  ]
>

/** Type alias for URLs with supported protocols. */
export const url = Object.assign(is.url({ protocol: /^https?|file|wasm|data|blob|jsr|npm$/, hostname: /.*/ }), {
  with(protocols: string[], { hostname = /.*/, ...options }: Exclude<is.z.core.$ZodURLParams, "protocol"> = {}) {
    return is.url({ protocol: new RegExp(`^https?|file|wasm|data|blob|jsr|npm|${protocols.join("|")}$`), hostname, ...options })
  },
}) as is.ZodURL & {
  /** Create a new type alias for URLs with additional custom supported protocols. */
  with(protocols: string[], params?: Exclude<is.z.core.$ZodURLParams, "protocol">): is.ZodURL
}

/** Type alias for body init. */
export const bodyInit = is.union([
  is.string(),
  is.instanceof(String),
  is.instanceof(Blob),
  is.instanceof(ArrayBuffer),
  is.instanceof(DataView),
  is.instanceof(FormData),
  is.instanceof(ReadableStream),
  is.instanceof(URLSearchParams),
  typedArray,
]).nullable() as is.ZodNullable<
  is.ZodUnion<
    readonly [
      is.ZodString,
      is.ZodCustom<String, String>,
      is.ZodCustom<Blob, Blob>,
      is.ZodCustom<ArrayBuffer, ArrayBuffer>,
      is.ZodCustom<DataView, DataView>,
      is.ZodCustom<FormData, FormData>,
      is.ZodCustom<ReadableStream, ReadableStream>,
      is.ZodCustom<URLSearchParams, URLSearchParams>,
      typeof typedArray,
    ]
  >
>

/**
 * System permissions flags.
 *
 * See {@link https://docs.deno.com/runtime/fundamentals/security/#system-information}.
 */
const sysflags = ["hostname", "osRelease", "osUptime", "loadavg", "networkInterfaces", "systemMemoryInfo", "uid", "gid"] as const

/** Type alias factory for permission descriptors. */
function permission({ expand = false, urls = false, sys = false } = {}) {
  return is.preprocess((value) => {
    if (value === "inherit") {
      return value
    }
    if (typeof value === "string") {
      value = [value]
    }
    if (expand) {
      if ((value === undefined) || (value === false)) {
        value = []
      }
    }
    if (urls && Array.isArray(value)) {
      value = value.map((value) => value instanceof URL ? value.href : value)
    }
    return value
  }, is.union([is.literal("inherit"), is.boolean(), is.array(sys ? is.enum(sysflags) : is.string())]))
}

/** Type alias factory for permissions object. */
export function permissions<T extends keyof Deno.PermissionOptionsObject>(options?: { set?: T[]; expand?: false }): is.ZodObject<Record<T, is.ZodOptional<is.ZodUnion<[is.ZodLiteral<"inherit">, is.ZodBoolean, is.ZodArray<is.ZodString>]>>>>
/** Type alias factory for permissions object. */
export function permissions<T extends keyof Deno.PermissionOptionsObject>(options: { set?: T[]; expand: true }): is.ZodObject<Record<T, is.ZodUnion<[is.ZodLiteral<"inherit">, is.ZodLiteral<true>, is.ZodArray<is.ZodString>]>>>
export function permissions<T extends keyof Deno.PermissionOptionsObject>({ set = ["read", "write", "net", "env", "run", "sys", "ffi", "import"] as T[], expand = false }: { set?: T[]; expand?: boolean } = {}) {
  const validator = is.object({
    read: permission({ expand, urls: true }),
    write: permission({ expand, urls: true }),
    net: permission({ expand }),
    env: permission({ expand }),
    sys: permission({ expand, sys: true }),
    run: permission({ expand, urls: true }),
    ffi: permission({ expand, urls: true }),
    import: permission({ expand }),
  }).pick<any>(Object.fromEntries(set.map((key) => [key, true]))).strict()

  // Return either the compact or expanded version
  if (!expand) {
    return validator.partial().or(is.union([is.literal("inherit"), is.literal("none")]))
  }
  return is.preprocess((value) => {
    if ((value === "inherit") || (value === "none")) {
      return Object.fromEntries(set.map((key) => [key, value === "none" ? [] : value]))
    }
    if (typeof value === "string") {
      value = [value]
    }
    if (Array.isArray(value)) {
      return { ...Object.fromEntries(set.map((permission) => [permission, false])), ...Object.fromEntries(value.map((permission) => [permission, "inherit"])) }
    }
    return value
  }, validator) as unknown
}

/** 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 unknown as is.ZodCustom<Function, unknown>

/** 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<
  is.ZodType,
  unknown
>

/** Object that ressemble a ZodError. */
type ZodErrorLike = { path?: PropertyKey[]; message: string; errors?: ZodErrorLike[][] }

/** Convert a property key path to a string. */
function toPath(path: PropertyKey[] = []): string {
  const segments = []
  for (const segment of path) {
    if (typeof segment === "number") {
      segments.push(`[${segment}]`)
    } else if (typeof segment === "symbol" || /[^\w$]/.test(String(segment))) {
      segments.push(`[${JSON.stringify(String(segment))}]`)
    } else {
      segments.push(`.${segment}`)
    }
  }
  return segments.join("")
}

/** Prettify validation issues in a more detailed way. */
function prettifyIssue(errors: ZodErrorLike | ZodErrorLike[], { indent = "" } = {}): string {
  const lines = []
  if (!Array.isArray(errors)) {
    errors = [errors]
  }
  errors.sort((a, b) => (a.path ?? []).length - (b.path ?? []).length)
  for (const error of errors) {
    lines.push(`${indent}${error.message}`)
    if (error.path?.length) {
      lines.push(`${indent}  → at ${toPath(error.path)}`)
    }
    if (error.errors?.length) {
      lines.push(`${indent}  → tried to fit into one of the allowed types:`)
      for (const suberror of error.errors) {
        lines.push(prettifyIssue(suberror, { indent: `${indent}      ` }).replace("  ✘", "- ✘"))
      }
    }
  }
  return lines.join("\n")
}

/** Options for {@linkcode parse()}. */
export type ParseOptions = {
  /** Whether to parse asynchronously. */
  async?: boolean
  /** Whether to throw a `ZodError` rather than a `TypeError` in case of failure */
  zodError?: boolean
}

/** Parse a schema synchronously. */
export function parse<T extends is.ZodType>(schema: T, values: unknown, options?: Exclude<ParseOptions, "async"> & { async?: false }): is.infer<T>
/** Parse a schema asynchronously. */
export function parse<T extends is.ZodType>(schema: T, values: unknown, options?: Exclude<ParseOptions, "async"> & { async?: true }): Promise<is.infer<T>>
export function parse<T extends is.ZodType>(schema: T, values: unknown, { async = false, zodError }: ParseOptions = {}) {
  try {
    return async
      ? schema.parseAsync(values).catch((error) => {
        if ((!(error instanceof is.ZodError)) || zodError) {
          throw error
        }
        throw new TypeError(`Validation failed: \n${prettifyIssue(error.issues)}`)
      })
      : schema.parse(values)
  } catch (error) {
    if ((!(error instanceof is.ZodError)) || zodError) {
      throw error
    }
    throw new TypeError(`Validation failed: \n${prettifyIssue(error.issues)}`)
  }
}