Skip to content

Commit

Permalink
Adapter type inference fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ciscoheat committed Nov 15, 2024
1 parent e04df9b commit f2201d1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
16 changes: 7 additions & 9 deletions src/lib/adapters/arktype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import type { type } from 'arktype';
import {
type ValidationAdapter,
type RequiredDefaultsOptions,
type Infer,
type InferIn,
createAdapter,
type ClientValidationAdapter,
type ValidationResult,
Expand All @@ -21,12 +19,12 @@ const fetchModule = /* @__PURE__ */ memoize(modules);
async function _validate<T extends type.Any>(
schema: T,
data: unknown
): Promise<ValidationResult<Infer<T>>> {
): Promise<ValidationResult<T['infer']>> {
const { type } = await fetchModule();
const result = schema(data);
if (!(result instanceof type.errors)) {
return {
data: result as Infer<T>,
data: result as T['infer'],
success: true
};
}
Expand All @@ -42,22 +40,22 @@ async function _validate<T extends type.Any>(

function _arktype<T extends type.Any>(
schema: T,
options: RequiredDefaultsOptions<Infer<T>>
): ValidationAdapter<Infer<T>, InferIn<T>> {
options: RequiredDefaultsOptions<T['infer']>
): ValidationAdapter<T['infer'], T['inferIn']> {
return createAdapter({
superFormValidationLibrary: 'arktype',
defaults: options.defaults,
jsonSchema: createJsonSchema(options),
validate: async (data) => _validate(schema, data)
validate: async (data) => _validate<T>(schema, data)
});
}

function _arktypeClient<T extends type.Any>(
schema: T
): ClientValidationAdapter<Infer<T>, InferIn<T>> {
): ClientValidationAdapter<T['infer'], T['inferIn']> {
return {
superFormValidationLibrary: 'arktype',
validate: async (data) => _validate(schema, data)
validate: async (data) => _validate<T>(schema, data)
};
}

Expand Down
6 changes: 3 additions & 3 deletions src/lib/adapters/valibot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const valibotToJSONSchema = (options: ToJSONSchemaOptions) => {
return valibotToJSON({ ...defaultOptions, ...options }) as JSONSchema;
};

async function validate<T extends SupportedSchemas>(
async function _validate<T extends SupportedSchemas>(
schema: T,
data: unknown,
config?: Config<GenericIssue<unknown>>
Expand Down Expand Up @@ -72,7 +72,7 @@ function _valibot<T extends SupportedSchemas>(
): ValidationAdapter<Infer<T>, InferIn<T>> {
return createAdapter({
superFormValidationLibrary: 'valibot',
validate: async (data) => validate(schema, data, options?.config),
validate: async (data) => _validate<T>(schema, data, options?.config),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
jsonSchema: options?.jsonSchema ?? valibotToJSONSchema({ schema: schema as any, ...options }),
defaults: 'defaults' in options ? options.defaults : undefined
Expand All @@ -84,7 +84,7 @@ function _valibotClient<T extends SupportedSchemas>(
): ClientValidationAdapter<Infer<T>, InferIn<T>> {
return {
superFormValidationLibrary: 'valibot',
validate: async (data) => validate(schema, data)
validate: async (data) => _validate<T>(schema, data)
};
}

Expand Down

0 comments on commit f2201d1

Please sign in to comment.