Skip to content

Commit

Permalink
Merge pull request #1 from fabian-hiller/main
Browse files Browse the repository at this point in the history
Improve Valibot validation implementation
  • Loading branch information
matt45400 authored Jun 12, 2024
2 parents 4c4c0c6 + f1f246b commit b3401b5
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions packages/valibot/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ import type {AdapterResolver} from './resolver';
import type {ValidationAdapter} from '@typeschema/core';

import {memoize} from '@typeschema/core';
import { getDotPath } from 'valibot';

const importValidationModule = memoize(async () => {
const {safeParseAsync} = await import('valibot');
return {safeParseAsync};
const {getDotPath, safeParseAsync} = await import('valibot');
return {getDotPath, safeParseAsync};
});

export const validationAdapter: ValidationAdapter<
AdapterResolver
> = async schema => {
const {safeParseAsync} = await importValidationModule();
const {getDotPath, safeParseAsync} = await importValidationModule();
return async data => {
const result = await safeParseAsync(schema, data);
if (result.success) {
Expand All @@ -23,14 +22,10 @@ export const validationAdapter: ValidationAdapter<
};
}
return {
issues: result.issues.map((issue) => {
const path = getDotPath(issue);

return {
message: issue.message,
path: path?.split('.'),
};
}),
issues: result.issues.map(issue => ({
message: issue.message,
path: getDotPath(issue)?.split('.'),
})),
success: false,
};
};
Expand Down

0 comments on commit b3401b5

Please sign in to comment.