-
-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support to async functions on server validate (#855)
* serverValidate `validate` to `validateAsync` * Fix `onServerValidate` type * `pnpm run prettier:write` * Add createServerValidate test for zod-form-adapter * test: add tests on yup and valibot --------- Co-authored-by: Leonardo Montini <[email protected]>
- Loading branch information
1 parent
179a323
commit 4e7f01d
Showing
9 changed files
with
526 additions
and
153 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
118 changes: 118 additions & 0 deletions
118
packages/valibot-form-adapter/tests/createServerValidate.spec.ts
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 |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { createServerValidate } from '@tanstack/react-form/nextjs' | ||
import * as v from 'valibot' | ||
import { valibotValidator } from '../src/index' | ||
import { sleep } from './utils' | ||
|
||
describe('valibot createServerValidate api', () => { | ||
it('should run v.string validation', async () => { | ||
const serverValidate = createServerValidate({ | ||
validatorAdapter: valibotValidator(), | ||
onServerValidate: v.object({ | ||
name: v.pipe( | ||
v.string(), | ||
v.minLength(3, 'You must have a length of at least 3'), | ||
), | ||
}), | ||
}) | ||
|
||
const formData1 = new FormData() | ||
formData1.append('name', 'a') | ||
expect( | ||
await serverValidate(formData1).catch((e) => e.formState.errors), | ||
).toEqual(['You must have a length of at least 3']) | ||
|
||
const formData2 = new FormData() | ||
formData2.append('name', 'asdf') | ||
expect(await serverValidate(formData2)).toBeUndefined() | ||
}) | ||
|
||
it('should run fn with valibot validation option enabled', async () => { | ||
const serverValidate = createServerValidate({ | ||
validatorAdapter: valibotValidator(), | ||
onServerValidate: ({ value }: { value: { name: string } }) => | ||
value.name === 'a' ? 'Test' : undefined, | ||
}) | ||
|
||
const formData1 = new FormData() | ||
formData1.append('name', 'a') | ||
expect( | ||
await serverValidate(formData1).catch((e) => e.formState.errors), | ||
).toEqual(['Test']) | ||
|
||
const formData2 = new FormData() | ||
formData2.append('name', 'asdf') | ||
expect(await serverValidate(formData2)).toBeUndefined() | ||
}) | ||
|
||
it('should run v.string async validation', async () => { | ||
const serverValidate = createServerValidate({ | ||
validatorAdapter: valibotValidator(), | ||
onServerValidate: v.pipeAsync( | ||
v.object({ | ||
name: v.string(), | ||
}), | ||
v.checkAsync(async (val) => { | ||
await sleep(1) | ||
return val.name.length > 3 | ||
}, 'Testing 123'), | ||
), | ||
}) | ||
|
||
const formData1 = new FormData() | ||
formData1.append('name', 'a') | ||
expect( | ||
await serverValidate(formData1).catch((e) => e.formState.errors), | ||
).toEqual(['Testing 123']) | ||
|
||
const formData2 = new FormData() | ||
formData2.append('name', 'asdf') | ||
expect(await serverValidate(formData2)).toBeUndefined() | ||
}) | ||
|
||
it('should run async fn with valibot validation option enabled', async () => { | ||
const serverValidate = createServerValidate({ | ||
validatorAdapter: valibotValidator(), | ||
onServerValidate: async ({ value }: { value: { name: string } }) => { | ||
await sleep(1) | ||
return value.name === 'a' ? 'Test' : undefined | ||
}, | ||
}) | ||
|
||
const formData1 = new FormData() | ||
formData1.append('name', 'a') | ||
expect( | ||
await serverValidate(formData1).catch((e) => e.formState.errors), | ||
).toEqual(['Test']) | ||
|
||
const formData2 = new FormData() | ||
formData2.append('name', 'asdf') | ||
expect(await serverValidate(formData2)).toBeUndefined() | ||
}) | ||
|
||
it('should transform errors to display all error message', async () => { | ||
const serverValidate = createServerValidate({ | ||
validatorAdapter: valibotValidator(), | ||
onServerValidate: v.object({ | ||
name: v.pipe( | ||
v.string(), | ||
v.minLength(3, 'You must have a length of at least 3'), | ||
v.uuid('UUID'), | ||
), | ||
}), | ||
}) | ||
|
||
const formData1 = new FormData() | ||
formData1.append('name', 'aa') | ||
expect( | ||
await serverValidate(formData1).catch((e) => e.formState.errors), | ||
).toEqual(['You must have a length of at least 3, UUID']) | ||
|
||
const formData2 = new FormData() | ||
formData2.append('name', 'aaa') | ||
expect( | ||
await serverValidate(formData2).catch((e) => e.formState.errors), | ||
).toEqual(['UUID']) | ||
}) | ||
}) |
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
111 changes: 111 additions & 0 deletions
111
packages/yup-form-adapter/tests/createServerValidate.spec.ts
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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { createServerValidate } from '@tanstack/react-form/nextjs' | ||
import yup from 'yup' | ||
import { yupValidator } from '../src/index' | ||
import { sleep } from './utils' | ||
|
||
describe('yup createServerValidate api', () => { | ||
it('should run yup.string validation', async () => { | ||
const serverValidate = createServerValidate({ | ||
validatorAdapter: yupValidator(), | ||
onServerValidate: yup.object({ | ||
name: yup.string().min(3, 'You must have a length of at least 3'), | ||
}), | ||
}) | ||
|
||
const formData1 = new FormData() | ||
formData1.append('name', 'a') | ||
expect( | ||
await serverValidate(formData1).catch((e) => e.formState.errors), | ||
).toEqual(['You must have a length of at least 3']) | ||
|
||
const formData2 = new FormData() | ||
formData2.append('name', 'asdf') | ||
expect(await serverValidate(formData2)).toBeUndefined() | ||
}) | ||
|
||
it('should run fn with yup validation option enabled', async () => { | ||
const serverValidate = createServerValidate({ | ||
validatorAdapter: yupValidator(), | ||
onServerValidate: ({ value }: { value: { name: string } }) => | ||
value.name === 'a' ? 'Test' : undefined, | ||
}) | ||
|
||
const formData1 = new FormData() | ||
formData1.append('name', 'a') | ||
expect( | ||
await serverValidate(formData1).catch((e) => e.formState.errors), | ||
).toEqual(['Test']) | ||
|
||
const formData2 = new FormData() | ||
formData2.append('name', 'asdf') | ||
expect(await serverValidate(formData2)).toBeUndefined() | ||
}) | ||
|
||
it('should run yup.string async validation', async () => { | ||
const serverValidate = createServerValidate({ | ||
validatorAdapter: yupValidator(), | ||
onServerValidate: yup.object({ | ||
name: yup.string().test('Testing 123', 'Testing 123', async (val) => { | ||
await sleep(1) | ||
return typeof val === 'string' ? val.length > 3 : false | ||
}), | ||
}), | ||
}) | ||
|
||
const formData1 = new FormData() | ||
formData1.append('name', 'a') | ||
expect( | ||
await serverValidate(formData1).catch((e) => e.formState.errors), | ||
).toEqual(['Testing 123']) | ||
|
||
const formData2 = new FormData() | ||
formData2.append('name', 'asdf') | ||
expect(await serverValidate(formData2)).toBeUndefined() | ||
}) | ||
|
||
it('should run async fn with yup validation option enabled', async () => { | ||
const serverValidate = createServerValidate({ | ||
validatorAdapter: yupValidator(), | ||
onServerValidate: async ({ value }: { value: { name: string } }) => { | ||
await sleep(1) | ||
return value.name === 'a' ? 'Test' : undefined | ||
}, | ||
}) | ||
|
||
const formData1 = new FormData() | ||
formData1.append('name', 'a') | ||
expect( | ||
await serverValidate(formData1).catch((e) => e.formState.errors), | ||
).toEqual(['Test']) | ||
|
||
const formData2 = new FormData() | ||
formData2.append('name', 'asdf') | ||
expect(await serverValidate(formData2)).toBeUndefined() | ||
}) | ||
|
||
it('should transform errors to display all error message', async () => { | ||
const serverValidate = createServerValidate({ | ||
validatorAdapter: yupValidator(), | ||
onServerValidate: yup.object({ | ||
name: yup | ||
.string() | ||
.min(3, 'You must have a length of at least 3') | ||
.uuid('UUID'), | ||
}), | ||
}) | ||
|
||
const formData1 = new FormData() | ||
formData1.append('name', 'aa') | ||
expect( | ||
await serverValidate(formData1).catch((e) => e.formState.errors), | ||
).toEqual(['You must have a length of at least 3']) | ||
|
||
const formData2 = new FormData() | ||
formData2.append('name', 'aaa') | ||
expect( | ||
await serverValidate(formData2).catch((e) => e.formState.errors), | ||
).toEqual(['UUID']) | ||
}) | ||
}) |
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
Oops, something went wrong.