🎥 Make videos programmatically with React
implement all zod types
Jonny Burger55085392a630
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/create-zod-values.ts b/packages/cli/src/editor/components/RenderModal/SchemaEditor/create-zod-values.tsindex 6585589a82..e1f1534bab 100644--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/create-zod-values.ts+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/create-zod-values.ts@@ -1,6 +1,10 @@import {z} from 'remotion';export const createZodValues = (schema: z.ZodTypeAny): unknown => {+ if (!schema) {+ throw new Error('Invalid zod schema');+ }+const def = schema._def;const typeName = def.typeName as z.ZodFirstPartyTypeKind;@@ -24,15 +28,15 @@ export const createZodValues = (schema: z.ZodTypeAny): unknown => {case z.ZodFirstPartyTypeKind.ZodNull:return null;case z.ZodFirstPartyTypeKind.ZodAny:- throw new Error('Cannot create a value for any');+ throw new Error('Cannot create a value for type z.any()');case z.ZodFirstPartyTypeKind.ZodUnknown:- throw new Error('Cannot create a value for unknown');+ throw new Error('Cannot create a value for type z.unknown()');case z.ZodFirstPartyTypeKind.ZodNever:- throw new Error('Cannot create a value for never');+ throw new Error('Cannot create a value for type z.never()');case z.ZodFirstPartyTypeKind.ZodVoid:return undefined;case z.ZodFirstPartyTypeKind.ZodObject: {- const shape = def.shape() as Record<string, z.ZodTypeAny>;+ const shape = (def as z.ZodObjectDef).shape();const keys = Object.keys(shape);const returnValue = keys.reduce((existing, key) => {existing[key] = createZodValues(shape[key]);@@ -42,34 +46,135 @@ export const createZodValues = (schema: z.ZodTypeAny): unknown => {}case z.ZodFirstPartyTypeKind.ZodArray: {- return [createZodValues(def.type)];+ return [createZodValues((def as z.ZodArrayDef).type)];}case z.ZodFirstPartyTypeKind.ZodUnion: {- const firstOptions = def.options[0];+ const firstOptions = (def as z.ZodUnionDef).options[0];return firstOptions ? createZodValues(firstOptions) : undefined;}- case z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion:- case z.ZodFirstPartyTypeKind.ZodIntersection:- case z.ZodFirstPartyTypeKind.ZodTuple:- case z.ZodFirstPartyTypeKind.ZodRecord:- case z.ZodFirstPartyTypeKind.ZodMap:- case z.ZodFirstPartyTypeKind.ZodSet:- case z.ZodFirstPartyTypeKind.ZodFunction:- case z.ZodFirstPartyTypeKind.ZodLazy:- case z.ZodFirstPartyTypeKind.ZodLiteral:- case z.ZodFirstPartyTypeKind.ZodEnum:- case z.ZodFirstPartyTypeKind.ZodEffects:- case z.ZodFirstPartyTypeKind.ZodNativeEnum:- case z.ZodFirstPartyTypeKind.ZodOptional:- case z.ZodFirstPartyTypeKind.ZodNullable:- case z.ZodFirstPartyTypeKind.ZodDefault:- case z.ZodFirstPartyTypeKind.ZodCatch:- case z.ZodFirstPartyTypeKind.ZodPromise:- case z.ZodFirstPartyTypeKind.ZodBranded:- case z.ZodFirstPartyTypeKind.ZodPipeline:+ case z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion: {+ const options = (def as z.ZodDiscriminatedUnionDef<string>).options[0];+ return createZodValues(options);+ }++ case z.ZodFirstPartyTypeKind.ZodLiteral: {+ return (def as z.ZodLiteralDef).value;+ }++ case z.ZodFirstPartyTypeKind.ZodEffects: {+ return createZodValues((def as z.ZodEffectsDef).schema);+ }++ case z.ZodFirstPartyTypeKind.ZodIntersection: {+ const {left, right} = def as z.ZodIntersectionDef;+ const leftValue = createZodValues(left);+ if (typeof leftValue !== 'object') {+ throw new Error(+ 'Cannot create value for type z.intersection: Left side is not an object'+ );+ }++ const rightValue = createZodValues(right);++ if (typeof rightValue !== 'object') {+ throw new Error(+ 'Cannot create value for type z.intersection: Right side is not an object'+ );+ }++ return {...leftValue, ...rightValue};+ }++ case z.ZodFirstPartyTypeKind.ZodTuple: {+ const items = (def as z.ZodTupleDef).items.map((item) =>+ createZodValues(item)+ );+ return items;+ }++ case z.ZodFirstPartyTypeKind.ZodRecord: {+ const values = createZodValues((def as z.ZodRecordDef).valueType);+ return {key: values};+ }++ case z.ZodFirstPartyTypeKind.ZodMap: {+ const defType = def as z.ZodMapDef;+ const values = createZodValues(defType.valueType);+ const key = createZodValues(defType.keyType);+ return new Map([[key, values]]);+ }++ case z.ZodFirstPartyTypeKind.ZodLazy: {+ const defType = def as z.ZodLazyDef;+ const type = defType.getter();+ return createZodValues(type);+ }++ case z.ZodFirstPartyTypeKind.ZodSet: {+ const defType = def as z.ZodSetDef;+ const values = createZodValues(defType.valueType);+ return new Set([values]);+ }++ case z.ZodFirstPartyTypeKind.ZodFunction: {+ throw new Error('Cannot create a value for type function');+ }++ case z.ZodFirstPartyTypeKind.ZodEnum: {+ const {values} = def as z.ZodEnumDef;+ return values[0];+ }++ case z.ZodFirstPartyTypeKind.ZodNativeEnum: {+ return 0;+ }++ case z.ZodFirstPartyTypeKind.ZodOptional: {+ const defType = def as z.ZodOptionalDef;+ const value = createZodValues(defType.innerType);+ return value;+ }++ case z.ZodFirstPartyTypeKind.ZodNullable: {+ const defType = def as z.ZodNullableDef;+ const value = createZodValues(defType.innerType);+ return value;+ }++ case z.ZodFirstPartyTypeKind.ZodDefault: {+ const defType = def as z.ZodDefaultDef;+ return defType.defaultValue();+ }++ case z.ZodFirstPartyTypeKind.ZodCatch: {+ const defType = def as z.ZodCatchDef;+ const value = createZodValues(defType.innerType);+ return value;+ }++ case z.ZodFirstPartyTypeKind.ZodPromise: {+ const defType = def as z.ZodPromiseDef;+ const value = createZodValues(defType.type);+ return Promise.resolve(value);+ }++ case z.ZodFirstPartyTypeKind.ZodBranded: {+ // eslint-disable-next-line @typescript-eslint/no-explicit-any+ const defType = def as z.ZodBrandedDef<any>;+ const value = createZodValues(defType.type);+ return value;+ }++ case z.ZodFirstPartyTypeKind.ZodPipeline: {+ // eslint-disable-next-line @typescript-eslint/no-explicit-any+ const defType = def as z.ZodPipelineDef<any, any>;+ const value = createZodValues(defType.out);+ return value;+ }+default:- throw new Error('Not implemented');+ throw new Error('Not implemented: ' + typeName);}};diff --git a/packages/cli/src/test/create-zod-values.test.ts b/packages/cli/src/test/create-zod-values.test.tsindex 3a3690216b..5c746ab666 100644--- a/packages/cli/src/test/create-zod-values.test.ts+++ b/packages/cli/src/test/create-zod-values.test.ts@@ -33,3 +33,156 @@ test('Should be able to create a union', () => {// @ts-expect-error unionexpect(createZodValues(z.union([]))).toBe(undefined);});++test('Zod literal', () => {+ expect(createZodValues(z.literal('hi'))).toBe('hi');+});++test('Should be able to create a discriminated union', () => {+ expect(+ createZodValues(+ z.discriminatedUnion('status', [+ z.object({status: z.literal('success'), data: z.string()}),+ z.object({status: z.literal('failed'), error: z.instanceof(Error)}),+ ])+ )+ ).toEqual({status: 'success', data: ''});++ expect(+ createZodValues(+ z.discriminatedUnion('status', [+ z.object({status: z.literal('failed'), error: z.number()}),+ z.object({status: z.literal('success'), data: z.string()}),+ ])+ )+ ).toEqual({status: 'failed', error: 0});++ // @ts-expect-error invalid zod type+ expect(() => createZodValues(z.discriminatedUnion('status', []))).toThrow(+ /Invalid zod schema/+ );+});++test('Zod instanceof', () => {+ expect(() => createZodValues(z.instanceof(Error))).toThrow(+ /Cannot create a value for type z.any()/+ );+});++test('Zod intersection', () => {+ const Person = z.object({+ name: z.string(),+ });++ const Employee = z.object({+ role: z.string(),+ });++ const EmployedPerson = z.intersection(Person, Employee);+ expect(createZodValues(EmployedPerson)).toEqual({+ name: '',+ role: '',+ });+});++test('Zod tuples', () => {+ const Tuple = z.tuple([z.string(), z.number()]);+ expect(createZodValues(Tuple)).toEqual(['', 0]);+});++test('Zod record', () => {+ const Record = z.record(z.string());+ expect(createZodValues(Record)).toEqual({key: ''});+});++test('Zod map', () => {+ const map = z.map(z.string(), z.number());+ expect(createZodValues(map)).toEqual(new Map([['', 0]]));+});++test('Zod lazy', () => {+ const Lazy = z.lazy(() => z.string());+ expect(createZodValues(Lazy)).toBe('');+});++test('Zod set', () => {+ const set = z.set(z.string());+ expect(createZodValues(set)).toEqual(new Set(['']));+ const set2 = z.set(z.number());+ expect(createZodValues(set2)).toEqual(new Set([0]));+});++test('Zod function', () => {+ const fn = z.function();+ expect(() => createZodValues(fn)).toThrow(+ /Cannot create a value for type function/+ );+});++test('Zod undefined', () => {+ const undef = z.void();+ expect(createZodValues(undef)).toBe(undefined);+});++test('Zod null', () => {+ const undef = z.null();+ expect(createZodValues(undef)).toBe(null);+});++test('Zod enum', () => {+ const Enum = z.enum(['a', 'b']);+ expect(createZodValues(Enum)).toBe('a');+});++test('Zod nativeEnum', () => {+ enum Fruits {+ Apple,+ Banana,+ }+ const Enum = z.nativeEnum(Fruits);+ expect(createZodValues(Enum)).toBe(Fruits.Apple);+});++test('Zod optional', () => {+ const Optional = z.string().optional();+ expect(createZodValues(Optional)).toBe('');+});++test('Zod nullable', () => {+ const Nullable = z.string().nullable();+ expect(createZodValues(Nullable)).toBe('');+});++test('Zod undefined', () => {+ const undef = z.string().default('tuna');+ expect(createZodValues(undef)).toBe('tuna');+});++test('Zod catch', () => {+ const undef = z.string().catch('tuna');+ expect(createZodValues(undef)).toBe('');+});++test('Zod promise', () => {+ const undef = z.string().promise();+ (createZodValues(undef) as Promise<unknown>).then((v) => {+ expect(v).toBe('');+ });+});++test('Zod transform', () => {+ // Intentional: We don't parse the Zod values, so we should not transform them+ const undef = z.literal('abc').transform((v) => v.toUpperCase());+ expect(createZodValues(undef)).toBe('abc');+});++test('Zod branded', () => {+ const Cat = z.object({name: z.string()}).brand<'Cat'>();+ expect(createZodValues(Cat)).toEqual({name: ''});+});++test('Zod lazy', () => {+ const datelike = z.union([z.number(), z.string(), z.date()]);+ const datelikeToDate = datelike.pipe(z.coerce.date());+ expect(createZodValues(datelikeToDate)).toBeInstanceOf(Date);+});