Skip to content

Commit 2750ec9

Browse files
committed
Added config option to Valibot adapter.
1 parent 79f82f9 commit 2750ec9

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Added `config` option to Valibot adapter, for the [SchemaConfig](https://valibot.dev/api/SchemaConfig/) type.
13+
1014
### Fixed
1115

1216
- Constraints weren't added when using default values for an adapter with introspection.

src/lib/adapters/valibot.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ import {
1515
toJSONSchema as valibotToJSON
1616
} from '@gcornut/valibot-json-schema';
1717
import type { JSONSchema } from '../jsonSchema/index.js';
18+
import type { Prettify } from '$lib/utils.js';
1819

1920
type SupportedSchemas = BaseSchema | BaseSchemaAsync;
2021

22+
type SchemaConfig = NonNullable<Parameters<typeof safeParseAsync>[2]>;
23+
2124
const defaultOptions = {
2225
strictObjectTypes: true,
2326
dateStrategy: 'integer' as const,
@@ -32,9 +35,10 @@ export const valibotToJSONSchema = (options: ToJSONSchemaOptions) => {
3235

3336
async function validate<T extends SupportedSchemas>(
3437
schema: T,
35-
data: unknown
38+
data: unknown,
39+
config?: SchemaConfig
3640
): Promise<ValidationResult<Infer<T>>> {
37-
const result = await safeParseAsync(schema, data);
41+
const result = await safeParseAsync(schema, data, config);
3842
if (result.success) {
3943
return {
4044
data: result.output as Infer<T>,
@@ -52,11 +56,16 @@ async function validate<T extends SupportedSchemas>(
5256

5357
function _valibot<T extends SupportedSchemas>(
5458
schema: T,
55-
options: Omit<ToJSONSchemaOptions, 'schema'> | AdapterOptions<T> = {}
59+
options: Prettify<
60+
Omit<ToJSONSchemaOptions, 'schema'> &
61+
AdapterOptions<T> & {
62+
config?: SchemaConfig;
63+
}
64+
> = {}
5665
): ValidationAdapter<Infer<T>, InferIn<T>> {
5766
return createAdapter({
5867
superFormValidationLibrary: 'valibot',
59-
validate: async (data) => validate(schema, data),
68+
validate: async (data) => validate(schema, data, options?.config),
6069
jsonSchema: createJsonSchema(
6170
options,
6271
// eslint-disable-next-line @typescript-eslint/no-explicit-any

src/tests/superValidate.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,14 @@ describe('Valibot', () => {
210210
schemaTest(valibot(schema, { defaults }), undefined);
211211
});
212212

213+
it('should work with the config options', async () => {
214+
const adapter = valibot(schema, { config: { abortEarly: true } });
215+
const form = await superValidate({ email: 'no-email', score: -100 }, adapter);
216+
assert(!form.valid);
217+
expect(Object.keys(form.errors).length).toBe(1);
218+
expect(form.errors.email).toMatch(/^Invalid email/);
219+
});
220+
213221
it('should produce a required enum if no default', () => {
214222
const schema = v.object({
215223
enum: v.picklist(['a', 'b', 'c']),

0 commit comments

Comments
 (0)