@1nder-labs/schemalint 1.0.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.
- package/LICENSE-APACHE +176 -0
- package/LICENSE-MIT +19 -0
- package/README.md +52 -0
- package/bin/schemalint-zod.js +6 -0
- package/dist/discover.d.ts +43 -0
- package/dist/discover.d.ts.map +1 -0
- package/dist/discover.js +145 -0
- package/dist/discover.js.map +1 -0
- package/dist/discover_ast.d.ts +30 -0
- package/dist/discover_ast.d.ts.map +1 -0
- package/dist/discover_ast.js +202 -0
- package/dist/discover_ast.js.map +1 -0
- package/dist/evaluate.d.ts +18 -0
- package/dist/evaluate.d.ts.map +1 -0
- package/dist/evaluate.js +112 -0
- package/dist/evaluate.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +3 -0
- package/dist/main.js.map +1 -0
- package/dist/server.d.ts +2 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +82 -0
- package/dist/server.js.map +1 -0
- package/dist/target_emit.d.ts +12 -0
- package/dist/target_emit.d.ts.map +1 -0
- package/dist/target_emit.js +111 -0
- package/dist/target_emit.js.map +1 -0
- package/dist/target_imports.d.ts +19 -0
- package/dist/target_imports.d.ts.map +1 -0
- package/dist/target_imports.js +79 -0
- package/dist/target_imports.js.map +1 -0
- package/dist/target_resolution.d.ts +20 -0
- package/dist/target_resolution.d.ts.map +1 -0
- package/dist/target_resolution.js +174 -0
- package/dist/target_resolution.js.map +1 -0
- package/dist/targets.d.ts +5 -0
- package/dist/targets.d.ts.map +1 -0
- package/dist/targets.js +116 -0
- package/dist/targets.js.map +1 -0
- package/index.cjs +504 -0
- package/package.json +34 -0
package/LICENSE-APACHE
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2026 1nder-labs
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# @1nder-labs/schemalint
|
|
2
|
+
|
|
3
|
+
Lint JSON Schema and Zod schemas before OpenAI or Anthropic structured-output APIs reject them.
|
|
4
|
+
|
|
5
|
+
This is the schemalint CLI as a single npm package. Installing it adds a `schemalint` command that downloads the native binary for your platform on first use. The Zod ingestor (TypeScript AST discovery + JSON-RPC server) is bundled in `bin/` and `dist/` so no extra package or install step is needed.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install -g @1nder-labs/schemalint
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Or as a project dev dependency:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install -D @1nder-labs/schemalint
|
|
17
|
+
# bun add -d @1nder-labs/schemalint
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
schemalint check --profile openai.so.2026-04-30 schemas/
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Lint for both OpenAI and Anthropic:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
schemalint check \
|
|
30
|
+
--profile openai.so.2026-04-30 \
|
|
31
|
+
--profile anthropic.so.2026-04-30 \
|
|
32
|
+
schemas/
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Add it as a package script so CI can run it:
|
|
36
|
+
|
|
37
|
+
```json
|
|
38
|
+
{
|
|
39
|
+
"scripts": {
|
|
40
|
+
"schema": "schemalint check --profile openai.so.2026-04-30 schemas/"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Documentation
|
|
46
|
+
|
|
47
|
+
Full docs, profile reference, and Zod configuration guide at
|
|
48
|
+
https://1nder-labs.github.io/schemalint
|
|
49
|
+
|
|
50
|
+
## License
|
|
51
|
+
|
|
52
|
+
MIT OR Apache-2.0
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// ESM entry point for schemalint-zod.
|
|
3
|
+
// Imports from the compiled dist/ so this bin works in a published install
|
|
4
|
+
// without requiring tsx. (In the monorepo dev path, tsx also runs compiled JS fine.)
|
|
5
|
+
import { main } from '../dist/server.js';
|
|
6
|
+
main();
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalize a file-system path to forward slashes.
|
|
3
|
+
*
|
|
4
|
+
* `path.relative()` returns backslash-separated paths on Windows
|
|
5
|
+
* (e.g. `src\\foo.ts`), but picomatch globs use forward slashes.
|
|
6
|
+
* Replacing the OS separator with `/` is a no-op on POSIX and correct
|
|
7
|
+
* on Windows, making the glob filter work on both platforms.
|
|
8
|
+
*
|
|
9
|
+
* The `sep` parameter exists solely for unit-testing Windows paths on a
|
|
10
|
+
* POSIX machine — pass `'\\'` to simulate Windows `path.sep`.
|
|
11
|
+
*/
|
|
12
|
+
export declare function toPosixPath(p: string, sep?: string): string;
|
|
13
|
+
export interface SourceMapEntry {
|
|
14
|
+
file: string;
|
|
15
|
+
line?: number;
|
|
16
|
+
}
|
|
17
|
+
export interface DiscoveredModel {
|
|
18
|
+
name: string;
|
|
19
|
+
module_path: string;
|
|
20
|
+
schema: Record<string, unknown>;
|
|
21
|
+
source_map: Record<string, SourceMapEntry>;
|
|
22
|
+
}
|
|
23
|
+
export interface DiscoveryWarning {
|
|
24
|
+
model: string;
|
|
25
|
+
message: string;
|
|
26
|
+
}
|
|
27
|
+
export interface DiscoverResponse {
|
|
28
|
+
models: DiscoveredModel[];
|
|
29
|
+
warnings: DiscoveryWarning[];
|
|
30
|
+
provider_hint?: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Discover Zod schemas by walking TypeScript ASTs.
|
|
34
|
+
*
|
|
35
|
+
* 1. Reads tsconfig.json to resolve the project file list.
|
|
36
|
+
* 2. Filters files against the user-supplied source glob.
|
|
37
|
+
* 3. Walks each source file's AST looking for `z.object({...})` calls.
|
|
38
|
+
* 4. Extracts property source locations for source map.
|
|
39
|
+
* 5. Dynamically imports each file and evaluates schemas at runtime.
|
|
40
|
+
* 6. Converts schemas to JSON Schema via zod-to-json-schema or native.
|
|
41
|
+
*/
|
|
42
|
+
export declare function discoverZodSchemas(sourceGlob: string): Promise<DiscoverResponse>;
|
|
43
|
+
//# sourceMappingURL=discover.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"discover.d.ts","sourceRoot":"","sources":["../src/discover.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;GAUG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,GAAE,MAAiB,GAAG,MAAM,CAErE;AAQD,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;CAC5C;AAED,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,eAAe,EAAE,CAAC;IAC1B,QAAQ,EAAE,gBAAgB,EAAE,CAAC;IAC7B,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAED;;;;;;;;;GASG;AACH,wBAAsB,kBAAkB,CACtC,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,gBAAgB,CAAC,CAuK3B"}
|
package/dist/discover.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { evaluateSchema, evaluateSyntheticSchema } from './evaluate.js';
|
|
3
|
+
/**
|
|
4
|
+
* Normalize a file-system path to forward slashes.
|
|
5
|
+
*
|
|
6
|
+
* `path.relative()` returns backslash-separated paths on Windows
|
|
7
|
+
* (e.g. `src\\foo.ts`), but picomatch globs use forward slashes.
|
|
8
|
+
* Replacing the OS separator with `/` is a no-op on POSIX and correct
|
|
9
|
+
* on Windows, making the glob filter work on both platforms.
|
|
10
|
+
*
|
|
11
|
+
* The `sep` parameter exists solely for unit-testing Windows paths on a
|
|
12
|
+
* POSIX machine — pass `'\\'` to simulate Windows `path.sep`.
|
|
13
|
+
*/
|
|
14
|
+
export function toPosixPath(p, sep = path.sep) {
|
|
15
|
+
return sep === '/' ? p : p.split(sep).join('/');
|
|
16
|
+
}
|
|
17
|
+
import { buildSourceMapFromObjectLiteral, findExportedSchemaCalls, scanProviderImports, } from './discover_ast.js';
|
|
18
|
+
import { findSchemaTargets } from './targets.js';
|
|
19
|
+
/**
|
|
20
|
+
* Discover Zod schemas by walking TypeScript ASTs.
|
|
21
|
+
*
|
|
22
|
+
* 1. Reads tsconfig.json to resolve the project file list.
|
|
23
|
+
* 2. Filters files against the user-supplied source glob.
|
|
24
|
+
* 3. Walks each source file's AST looking for `z.object({...})` calls.
|
|
25
|
+
* 4. Extracts property source locations for source map.
|
|
26
|
+
* 5. Dynamically imports each file and evaluates schemas at runtime.
|
|
27
|
+
* 6. Converts schemas to JSON Schema via zod-to-json-schema or native.
|
|
28
|
+
*/
|
|
29
|
+
export async function discoverZodSchemas(sourceGlob) {
|
|
30
|
+
const tsModule = await import('typescript');
|
|
31
|
+
const pm = await import('picomatch');
|
|
32
|
+
// Resolve tsconfig.json
|
|
33
|
+
const configPath = tsModule.findConfigFile(process.cwd(), tsModule.sys.fileExists, 'tsconfig.json');
|
|
34
|
+
if (!configPath) {
|
|
35
|
+
throw new Error('No tsconfig.json found in the current project. ' +
|
|
36
|
+
'Run this tool from a TypeScript project root.');
|
|
37
|
+
}
|
|
38
|
+
const configFile = tsModule.readConfigFile(configPath, tsModule.sys.readFile);
|
|
39
|
+
if (configFile.error) {
|
|
40
|
+
throw new Error(`Failed to read tsconfig.json: ${tsModule.flattenDiagnosticMessageText(configFile.error.messageText, '\n')}`);
|
|
41
|
+
}
|
|
42
|
+
const parsedConfig = tsModule.parseJsonConfigFileContent(configFile.config, tsModule.sys, process.cwd());
|
|
43
|
+
const compilerOptions = parsedConfig.options;
|
|
44
|
+
// Resolve the full file list from tsconfig
|
|
45
|
+
let fileNames = parsedConfig.fileNames;
|
|
46
|
+
if (fileNames.length === 0) {
|
|
47
|
+
throw new Error('No source files found in tsconfig.json. Ensure "include" patterns match your project files.');
|
|
48
|
+
}
|
|
49
|
+
// Filter files against the source glob.
|
|
50
|
+
// tsconfig resolves absolute paths, but glob patterns match relative paths.
|
|
51
|
+
// Convert each file to a path relative to the project root before matching.
|
|
52
|
+
const picomatch = typeof pm.default === 'function' ? pm.default : pm;
|
|
53
|
+
if (typeof picomatch !== 'function') {
|
|
54
|
+
throw new Error('Failed to load picomatch: expected a function but got ' +
|
|
55
|
+
typeof picomatch +
|
|
56
|
+
'. Check that picomatch is correctly installed.');
|
|
57
|
+
}
|
|
58
|
+
const isMatch = picomatch(sourceGlob, { dot: true });
|
|
59
|
+
const projectRoot = process.cwd();
|
|
60
|
+
fileNames = fileNames.filter((f) => {
|
|
61
|
+
// Use path.relative so that:
|
|
62
|
+
// 1. Files exactly under projectRoot get a clean relative path ("src/foo.ts")
|
|
63
|
+
// without the boundary bug where startsWith("/repo/app") also matches
|
|
64
|
+
// "/repo/application/foo.ts".
|
|
65
|
+
// 2. Files outside projectRoot (monorepo tsconfig referencing "../shared/…")
|
|
66
|
+
// get a "../"-prefixed path that the caller's glob can match if desired.
|
|
67
|
+
//
|
|
68
|
+
// Normalize to forward slashes before matching: on Windows, path.relative()
|
|
69
|
+
// returns backslash-separated paths (e.g. "src\\foo.ts") but picomatch
|
|
70
|
+
// globs use forward slashes, causing every file to fail to match.
|
|
71
|
+
// toPosixPath() is a no-op on POSIX (path.sep === '/') and correct on Windows.
|
|
72
|
+
const relPath = toPosixPath(path.relative(projectRoot, f));
|
|
73
|
+
return isMatch(relPath);
|
|
74
|
+
});
|
|
75
|
+
if (fileNames.length === 0) {
|
|
76
|
+
return { models: [], warnings: [] };
|
|
77
|
+
}
|
|
78
|
+
// Create program and walk ASTs to discover schemas
|
|
79
|
+
const program = tsModule.createProgram(fileNames, compilerOptions);
|
|
80
|
+
const fileSet = new Set(fileNames);
|
|
81
|
+
const selectedSourceFiles = program.getSourceFiles().filter((sourceFile) => !sourceFile.isDeclarationFile &&
|
|
82
|
+
!sourceFile.fileName.includes('node_modules') &&
|
|
83
|
+
fileSet.has(sourceFile.fileName));
|
|
84
|
+
// Step 0: Scan provider imports for auto-detection
|
|
85
|
+
let providerHint;
|
|
86
|
+
for (const sourceFile of selectedSourceFiles) {
|
|
87
|
+
const hint = scanProviderImports(sourceFile, tsModule);
|
|
88
|
+
if (hint) {
|
|
89
|
+
providerHint = hint;
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
// Step 1: Prefer provider-facing call sites. This catches schemas passed to
|
|
94
|
+
// AI SDK, OpenAI helpers, and Anthropic helper APIs. Legacy exported-schema
|
|
95
|
+
// discovery remains as a fallback for simple projects and explicit schema
|
|
96
|
+
// modules that are not wired to a provider call in the selected source glob.
|
|
97
|
+
const callsiteTargets = findSchemaTargets(program, fileSet, tsModule, compilerOptions);
|
|
98
|
+
const discoveredLocations = [...callsiteTargets];
|
|
99
|
+
const nonFatal = [];
|
|
100
|
+
if (discoveredLocations.length === 0) {
|
|
101
|
+
for (const sourceFile of selectedSourceFiles) {
|
|
102
|
+
const exports = findExportedSchemaCalls(sourceFile, tsModule);
|
|
103
|
+
for (const exp of exports) {
|
|
104
|
+
const sourceMap = buildSourceMapFromObjectLiteral(exp.objectArg, sourceFile, tsModule);
|
|
105
|
+
discoveredLocations.push({
|
|
106
|
+
name: exp.name,
|
|
107
|
+
filePath: sourceFile.fileName,
|
|
108
|
+
exportName: exp.name,
|
|
109
|
+
sourceMap,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (discoveredLocations.length === 0) {
|
|
115
|
+
return { models: [], warnings: nonFatal };
|
|
116
|
+
}
|
|
117
|
+
// Step 3: Runtime evaluation — import each file and evaluate schemas
|
|
118
|
+
const models = [];
|
|
119
|
+
for (const loc of discoveredLocations) {
|
|
120
|
+
try {
|
|
121
|
+
const schemaJson = loc.syntheticSource
|
|
122
|
+
? await evaluateSyntheticSchema(loc.syntheticSource, loc.exportName, loc.filePath)
|
|
123
|
+
: await evaluateSchema(loc.filePath, loc.exportName);
|
|
124
|
+
models.push({
|
|
125
|
+
name: loc.name,
|
|
126
|
+
module_path: loc.filePath,
|
|
127
|
+
schema: schemaJson,
|
|
128
|
+
source_map: loc.sourceMap,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
catch (err) {
|
|
132
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
133
|
+
nonFatal.push({
|
|
134
|
+
model: loc.name,
|
|
135
|
+
message: `Failed to evaluate schema '${loc.name}' in ${loc.filePath}: ${message}`,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const response = { models, warnings: nonFatal };
|
|
140
|
+
if (providerHint) {
|
|
141
|
+
response.provider_hint = providerHint;
|
|
142
|
+
}
|
|
143
|
+
return response;
|
|
144
|
+
}
|
|
145
|
+
//# sourceMappingURL=discover.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"discover.js","sourceRoot":"","sources":["../src/discover.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,cAAc,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AAExE;;;;;;;;;;GAUG;AACH,MAAM,UAAU,WAAW,CAAC,CAAS,EAAE,MAAc,IAAI,CAAC,GAAG;IAC3D,OAAO,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAClD,CAAC;AACD,OAAO,EACL,+BAA+B,EAC/B,uBAAuB,EACvB,mBAAmB,GACpB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,iBAAiB,EAAqB,MAAM,cAAc,CAAC;AAyBpE;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,UAAkB;IAElB,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,YAAY,CAAC,CAAC;IAC5C,MAAM,EAAE,GAAG,MAAM,MAAM,CAAC,WAAW,CAAC,CAAC;IAErC,wBAAwB;IACxB,MAAM,UAAU,GAAG,QAAQ,CAAC,cAAc,CACxC,OAAO,CAAC,GAAG,EAAE,EACb,QAAQ,CAAC,GAAG,CAAC,UAAU,EACvB,eAAe,CAChB,CAAC;IACF,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CACb,iDAAiD;YAC/C,+CAA+C,CAClD,CAAC;IACJ,CAAC;IAED,MAAM,UAAU,GAAG,QAAQ,CAAC,cAAc,CAAC,UAAU,EAAE,QA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,SAAS;aAC1B,CAAC,CAAC;QACL,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,OAAO,GAAG,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YACjE,QAAQ,CAAC,IAAI,CAAC;gBACZ,KAAK,EAAE,GAAG,CAAC,IAAI;gBACf,OAAO,EAAE,8BAA8B,GAAG,CAAC,IAAI,QAAQ,GAAG,CAAC,QAAQ,KAAK,OAAO,EAAE;aAClF,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAED,MAAM,QAAQ,GAAqB,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC;IAClE,IAAI,YAAY,EAAE,CAAC;QACjB,QAAQ,CAAC,aAAa,GAAG,YAAY,CAAC;IACxC,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type * as ts from 'typescript';
|
|
2
|
+
import type { SourceMapEntry } from './discover.js';
|
|
3
|
+
export interface ExportedSchemaCall {
|
|
4
|
+
name: string;
|
|
5
|
+
objectArg: ts.ObjectLiteralExpression;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Find top-level `const` declarations that are `z.object({...})` calls.
|
|
9
|
+
*/
|
|
10
|
+
export declare function findExportedSchemaCalls(sourceFile: ts.SourceFile, tsModule: typeof ts): ExportedSchemaCall[];
|
|
11
|
+
/**
|
|
12
|
+
* Scan a source file's import declarations for provider SDKs.
|
|
13
|
+
* Returns "openai" or "anthropic" if detected, undefined otherwise.
|
|
14
|
+
*/
|
|
15
|
+
export declare function scanProviderImports(sourceFile: ts.SourceFile, tsModule: typeof ts): string | undefined;
|
|
16
|
+
export declare function hasExportModifier(node: ts.Node, tsModule: typeof ts): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Given a node, if it is `z.object({...})`, return the ObjectLiteralExpression argument.
|
|
19
|
+
* Handles chaining: `z.object({...}).extend({...})` — returns the initial object.
|
|
20
|
+
*/
|
|
21
|
+
export declare function findZObjectCall(node: ts.Node, tsModule: typeof ts): ts.ObjectLiteralExpression | null;
|
|
22
|
+
/**
|
|
23
|
+
* Walk an ObjectLiteralExpression (`{ email: z.string(), ... }`) and build a
|
|
24
|
+
* source map mapping JSON Pointer paths to file:line locations.
|
|
25
|
+
*
|
|
26
|
+
* Handles nested `z.object({...})` by recursing into inner object literals.
|
|
27
|
+
*/
|
|
28
|
+
export declare function buildSourceMapFromObjectLiteral(objLit: ts.ObjectLiteralExpression, sourceFile: ts.SourceFile, tsModule: typeof ts): Record<string, SourceMapEntry>;
|
|
29
|
+
export declare function buildRootSourceMap(node: ts.Node, sourceFile: ts.SourceFile): Record<string, SourceMapEntry>;
|
|
30
|
+
//# sourceMappingURL=discover_ast.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"discover_ast.d.ts","sourceRoot":"","sources":["../src/discover_ast.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,EAAE,MAAM,YAAY,CAAC;AAEtC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAEpD,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,EAAE,CAAC,uBAAuB,CAAC;CACvC;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CACrC,UAAU,EAAE,EAAE,CAAC,UAAU,EACzB,QAAQ,EAAE,OAAO,EAAE,GAClB,kBAAkB,EAAE,CA+CtB;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CACjC,UAAU,EAAE,EAAE,CAAC,UAAU,EACzB,QAAQ,EAAE,OAAO,EAAE,GAClB,MAAM,GAAG,SAAS,CAoBpB;AAED,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,EAAE,CAAC,IAAI,EACb,QAAQ,EAAE,OAAO,EAAE,GAClB,OAAO,CAQT;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAC7B,IAAI,EAAE,EAAE,CAAC,IAAI,EACb,QAAQ,EAAE,OAAO,EAAE,GAClB,EAAE,CAAC,uBAAuB,GAAG,IAAI,CAiDnC;AAmBD;;;;;GAKG;AACH,wBAAgB,+BAA+B,CAC7C,MAAM,EAAE,EAAE,CAAC,uBAAuB,EAClC,UAAU,EAAE,EAAE,CAAC,UAAU,EACzB,QAAQ,EAAE,OAAO,EAAE,GAClB,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CA6DhC;AAED,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,EAAE,CAAC,IAAI,EACb,UAAU,EAAE,EAAE,CAAC,UAAU,GACxB,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAUhC"}
|