Skip to content

Commit

Permalink
chore: fix lib tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Dec 5, 2023
1 parent 9f4259f commit 75f10c0
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/form-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ export type ValidationError = undefined | false | null | string

// If/when TypeScript supports higher-kinded types, this should not be `unknown` anymore
export type Validator<Type, Fn = unknown> = () => {
validate(value: Type, fn: Fn): ValidationError
validateAsync(value: Type, fn: Fn): Promise<ValidationError>
validate(options: { value: Type }, fn: Fn): ValidationError
validateAsync(options: { value: Type }, fn: Fn): Promise<ValidationError>
}

export type ValidationCause = 'change' | 'blur' | 'submit' | 'mount'
Expand Down
2 changes: 1 addition & 1 deletion packages/form-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export function runValidatorOrAdapter<
validateFn: unknown
// Order matters, first is run first
adapters: Array<Validator<any> | undefined>
value: TData
value: any
methodName: M
}): ReturnType<ReturnType<Validator<TData>>[M]> {
for (const adapter of props.adapters) {
Expand Down
7 changes: 5 additions & 2 deletions packages/valibot-form-adapter/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ export const valibotValidator = (<
Fn extends BaseSchema | BaseSchemaAsync = BaseSchema | BaseSchemaAsync,
>() => {
return {
validate(value: unknown, fn: Fn): ValidationError {
validate({ value }: { value: unknown }, fn: Fn): ValidationError {
if (fn.async) return
const result = safeParse(fn, value)
if (result.success) return
return result.issues.map((i) => i.message).join(', ')
},
async validateAsync(value: unknown, fn: Fn): Promise<ValidationError> {
async validateAsync(
{ value }: { value: unknown },
fn: Fn,
): Promise<ValidationError> {
const result = await safeParseAsync(fn, value)
if (result.success) return
return result.issues.map((i) => i.message).join(', ')
Expand Down
7 changes: 5 additions & 2 deletions packages/yup-form-adapter/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ValidationError, Validator } from '@tanstack/form-core'

export const yupValidator = (<Fn extends AnySchema = AnySchema>() => {
return {
validate(value: unknown, fn: Fn): ValidationError {
validate({ value }: { value: unknown }, fn: Fn): ValidationError {
try {
fn.validateSync(value)
return
Expand All @@ -12,7 +12,10 @@ export const yupValidator = (<Fn extends AnySchema = AnySchema>() => {
return e.errors.join(', ')
}
},
async validateAsync(value: unknown, fn: Fn): Promise<ValidationError> {
async validateAsync(
{ value }: { value: unknown },
fn: Fn,
): Promise<ValidationError> {
try {
await fn.validate(value)
return
Expand Down
7 changes: 5 additions & 2 deletions packages/zod-form-adapter/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ import type { ValidationError, Validator } from '@tanstack/form-core'

export const zodValidator = (<Fn extends ZodType = ZodType>() => {
return {
validate(value: unknown, fn: Fn): ValidationError {
validate({ value }: { value: unknown }, fn: Fn): ValidationError {
// Call Zod on the value here and return the error message
const result = (fn as ZodTypeAny).safeParse(value)
if (!result.success) {
return result.error.issues.map((issue) => issue.message).join(', ')
}
return
},
async validateAsync(value: unknown, fn: Fn): Promise<ValidationError> {
async validateAsync(
{ value }: { value: unknown },
fn: Fn,
): Promise<ValidationError> {
// Call Zod on the value here and return the error message
const result = await (fn as ZodTypeAny).safeParseAsync(value)
if (!result.success) {
Expand Down

0 comments on commit 75f10c0

Please sign in to comment.