Skip to content

Commit

Permalink
feat(ui-utils): add valibotSchema function
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed Sep 15, 2024
1 parent fd03f67 commit eb72ec3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 49 deletions.
2 changes: 2 additions & 0 deletions packages/ui-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@
"dependencies": {
"@ai-sdk/provider": "0.0.23",
"@ai-sdk/provider-utils": "1.0.19",
"@valibot/to-json-schema": "^0.2.0",
"json-schema": "0.4.0",
"secure-json-parse": "2.7.0",
"valibot": "^0.42.0",
"zod-to-json-schema": "3.23.2"
},
"devDependencies": {
Expand Down
15 changes: 15 additions & 0 deletions packages/ui-utils/src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Validator, validatorSymbol } from '@ai-sdk/provider-utils';
import { toJsonSchema as valibotToJsonSchema } from '@valibot/to-json-schema';
import { JSONSchema7 } from 'json-schema';
import * as v from 'valibot';
import { z } from 'zod';
import zodToJsonSchema from 'zod-to-json-schema';

Expand Down Expand Up @@ -83,3 +85,16 @@ export function zodSchema<OBJECT>(
},
);
}

export function valibotSchema<OBJECT>(
valibotSchema: v.GenericSchema<unknown, OBJECT>,
): Schema<OBJECT> {
return jsonSchema(valibotToJsonSchema(valibotSchema), {
validate: value => {
const result = v.safeParse(valibotSchema, value);
return result.success
? { success: true, value: result.output }
: { success: false, error: new v.ValiError(result.issues) };
},
});
}
123 changes: 74 additions & 49 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eb72ec3

Please sign in to comment.