-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
All properties for object unions weren't added to the adapter defaults.
Fixes #519
- Loading branch information
Showing
3 changed files
with
69 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { zod } from '$lib/adapters/zod.js'; | ||
import { superValidate } from '$lib/superValidate.js'; | ||
import { stringify } from 'devalue'; | ||
import { describe, test } from 'vitest'; | ||
import { z } from 'zod'; | ||
|
||
const ZodSchema = z.object({ | ||
addresses: z.object({ | ||
additional: z.discriminatedUnion('type', [ | ||
z.object({ | ||
type: z.literal('poBox'), | ||
name: z.string().min(1, 'min len').max(10, 'max len') | ||
}), | ||
z.object({ | ||
type: z.literal('none') | ||
}) | ||
]) | ||
}) | ||
}); | ||
const FormSchema = zod(ZodSchema); | ||
type FormSchema = (typeof FormSchema)['defaults']; | ||
|
||
async function validate(data: unknown) { | ||
const formInput = new FormData(); | ||
|
||
formInput.set('__superform_json', stringify(data)); | ||
try { | ||
await superValidate(formInput, FormSchema); | ||
} catch (err) { | ||
console.error(err); | ||
// | ||
throw err; | ||
} | ||
} | ||
|
||
describe('Demo', () => { | ||
test('Bad', async () => { | ||
const data = { | ||
addresses: { | ||
additional: { | ||
type: 'poBox', | ||
name: '' | ||
} | ||
} | ||
} satisfies FormSchema; | ||
await validate(data); | ||
}); | ||
|
||
test('Good', async () => { | ||
const data = { | ||
addresses: { | ||
additional: { | ||
type: 'none' | ||
} | ||
} | ||
} satisfies FormSchema; | ||
await validate(data); | ||
}); | ||
}); |