typenative 0.5.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/.github/workflows/ci.yml +2 -2
  2. package/.github/workflows/npm-publish-github-packages.yml +2 -2
  3. package/CHANGELOG.md +339 -0
  4. package/CONTRIBUTION.md +2 -1
  5. package/README.md +16 -7
  6. package/bin/cli.js +1 -0
  7. package/bin/index.js +62 -3
  8. package/bin/main.js +1 -0
  9. package/bin/node/core.js +46 -0
  10. package/bin/node/events.js +154 -15
  11. package/bin/node/http.js +1265 -127
  12. package/bin/node/process.js +13 -0
  13. package/bin/node/punycode.js +2 -2
  14. package/bin/node/stream.js +56 -7
  15. package/bin/node/stream_web.js +2 -2
  16. package/bin/node/timers.js +4 -2
  17. package/bin/node/url.js +28 -3
  18. package/bin/parse-node.js +2 -2
  19. package/bin/regex_vm.js +2105 -0
  20. package/bin/transpiler.js +17970 -4419
  21. package/bin/typecheck-node.js +2 -1
  22. package/bin/version.js +1 -1
  23. package/conformance.md +57 -15
  24. package/examples/README.md +50 -28
  25. package/examples/angular-templates/README.md +141 -0
  26. package/examples/angular-templates/bench.mjs +173 -0
  27. package/examples/angular-templates/dom-type-globals.d.ts +21 -0
  28. package/examples/angular-templates/dom-type-stubs.ts +21 -0
  29. package/examples/angular-templates/main.ts +135 -0
  30. package/examples/angular-templates/parity.mjs +157 -0
  31. package/examples/angular-templates/run-node.mjs +69 -0
  32. package/examples/angular-templates/vendor/angular/compiler/LICENSE +21 -0
  33. package/examples/desktop-notes/README.md +11 -13
  34. package/examples/dupe-finder/README.md +7 -5
  35. package/examples/fibonacci/README.md +7 -7
  36. package/examples/hello-world/README.md +6 -6
  37. package/examples/json-server/README.md +22 -18
  38. package/examples/mandelbrot-png/README.md +11 -13
  39. package/examples/marked-cli/README.md +60 -44
  40. package/examples/marked-cli/main.ts +122 -4
  41. package/examples/openai-chat/README.md +148 -0
  42. package/examples/openai-chat/bench.mjs +175 -0
  43. package/examples/openai-chat/main.ts +488 -0
  44. package/examples/react-compiler/README.md +185 -0
  45. package/examples/react-compiler/bench.mjs +181 -0
  46. package/examples/react-compiler/main.ts +324 -0
  47. package/examples/react-compiler/parity.mjs +362 -0
  48. package/examples/react-compiler/run-node.mjs +90 -0
  49. package/examples/react-compiler/vendor/babel/LICENSE +22 -0
  50. package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-lower.json +15 -0
  51. package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-upper.json +51 -0
  52. package/examples/react-compiler/vendor/charcodes/package.json +25 -0
  53. package/examples/react-compiler/vendor/invariant/LICENSE +21 -0
  54. package/examples/react-compiler/vendor/invariant/invariant.cjs +51 -0
  55. package/examples/react-compiler/vendor/invariant/invariant.d.cts +11 -0
  56. package/examples/react-compiler/vendor/invariant/types/LICENSE +21 -0
  57. package/examples/react-compiler/vendor/jridgewell/gen-mapping/LICENSE +19 -0
  58. package/examples/react-compiler/vendor/jridgewell/resolve-uri/LICENSE +19 -0
  59. package/examples/react-compiler/vendor/jridgewell/sourcemap-codec/LICENSE +19 -0
  60. package/examples/react-compiler/vendor/jridgewell/trace-mapping/LICENSE +19 -0
  61. package/examples/react-compiler/vendor/js-tokens/LICENSE +21 -0
  62. package/examples/react-compiler/vendor/js-tokens/index.cjs +23 -0
  63. package/examples/react-compiler/vendor/js-tokens/index.d.cts +13 -0
  64. package/examples/react-compiler/vendor/jsesc/LICENSE-MIT.txt +20 -0
  65. package/examples/react-compiler/vendor/jsesc/jsesc.cjs +337 -0
  66. package/examples/react-compiler/vendor/jsesc/jsesc.d.cts +384 -0
  67. package/examples/react-compiler/vendor/jsesc/types/LICENSE +21 -0
  68. package/examples/react-compiler/vendor/picocolors/LICENSE +15 -0
  69. package/examples/react-compiler/vendor/picocolors/picocolors.cjs +75 -0
  70. package/examples/react-compiler/vendor/picocolors/picocolors.d.cts +5 -0
  71. package/examples/react-compiler/vendor/picocolors/types.d.ts +51 -0
  72. package/examples/react-compiler/vendor/react-compiler/LICENSE +21 -0
  73. package/examples/react-compiler/vendor/stubs/babel-core.ts +31 -0
  74. package/examples/react-compiler/vendor/stubs/debug.ts +18 -0
  75. package/examples/react-compiler/vendor/stubs/js-tokens.d.ts +13 -0
  76. package/examples/react-compiler/vendor/stubs/pretty-format.ts +18 -0
  77. package/examples/react-compiler/vendor/zod/LICENSE +21 -0
  78. package/examples/react-compiler/vendor/zod-validation-error/LICENSE +9 -0
  79. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/NonEmptyArray.ts +5 -0
  80. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinPath.ts +49 -0
  81. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinValues.ts +48 -0
  82. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/stringify.ts +50 -0
  83. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/titleCase.ts +6 -0
  84. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/MessageBuilder.ts +126 -0
  85. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/ValidationError.ts +39 -0
  86. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/custom.ts +12 -0
  87. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/errorMap.ts +78 -0
  88. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/index.ts +2 -0
  89. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidElement.ts +12 -0
  90. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidKey.ts +12 -0
  91. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidStringFormat.ts +131 -0
  92. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidType.ts +54 -0
  93. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidUnion.ts +12 -0
  94. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidValue.ts +40 -0
  95. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/notMultipleOf.ts +12 -0
  96. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooBig.ts +76 -0
  97. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooSmall.ts +76 -0
  98. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/types.ts +27 -0
  99. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/unrecognizedKeys.ts +27 -0
  100. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromError.ts +13 -0
  101. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodError.ts +61 -0
  102. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodIssue.ts +38 -0
  103. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/index.ts +20 -0
  104. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationError.ts +5 -0
  105. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationErrorLike.ts +8 -0
  106. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isZodErrorLike.ts +11 -0
  107. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/toValidationError.ts +20 -0
  108. package/examples/react-compiler/vendor.mjs +688 -0
  109. package/examples/todo-cli/README.md +9 -9
  110. package/examples/yaml-cli/README.md +50 -34
  111. package/examples/yaml-cli/main.ts +236 -14
  112. package/package.json +8 -5
  113. package/performance/README.md +196 -0
  114. package/performance/any-dynamic.ts +21 -0
  115. package/performance/any-record-view.ts +20 -0
  116. package/performance/arr-hof.ts +21 -0
  117. package/performance/arr-push-index.ts +15 -0
  118. package/performance/arr-sort.ts +21 -0
  119. package/performance/async-await.ts +24 -0
  120. package/performance/class-dispatch.ts +52 -0
  121. package/performance/events-emit.ts +27 -0
  122. package/performance/exc-throw-rare.ts +25 -0
  123. package/performance/exc-try-catch.ts +26 -0
  124. package/performance/fn-closures.ts +29 -0
  125. package/performance/fn-recursion-tree.ts +31 -0
  126. package/performance/gen-for-of.ts +46 -0
  127. package/performance/json-roundtrip.ts +27 -0
  128. package/performance/map-delete.ts +24 -0
  129. package/performance/map-string-keys.ts +22 -0
  130. package/performance/num-arith-loop.ts +14 -0
  131. package/performance/num-bitwise.ts +21 -0
  132. package/performance/num-mod-int.ts +16 -0
  133. package/performance/num-to-string.ts +16 -0
  134. package/performance/obj-alloc.ts +32 -0
  135. package/performance/regex-exec.ts +29 -0
  136. package/performance/regex-site.ts +28 -0
  137. package/performance/set-ops.ts +20 -0
  138. package/performance/str-char-scan.ts +35 -0
  139. package/performance/str-concat-plus.ts +18 -0
  140. package/performance/str-concat.ts +24 -0
  141. package/performance/str-short-scan.ts +26 -0
  142. package/performance/str-split-join.ts +23 -0
  143. package/performance/str-template.ts +16 -0
  144. package/performance/union-switch.ts +57 -0
  145. package/tsparser/check.go +2 -1
  146. package/tsparser/main.go +1 -0
  147. package/types/typenative-npm.d.ts +3 -2
  148. package/types/typenative.d.ts +42 -12
  149. package/ROUND2-HANDOFF.md +0 -200
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Colin McDonnell
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,9 @@
1
+ (The MIT License)
2
+
3
+ Copyright 2022 Causaly, Inc <front-end@causaly.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,5 @@
1
+ export type NonEmptyArray<T> = [T, ...T[]];
2
+
3
+ export function isNonEmptyArray<T>(value: T[]): value is NonEmptyArray<T> {
4
+ return value.length !== 0;
5
+ }
@@ -0,0 +1,49 @@
1
+ import { stringifySymbol } from './stringify.ts';
2
+ import type { NonEmptyArray } from './NonEmptyArray.ts';
3
+
4
+ /**
5
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#identifiers
6
+ */
7
+ const identifierRegex = /[$_\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*/u;
8
+
9
+ export function joinPath(path: NonEmptyArray<PropertyKey>): string {
10
+ if (path.length === 1) {
11
+ let propertyKey = path[0];
12
+
13
+ if (typeof propertyKey === 'symbol') {
14
+ propertyKey = stringifySymbol(propertyKey);
15
+ }
16
+
17
+ return propertyKey.toString() || '""';
18
+ }
19
+
20
+ return path.reduce<string>((acc, propertyKey) => {
21
+ // handle numeric indices
22
+ if (typeof propertyKey === 'number') {
23
+ return acc + '[' + propertyKey.toString() + ']';
24
+ }
25
+
26
+ // handle symbols
27
+ if (typeof propertyKey === 'symbol') {
28
+ propertyKey = stringifySymbol(propertyKey);
29
+ }
30
+
31
+ // handle quoted values
32
+ if (propertyKey.includes('"')) {
33
+ return acc + '["' + escapeQuotes(propertyKey) + '"]';
34
+ }
35
+
36
+ // handle special characters
37
+ if (!identifierRegex.test(propertyKey)) {
38
+ return acc + '["' + propertyKey + '"]';
39
+ }
40
+
41
+ // handle normal values
42
+ const separator = acc.length === 0 ? '' : '.';
43
+ return acc + separator + propertyKey;
44
+ }, '');
45
+ }
46
+
47
+ function escapeQuotes(str: string): string {
48
+ return str.replace(/"/g, '\\"');
49
+ }
@@ -0,0 +1,48 @@
1
+ import { stringify } from './stringify.ts';
2
+ import type { util } from '../../../zod/src/v4/core/index.ts';
3
+
4
+ export type JoinValuesOptions = {
5
+ separator: string;
6
+ lastSeparator?: string;
7
+ wrapStringValuesInQuote?: boolean;
8
+ maxValuesToDisplay?: number;
9
+ };
10
+
11
+ export function joinValues(
12
+ values: Array<util.Primitive>,
13
+ options: JoinValuesOptions
14
+ ): string {
15
+ const valuesToDisplay = (
16
+ options.maxValuesToDisplay
17
+ ? values.slice(0, options.maxValuesToDisplay)
18
+ : values
19
+ ).map((value) => {
20
+ return stringify(value, {
21
+ wrapStringValueInQuote: options.wrapStringValuesInQuote,
22
+ });
23
+ });
24
+
25
+ // add remaining values count (if any)
26
+ // this is to avoid displaying too many values in the error message
27
+ // and to keep the message concise
28
+ // e.g. `"foo", "bar", "baz" or 3 more value(s)`
29
+ if (valuesToDisplay.length < values.length) {
30
+ valuesToDisplay.push(
31
+ `${values.length - valuesToDisplay.length} more value(s)`
32
+ );
33
+ }
34
+
35
+ return valuesToDisplay.reduce<string>((acc, value, index) => {
36
+ if (index > 0) {
37
+ if (index === valuesToDisplay.length - 1 && options.lastSeparator) {
38
+ acc += options.lastSeparator;
39
+ } else {
40
+ acc += options.separator;
41
+ }
42
+ }
43
+
44
+ acc += value;
45
+
46
+ return acc;
47
+ }, '');
48
+ }
@@ -0,0 +1,50 @@
1
+ import type { util } from '../../../zod/src/v4/core/index.ts';
2
+
3
+ export function stringifySymbol(symbol: symbol): string {
4
+ return symbol.description ?? '';
5
+ }
6
+
7
+ export type StringifyValueOptions = {
8
+ wrapStringValueInQuote?: boolean;
9
+ localization?: boolean | Intl.LocalesArgument;
10
+ };
11
+
12
+ export function stringify(
13
+ value: util.Primitive | Date,
14
+ options: StringifyValueOptions = {}
15
+ ): string {
16
+ switch (typeof value) {
17
+ case 'symbol':
18
+ return stringifySymbol(value);
19
+ case 'bigint':
20
+ case 'number': {
21
+ switch (options.localization) {
22
+ case true:
23
+ return value.toLocaleString();
24
+ case false:
25
+ return value.toString();
26
+ default:
27
+ return value.toLocaleString(options.localization);
28
+ }
29
+ }
30
+ case 'string': {
31
+ if (options.wrapStringValueInQuote) {
32
+ return `"${value}"`;
33
+ }
34
+ return value;
35
+ }
36
+ default: {
37
+ if (value instanceof Date) {
38
+ switch (options.localization) {
39
+ case true:
40
+ return value.toLocaleString();
41
+ case false:
42
+ return value.toISOString();
43
+ default:
44
+ return value.toLocaleString(options.localization);
45
+ }
46
+ }
47
+ return String(value);
48
+ }
49
+ }
50
+ }
@@ -0,0 +1,6 @@
1
+ export function titleCase(value: string): string {
2
+ if (value.length === 0) {
3
+ return value;
4
+ }
5
+ return value.charAt(0).toUpperCase() + value.slice(1);
6
+ }
@@ -0,0 +1,126 @@
1
+ import { joinPath } from '../utils/joinPath.ts';
2
+ import { isNonEmptyArray, type NonEmptyArray } from '../utils/NonEmptyArray.ts';
3
+ import { titleCase } from '../utils/titleCase.ts';
4
+ import type * as zod from '../../../zod/src/v4/core/index.ts';
5
+
6
+ export type ZodIssue = zod.$ZodIssue;
7
+
8
+ export type MessageBuilder = (issues: NonEmptyArray<ZodIssue>) => string;
9
+
10
+ export type MessageBuilderOptions = {
11
+ prefix: string | null | undefined;
12
+ prefixSeparator: string;
13
+ maxIssuesInMessage: number;
14
+ issueSeparator: string;
15
+ unionSeparator: string;
16
+ includePath: boolean;
17
+ forceTitleCase: boolean;
18
+ };
19
+
20
+ export const defaultMessageBuilderOptions: MessageBuilderOptions & {
21
+ prefix: string;
22
+ } = {
23
+ prefix: 'Validation error',
24
+ prefixSeparator: ': ',
25
+ maxIssuesInMessage: 99, // I've got 99 problems but the b$tch ain't one
26
+ unionSeparator: ' or ',
27
+ issueSeparator: '; ',
28
+ includePath: true,
29
+ forceTitleCase: true,
30
+ };
31
+
32
+ export function createMessageBuilder(
33
+ partialOptions: Partial<MessageBuilderOptions> = {}
34
+ ): MessageBuilder {
35
+ const options = {
36
+ ...defaultMessageBuilderOptions,
37
+ ...partialOptions,
38
+ };
39
+
40
+ return function messageBuilder(issues) {
41
+ const message = issues
42
+ // limit max number of issues printed in the reason section
43
+ .slice(0, options.maxIssuesInMessage)
44
+ // format error message
45
+ .map((issue) => mapIssue(issue, options))
46
+ // concat as string
47
+ .join(options.issueSeparator);
48
+
49
+ return conditionallyPrefixMessage(message, options);
50
+ };
51
+ }
52
+
53
+ function mapIssue(
54
+ issue: zod.$ZodIssue,
55
+ options: MessageBuilderOptions
56
+ ): string {
57
+ if (issue.code === 'invalid_union' && isNonEmptyArray(issue.errors)) {
58
+ const individualMessages = issue.errors.map((issues) =>
59
+ issues
60
+ .map((subIssue) =>
61
+ mapIssue(
62
+ {
63
+ ...subIssue,
64
+ path: issue.path.concat(subIssue.path),
65
+ },
66
+ options
67
+ )
68
+ )
69
+ .join(options.issueSeparator)
70
+ );
71
+
72
+ // deduplicate messages
73
+ // and join them with the union separator
74
+ // to create a single message for the invalid union issue
75
+ return Array.from(new Set(individualMessages)).join(options.unionSeparator);
76
+ }
77
+
78
+ const buf = [];
79
+
80
+ if (options.forceTitleCase) {
81
+ buf.push(titleCase(issue.message));
82
+ } else {
83
+ buf.push(issue.message);
84
+ }
85
+
86
+ pathCondition: if (
87
+ options.includePath &&
88
+ issue.path !== undefined &&
89
+ isNonEmptyArray(issue.path)
90
+ ) {
91
+ // handle array indices
92
+ if (issue.path.length === 1) {
93
+ const identifier = issue.path[0];
94
+
95
+ if (typeof identifier === 'number') {
96
+ buf.push(` at index ${identifier}`);
97
+ break pathCondition;
98
+ }
99
+ }
100
+
101
+ buf.push(` at "${joinPath(issue.path)}"`);
102
+ }
103
+
104
+ return buf.join('');
105
+ }
106
+
107
+ function conditionallyPrefixMessage(
108
+ message: string,
109
+ options: Pick<MessageBuilderOptions, 'prefix' | 'prefixSeparator'>
110
+ ): string {
111
+ if (options.prefix != null) {
112
+ if (message.length > 0) {
113
+ return [options.prefix, message].join(options.prefixSeparator);
114
+ }
115
+
116
+ return options.prefix;
117
+ }
118
+
119
+ if (message.length > 0) {
120
+ return message;
121
+ }
122
+
123
+ // if both reason and prefix are empty, return default prefix
124
+ // to avoid having an empty error message
125
+ return defaultMessageBuilderOptions.prefix;
126
+ }
@@ -0,0 +1,39 @@
1
+ import { isZodErrorLike } from './isZodErrorLike.ts';
2
+ import type * as zod from '../../../zod/src/v4/core/index.ts';
3
+
4
+ export const ZOD_VALIDATION_ERROR_NAME = 'ZodValidationError';
5
+
6
+ // make zod-validation-error compatible with
7
+ // earlier to es2022 typescript configurations
8
+ // @see https://github.com/causaly/zod-validation-error/issues/226
9
+ export interface ErrorOptions {
10
+ cause?: unknown;
11
+ }
12
+
13
+ export class ValidationError extends Error {
14
+ name: typeof ZOD_VALIDATION_ERROR_NAME;
15
+ details: Array<zod.$ZodIssue>;
16
+
17
+ constructor(message?: string, options?: ErrorOptions) {
18
+ super(message, options);
19
+ this.name = ZOD_VALIDATION_ERROR_NAME;
20
+ this.details = getIssuesFromErrorOptions(options);
21
+ }
22
+
23
+ toString(): string {
24
+ return this.message;
25
+ }
26
+ }
27
+
28
+ function getIssuesFromErrorOptions(
29
+ options?: ErrorOptions
30
+ ): Array<zod.$ZodIssue> {
31
+ if (options) {
32
+ const cause = options.cause;
33
+ if (isZodErrorLike(cause)) {
34
+ return cause.issues;
35
+ }
36
+ }
37
+
38
+ return [];
39
+ }
@@ -0,0 +1,12 @@
1
+ import type * as zod from '../../../../zod/src/v4/core/index.ts';
2
+ import type { AbstractSyntaxTree } from './types.ts';
3
+
4
+ export function parseCustomIssue(
5
+ issue: zod.$ZodRawIssue<zod.$ZodIssueCustom>
6
+ ): AbstractSyntaxTree {
7
+ return {
8
+ type: issue.code,
9
+ path: issue.path,
10
+ message: issue.message ?? 'Invalid input',
11
+ };
12
+ }
@@ -0,0 +1,78 @@
1
+ import { parseCustomIssue } from './custom.ts';
2
+ import { parseInvalidElementIssue } from './invalidElement.ts';
3
+ import { parseInvalidKeyIssue } from './invalidKey.ts';
4
+ import { parseInvalidStringFormatIssue } from './invalidStringFormat.ts';
5
+ import { parseInvalidTypeIssue } from './invalidType.ts';
6
+ import { parseInvalidUnionIssue } from './invalidUnion.ts';
7
+ import { parseInvalidValueIssue } from './invalidValue.ts';
8
+ import { parseNotMultipleOfIssue } from './notMultipleOf.ts';
9
+ import { parseTooBigIssue } from './tooBig.ts';
10
+ import { parseTooSmallIssue } from './tooSmall.ts';
11
+ import { parseUnrecognizedKeysIssue } from './unrecognizedKeys.ts';
12
+ import type {
13
+ AbstractSyntaxTree,
14
+ ErrorMapOptions,
15
+ IssueType,
16
+ } from './types.ts';
17
+ import type * as zod from '../../../../zod/src/v4/core/index.ts';
18
+
19
+ type IssueParsers = {
20
+ [IssueCode in IssueType]: (
21
+ issue: zod.$ZodRawIssue<Extract<zod.$ZodIssue, { code: IssueCode }>>,
22
+ options: ErrorMapOptions
23
+ ) => AbstractSyntaxTree;
24
+ };
25
+
26
+ const issueParsers: IssueParsers = {
27
+ invalid_type: parseInvalidTypeIssue,
28
+ too_big: parseTooBigIssue,
29
+ too_small: parseTooSmallIssue,
30
+ invalid_format: parseInvalidStringFormatIssue,
31
+ invalid_value: parseInvalidValueIssue,
32
+ invalid_element: parseInvalidElementIssue,
33
+ not_multiple_of: parseNotMultipleOfIssue,
34
+ unrecognized_keys: parseUnrecognizedKeysIssue,
35
+ invalid_key: parseInvalidKeyIssue,
36
+ custom: parseCustomIssue,
37
+ invalid_union: parseInvalidUnionIssue,
38
+ };
39
+
40
+ export const defaultErrorMapOptions = {
41
+ displayInvalidFormatDetails: false,
42
+ allowedValuesSeparator: ', ',
43
+ allowedValuesLastSeparator: ' or ',
44
+ wrapAllowedValuesInQuote: true,
45
+ maxAllowedValuesToDisplay: 10,
46
+ unrecognizedKeysSeparator: ', ',
47
+ unrecognizedKeysLastSeparator: ' and ',
48
+ wrapUnrecognizedKeysInQuote: true,
49
+ maxUnrecognizedKeysToDisplay: 5,
50
+ dateLocalization: true,
51
+ numberLocalization: true,
52
+ } as const satisfies ErrorMapOptions;
53
+
54
+ export function createErrorMap(
55
+ partialOptions: Partial<ErrorMapOptions> = {}
56
+ ): zod.$ZodErrorMap<zod.$ZodIssue> {
57
+ // fill-in default options
58
+ const options = {
59
+ ...defaultErrorMapOptions,
60
+ ...partialOptions,
61
+ };
62
+
63
+ const errorMap: zod.$ZodErrorMap<zod.$ZodIssue> = (issue) => {
64
+ if (issue.code === undefined) {
65
+ // TODO: handle this case
66
+ return 'Not supported issue type';
67
+ }
68
+
69
+ const parseFunc = issueParsers[issue.code] as (
70
+ iss: typeof issue,
71
+ opts: ErrorMapOptions
72
+ ) => AbstractSyntaxTree;
73
+ const ast = parseFunc(issue, options);
74
+ return ast.message;
75
+ };
76
+
77
+ return errorMap;
78
+ }
@@ -0,0 +1,2 @@
1
+ export { createErrorMap, defaultErrorMapOptions } from './errorMap.ts';
2
+ export type { ErrorMapOptions } from './types.ts';
@@ -0,0 +1,12 @@
1
+ import type * as zod from '../../../../zod/src/v4/core/index.ts';
2
+ import type { AbstractSyntaxTree } from './types.ts';
3
+
4
+ export function parseInvalidElementIssue(
5
+ issue: zod.$ZodRawIssue<zod.$ZodIssueInvalidElement>
6
+ ): AbstractSyntaxTree {
7
+ return {
8
+ type: issue.code,
9
+ path: issue.path,
10
+ message: `unexpected element in ${issue.origin}`,
11
+ };
12
+ }
@@ -0,0 +1,12 @@
1
+ import type * as zod from '../../../../zod/src/v4/core/index.ts';
2
+ import type { AbstractSyntaxTree } from './types.ts';
3
+
4
+ export function parseInvalidKeyIssue(
5
+ issue: zod.$ZodRawIssue<zod.$ZodIssueInvalidKey>
6
+ ): AbstractSyntaxTree {
7
+ return {
8
+ type: issue.code,
9
+ path: issue.path,
10
+ message: `unexpected key in ${issue.origin}`,
11
+ };
12
+ }
@@ -0,0 +1,131 @@
1
+ import type { AbstractSyntaxTree, ErrorMapOptions } from './types.ts';
2
+ import type * as zod from '../../../../zod/src/v4/core/index.ts';
3
+
4
+ export function parseInvalidStringFormatIssue(
5
+ issue: zod.$ZodRawIssue<zod.$ZodIssueInvalidStringFormat>,
6
+ options: Pick<ErrorMapOptions, 'displayInvalidFormatDetails'> = {
7
+ displayInvalidFormatDetails: false,
8
+ }
9
+ ): AbstractSyntaxTree {
10
+ switch (issue.format) {
11
+ case 'lowercase':
12
+ case 'uppercase':
13
+ return {
14
+ type: issue.code,
15
+ path: issue.path,
16
+ message: `value must be in ${issue.format} format`,
17
+ };
18
+ default: {
19
+ if (isZodIssueStringStartsWith(issue)) {
20
+ return parseStringStartsWith(issue);
21
+ }
22
+ if (isZodIssueStringEndsWith(issue)) {
23
+ return parseStringEndsWith(issue);
24
+ }
25
+ if (isZodIssueStringIncludes(issue)) {
26
+ return parseStringIncludes(issue);
27
+ }
28
+ if (isZodIssueStringInvalidRegex(issue)) {
29
+ return parseStringInvalidRegex(issue, options);
30
+ }
31
+ if (isZodIssueStringInvalidJWT(issue)) {
32
+ return parseStringInvalidJWT(issue, options);
33
+ }
34
+
35
+ return {
36
+ type: issue.code,
37
+ path: issue.path,
38
+ message: `invalid ${issue.format}`,
39
+ };
40
+ }
41
+ }
42
+ }
43
+ function isZodIssueStringStartsWith(
44
+ issue: zod.$ZodRawIssue<zod.$ZodIssueInvalidStringFormat>
45
+ ): issue is zod.$ZodRawIssue<zod.$ZodIssueStringStartsWith> {
46
+ return issue.format === 'starts_with';
47
+ }
48
+
49
+ function parseStringStartsWith(
50
+ issue: zod.$ZodRawIssue<zod.$ZodIssueStringStartsWith>
51
+ ): AbstractSyntaxTree {
52
+ return {
53
+ type: issue.code,
54
+ path: issue.path,
55
+ message: `value must start with "${issue.prefix}"`,
56
+ };
57
+ }
58
+
59
+ function isZodIssueStringEndsWith(
60
+ issue: zod.$ZodRawIssue<zod.$ZodIssueInvalidStringFormat>
61
+ ): issue is zod.$ZodRawIssue<zod.$ZodIssueStringEndsWith> {
62
+ return issue.format === 'ends_with';
63
+ }
64
+ function parseStringEndsWith(
65
+ issue: zod.$ZodRawIssue<zod.$ZodIssueStringEndsWith>
66
+ ): AbstractSyntaxTree {
67
+ return {
68
+ type: issue.code,
69
+ path: issue.path,
70
+ message: `value must end with "${issue.suffix}"`,
71
+ };
72
+ }
73
+
74
+ function isZodIssueStringIncludes(
75
+ issue: zod.$ZodRawIssue<zod.$ZodIssueInvalidStringFormat>
76
+ ): issue is zod.$ZodRawIssue<zod.$ZodIssueStringIncludes> {
77
+ return issue.format === 'includes';
78
+ }
79
+ function parseStringIncludes(
80
+ issue: zod.$ZodRawIssue<zod.$ZodIssueStringIncludes>
81
+ ): AbstractSyntaxTree {
82
+ return {
83
+ type: issue.code,
84
+ path: issue.path,
85
+ message: `value must include "${issue.includes}"`,
86
+ };
87
+ }
88
+
89
+ function isZodIssueStringInvalidRegex(
90
+ issue: zod.$ZodRawIssue<zod.$ZodIssueInvalidStringFormat>
91
+ ): issue is zod.$ZodRawIssue<zod.$ZodIssueStringInvalidRegex> {
92
+ return issue.format === 'regex';
93
+ }
94
+ function parseStringInvalidRegex(
95
+ issue: zod.$ZodRawIssue<zod.$ZodIssueStringInvalidRegex>,
96
+ options: Pick<ErrorMapOptions, 'displayInvalidFormatDetails'> = {
97
+ displayInvalidFormatDetails: false,
98
+ }
99
+ ): AbstractSyntaxTree {
100
+ let message = 'value must match pattern';
101
+ if (options.displayInvalidFormatDetails) {
102
+ message += ` "${issue.pattern}"`;
103
+ }
104
+
105
+ return {
106
+ type: issue.code,
107
+ path: issue.path,
108
+ message,
109
+ };
110
+ }
111
+
112
+ function isZodIssueStringInvalidJWT(
113
+ issue: zod.$ZodRawIssue<zod.$ZodIssueInvalidStringFormat>
114
+ ): issue is zod.$ZodRawIssue<zod.$ZodIssueStringInvalidJWT> {
115
+ return issue.format === 'jwt';
116
+ }
117
+ function parseStringInvalidJWT(
118
+ issue: zod.$ZodRawIssue<zod.$ZodIssueStringInvalidJWT>,
119
+ options: Pick<ErrorMapOptions, 'displayInvalidFormatDetails'> = {
120
+ displayInvalidFormatDetails: false,
121
+ }
122
+ ): AbstractSyntaxTree {
123
+ return {
124
+ type: issue.code,
125
+ path: issue.path,
126
+ message:
127
+ options.displayInvalidFormatDetails && issue.algorithm
128
+ ? `invalid jwt/${issue.algorithm}`
129
+ : `invalid jwt`,
130
+ };
131
+ }
@@ -0,0 +1,54 @@
1
+ import type { AbstractSyntaxTree } from './types.ts';
2
+ import type * as zod from '../../../../zod/src/v4/core/index.ts';
3
+
4
+ export function parseInvalidTypeIssue(
5
+ issue: zod.$ZodRawIssue<zod.$ZodIssueInvalidType>
6
+ ): AbstractSyntaxTree {
7
+ let message = `expected ${issue.expected}`;
8
+
9
+ // note: it's possible that issue.input is not defined
10
+ if ('input' in issue) {
11
+ message += `, received ${getTypeName(issue.input)}`;
12
+ }
13
+
14
+ return {
15
+ type: issue.code,
16
+ path: issue.path,
17
+ message,
18
+ };
19
+ }
20
+
21
+ export function getTypeName(value: unknown): string {
22
+ if (typeof value === 'object') {
23
+ if (value === null) {
24
+ return 'null';
25
+ }
26
+ if (value === undefined) {
27
+ return 'undefined';
28
+ }
29
+ if (Array.isArray(value)) {
30
+ return 'array';
31
+ }
32
+ if (value instanceof Date) {
33
+ return 'date';
34
+ }
35
+ if (value instanceof RegExp) {
36
+ return 'regexp';
37
+ }
38
+ if (value instanceof Map) {
39
+ return 'map';
40
+ }
41
+ if (value instanceof Set) {
42
+ return 'set';
43
+ }
44
+ if (value instanceof Error) {
45
+ return 'error';
46
+ }
47
+ if (value instanceof Function) {
48
+ return 'function';
49
+ }
50
+ return 'object';
51
+ }
52
+
53
+ return typeof value;
54
+ }
@@ -0,0 +1,12 @@
1
+ import type * as zod from '../../../../zod/src/v4/core/index.ts';
2
+ import type { AbstractSyntaxTree } from './types.ts';
3
+
4
+ export function parseInvalidUnionIssue(
5
+ issue: zod.$ZodRawIssue<zod.$ZodIssueInvalidUnion>
6
+ ): AbstractSyntaxTree {
7
+ return {
8
+ type: issue.code,
9
+ path: issue.path,
10
+ message: issue.message ?? 'Invalid input',
11
+ };
12
+ }