@@ -4,9 +4,11 @@ import {
4
4
TypeValidationError ,
5
5
} from '@ai-sdk/provider' ;
6
6
import { secureJsonParse } from './secure-json-parse' ;
7
- import { ZodSchema } from 'zod' ;
7
+ import * as z3 from 'zod/v3' ;
8
+ import * as z4 from 'zod/v4/core' ;
8
9
import { safeValidateTypes , validateTypes } from './validate-types' ;
9
10
import { Validator } from './validator' ;
11
+ import { InferSchema } from './schema' ;
10
12
11
13
/**
12
14
* Parses a JSON string into an unknown object.
@@ -26,17 +28,17 @@ export async function parseJSON(options: {
26
28
* @param {Validator<T> } schema - The schema to use for parsing the JSON.
27
29
* @returns {Promise<T> } - The parsed object.
28
30
*/
29
- export async function parseJSON < T > ( options : {
31
+ export async function parseJSON < SCHEMA extends z4 . $ZodType | z3 . Schema | Validator , VALUE = InferSchema < SCHEMA > > ( options : {
30
32
text : string ;
31
- schema : ZodSchema < T > | Validator < T > ;
32
- } ) : Promise < T > ;
33
- export async function parseJSON < T > ( {
33
+ schema : SCHEMA ;
34
+ } ) : Promise < VALUE > ;
35
+ export async function parseJSON < SCHEMA extends z4 . $ZodType | z3 . Schema | Validator , VALUE = InferSchema < SCHEMA > > ( {
34
36
text,
35
37
schema,
36
38
} : {
37
39
text : string ;
38
- schema ?: ZodSchema < T > | Validator < T > ;
39
- } ) : Promise < T > {
40
+ schema ?: SCHEMA ;
41
+ } ) : Promise < VALUE > {
40
42
try {
41
43
const value = secureJsonParse ( text ) ;
42
44
@@ -83,22 +85,22 @@ export async function safeParseJSON(options: {
83
85
* @param {Validator<T> } schema - The schema to use for parsing the JSON.
84
86
* @returns An object with either a `success` flag and the parsed and typed data, or a `success` flag and an error object.
85
87
*/
86
- export async function safeParseJSON < T > ( options : {
88
+ export async function safeParseJSON < SCHEMA extends z4 . $ZodType | z3 . Schema | Validator , VALUE = InferSchema < SCHEMA > > ( options : {
87
89
text : string ;
88
- schema : ZodSchema < T > | Validator < T > ;
89
- } ) : Promise < ParseResult < T > > ;
90
- export async function safeParseJSON < T > ( {
90
+ schema : SCHEMA ;
91
+ } ) : Promise < ParseResult < VALUE > > ;
92
+ export async function safeParseJSON < SCHEMA extends z4 . $ZodType | z3 . Schema | Validator , VALUE = InferSchema < SCHEMA > > ( {
91
93
text,
92
94
schema,
93
95
} : {
94
96
text : string ;
95
- schema ?: ZodSchema < T > | Validator < T > ;
96
- } ) : Promise < ParseResult < T > > {
97
+ schema ?: SCHEMA ;
98
+ } ) : Promise < ParseResult < VALUE > > {
97
99
try {
98
100
const value = secureJsonParse ( text ) ;
99
101
100
102
if ( schema == null ) {
101
- return { success : true , value : value as T , rawValue : value } ;
103
+ return { success : true , value : value as VALUE , rawValue : value } ;
102
104
}
103
105
104
106
return await safeValidateTypes ( { value, schema } ) ;
0 commit comments