|
1 | 1 | import { describe, expect, expectTypeOf, it } from "vitest"; |
2 | | -import { FnError, UnexpectedError, ValidationError, v } from "./index"; |
| 2 | +import { |
| 3 | + FnError, |
| 4 | + memoryAdapter, |
| 5 | + UnexpectedError, |
| 6 | + ValidationError, |
| 7 | + v, |
| 8 | +} from "./index"; |
3 | 9 |
|
4 | 10 | const session = v.var("fnt_session", { |
5 | 11 | default: null as { userId: string } | null, |
@@ -655,6 +661,75 @@ describe("fn schema var widening", () => { |
655 | 661 | .toEqualTypeOf<{ id: string }>(); |
656 | 662 | })(); |
657 | 663 | }); |
| 664 | + |
| 665 | + it("a nested namespace used fn widens the same way as a top-level one", () => { |
| 666 | + const nestedUser = v.var("fnt_nested_user", { |
| 667 | + default: null, |
| 668 | + schema: v.object({ id: v.string() }), |
| 669 | + }); |
| 670 | + const withEmail = v.extend(nestedUser, { email: v.string() }); |
| 671 | + const createUser = v.fn( |
| 672 | + "fnt.ns.create", |
| 673 | + { input: nestedUser }, |
| 674 | + (c) => c.input, |
| 675 | + ); |
| 676 | + v.fn({ use: [{ db: { createUser } }, { withEmail }] as const }, (c) => { |
| 677 | + expectTypeOf(c.db.createUser).parameter(0).toEqualTypeOf<{ |
| 678 | + id: string; |
| 679 | + email: string; |
| 680 | + }>(); |
| 681 | + }); |
| 682 | + }); |
| 683 | + |
| 684 | + it("a merge-var helper widens against mounted v.extend", () => { |
| 685 | + const mergeUser = v.var("fnt_merge_user", { |
| 686 | + default: null, |
| 687 | + schema: v.object({ id: v.string() }), |
| 688 | + }); |
| 689 | + const mergeAccount = v.var("fnt_merge_account", { |
| 690 | + default: null, |
| 691 | + schema: v.object({ id: v.string(), userId: v.string() }), |
| 692 | + }); |
| 693 | + const userWithEmail = v.extend(mergeUser, { email: v.string() }); |
| 694 | + const accountWithPassword = v.extend(mergeAccount, { |
| 695 | + password: v.string(), |
| 696 | + }); |
| 697 | + const store = v.storage(memoryAdapter(), { |
| 698 | + user: mergeUser, |
| 699 | + account: mergeAccount, |
| 700 | + }); |
| 701 | + const db = v.var("db", { merge: true, default: store }); |
| 702 | + const createUser = v.fn( |
| 703 | + "db.create_user", |
| 704 | + { input: mergeUser, use: [{ db, user: mergeUser }] }, |
| 705 | + async (c) => c.input, |
| 706 | + ); |
| 707 | + const createAccount = v.fn( |
| 708 | + "db.create_account", |
| 709 | + { input: mergeAccount, use: [{ db, account: mergeAccount }] }, |
| 710 | + async (c) => c.input, |
| 711 | + ); |
| 712 | + v.fn( |
| 713 | + { |
| 714 | + use: [ |
| 715 | + { user: mergeUser, db: { createUser } }, |
| 716 | + { account: mergeAccount, db: { createAccount } }, |
| 717 | + { db, userWithEmail, accountWithPassword }, |
| 718 | + ] as const, |
| 719 | + }, |
| 720 | + async (c) => { |
| 721 | + expectTypeOf(c.db.createUser).parameter(0).toEqualTypeOf<{ |
| 722 | + id: string; |
| 723 | + email: string; |
| 724 | + }>(); |
| 725 | + expectTypeOf(c.db.createAccount).parameter(0).toEqualTypeOf<{ |
| 726 | + id: string; |
| 727 | + userId: string; |
| 728 | + password: string; |
| 729 | + }>(); |
| 730 | + }, |
| 731 | + ); |
| 732 | + }); |
658 | 733 | }); |
659 | 734 |
|
660 | 735 | describe("declared errors", () => { |
|
0 commit comments