-
-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: add ability to transform errors from validators. To migrate, c…
…all your validators `validator: yupValidator()`, not `validator: yupValidator` (#755) * feat: add errorMap params to the valibot validator * fix: update all valibotValidator calls * refactor: rename errorMap to transformErrors * fix: transformErrors should return a string * feat: add transformErrors to the yup validator * test: add yup validator transformErrors base test * chore: remove unused import from yup validator * feat: add transformErrors params to the zod validator * Apply suggestions from code review Co-authored-by: Leonardo Montini <[email protected]> * chore: missing docs references * chore: fix prettier * chore: fix import * chore: fix valibot typings and such --------- Co-authored-by: Corbin Crutchley <[email protected]> Co-authored-by: Leonardo Montini <[email protected]> Co-authored-by: Corbin Crutchley <[email protected]>
- Loading branch information
1 parent
601aa22
commit b67bd8d
Showing
31 changed files
with
272 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,34 @@ | ||
import { safeParse, safeParseAsync } from 'valibot' | ||
import type { BaseIssue, BaseSchema, BaseSchemaAsync } from 'valibot' | ||
import type { Validator } from '@tanstack/form-core' | ||
import type { ValidationError, Validator } from '@tanstack/form-core' | ||
|
||
export const valibotValidator = (() => { | ||
return { | ||
validate({ value }, fn) { | ||
if (fn.async) return | ||
const result = safeParse(fn, value) | ||
if (result.success) return | ||
return result.issues.map((i) => i.message).join(', ') | ||
}, | ||
async validateAsync({ value }, fn) { | ||
const result = await safeParseAsync(fn, value) | ||
if (result.success) return | ||
return result.issues.map((i) => i.message).join(', ') | ||
}, | ||
} | ||
}) as Validator< | ||
unknown, | ||
| BaseSchema<unknown, unknown, BaseIssue<unknown>> | ||
| BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>> | ||
> | ||
type Params = { | ||
transformErrors?: (errors: BaseIssue<unknown>[]) => ValidationError | ||
} | ||
|
||
export const valibotValidator = (params: Params = {}) => | ||
(() => { | ||
return { | ||
validate({ value }, fn) { | ||
if (fn.async) return | ||
const result = safeParse(fn, value) | ||
if (result.success) return | ||
if (params.transformErrors) { | ||
return params.transformErrors(result.issues) | ||
} | ||
return result.issues.map((i) => i.message).join(', ') | ||
}, | ||
async validateAsync({ value }, fn) { | ||
const result = await safeParseAsync(fn, value) | ||
if (result.success) return | ||
if (params.transformErrors) { | ||
return params.transformErrors(result.issues) | ||
} | ||
return result.issues.map((i) => i.message).join(', ') | ||
}, | ||
} | ||
}) as Validator< | ||
unknown, | ||
| BaseSchema<unknown, unknown, BaseIssue<unknown>> | ||
| BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>> | ||
> |
Oops, something went wrong.