feat: zod v4 with backwards compatibility for v3.25+ (#1040)

This commit is contained in:
Devin Clark 2025-11-19 12:24:53 -08:00 • committed by GitHub
commit 418b7f9b85
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,5 +1,5 @@
import { z } from 'zod';
import { completable } from './completable.js';
import * as z from 'zod/v4';
import { completable, getCompleter } from './completable.js';
describe('completable', () => {
it('preserves types and values of underlying schema', () => {
@ -14,27 +14,35 @@ describe('completable', () => {
const completions = ['foo', 'bar', 'baz'];
const schema = completable(z.string(), () => completions);
expect(await schema._def.complete('')).toEqual(completions);
const completer = getCompleter(schema);
expect(completer).toBeDefined();
expect(await completer!('')).toEqual(completions);
});
it('allows async completion functions', async () => {
const completions = ['foo', 'bar', 'baz'];
const schema = completable(z.string(), async () => completions);
expect(await schema._def.complete('')).toEqual(completions);
const completer = getCompleter(schema);
expect(completer).toBeDefined();
expect(await completer!('')).toEqual(completions);
});
it('passes current value to completion function', async () => {
const schema = completable(z.string(), value => [value + '!']);
expect(await schema._def.complete('test')).toEqual(['test!']);
const completer = getCompleter(schema);
expect(completer).toBeDefined();
expect(await completer!('test')).toEqual(['test!']);
});
it('works with number schemas', async () => {
const schema = completable(z.number(), () => [1, 2, 3]);
expect(schema.parse(1)).toBe(1);
expect(await schema._def.complete(0)).toEqual([1, 2, 3]);
const completer = getCompleter(schema);
expect(completer).toBeDefined();
expect(await completer!(0)).toEqual([1, 2, 3]);
});
it('preserves schema description', () => {