Skip to content

🎥 Make videos programmatically with React

zod types

Jonny Burger7de397742697
diff --git a/packages/cli/src/color-math.ts b/packages/cli/src/color-math.ts
index b4607b4502..9880a6e009 100644
--- a/packages/cli/src/color-math.ts
+++ b/packages/cli/src/color-math.ts
@@ -3,9 +3,9 @@ import type {ZodTypesType} from './editor/components/get-zod-if-possible';
export const colorWithNewOpacity = (
color: string,
opacity: number,
- zColor: ZodTypesType
+ zodTypes: ZodTypesType
) => {
- const {r, g, b} = zColor.ZodZypesInternals.parseColor(color);
+ const {r, g, b} = zodTypes.ZodZypesInternals.parseColor(color);
if (opacity >= 255) {
return `#${r.toString(16)}${g.toString(16)}${b.toString(16)}`;
}
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx
index 4446988e62..efa669fb55 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx
@@ -70,7 +70,7 @@ export const ZodArrayEditor: React.FC<{
throw new Error('expected zod');
}
- const zColor = useZodTypesIfPossible();
+ const zodTypes = useZodTypesIfPossible();
const typeName = def.typeName as z.ZodFirstPartyTypeKind;
if (typeName !== z.ZodFirstPartyTypeKind.ZodArray) {
@@ -121,7 +121,7 @@ export const ZodArrayEditor: React.FC<{
const onAdd = useCallback(() => {
onChange((oldV) => {
- return [...oldV, createZodValues(def.type, z, zColor)];
+ return [...oldV, createZodValues(def.type, z, zodTypes)];
}, true);
}, [def.type, onChange, z]);
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx
index 3c4485089e..4162309207 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx
@@ -60,7 +60,7 @@ export const ZodOrNullishEditor: React.FC<{
throw new Error('expected zod');
}
- const zColor = useZodTypesIfPossible();
+ const zodTypes = useZodTypesIfPossible();
const isChecked = value === nullishValue;
@@ -76,10 +76,10 @@ export const ZodOrNullishEditor: React.FC<{
(e) => {
const val = e.target.checked
? nullishValue
- : createZodValues(schema, z, zColor);
+ : createZodValues(schema, z, zodTypes);
onValueChange(val);
},
- [nullishValue, onValueChange, schema, z, zColor]
+ [nullishValue, onValueChange, schema, z, zodTypes]
);
const reset = useCallback(() => {
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.ts
index ce925d5654..e3780dcc26 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
@@ -4,7 +4,7 @@ import type {ZodType, ZodTypesType} from '../../get-zod-if-possible';
export const createZodValues = (
schema: Zod.ZodTypeAny,
zodRuntime: ZodType,
- zColorRuntime: ZodTypesType | null
+ zodTypes: ZodTypesType | null
): unknown => {
if (!schema) {
throw new Error('Invalid zod schema');
@@ -44,7 +44,7 @@ export const createZodValues = (
const shape = (def as z.ZodObjectDef).shape();
const keys = Object.keys(shape);
const returnValue = keys.reduce((existing, key) => {
- existing[key] = createZodValues(shape[key], zodRuntime, zColorRuntime);
+ existing[key] = createZodValues(shape[key], zodRuntime, zodTypes);
return existing;
}, {} as Record<string, unknown>);
return returnValue;
@@ -52,20 +52,20 @@ export const createZodValues = (
case zodRuntime.ZodFirstPartyTypeKind.ZodArray: {
return [
- createZodValues((def as z.ZodArrayDef).type, zodRuntime, zColorRuntime),
+ createZodValues((def as z.ZodArrayDef).type, zodRuntime, zodTypes),
];
}
case zodRuntime.ZodFirstPartyTypeKind.ZodUnion: {
const firstOptions = (def as z.ZodUnionDef).options[0];
return firstOptions
- ? createZodValues(firstOptions, zodRuntime, zColorRuntime)
+ ? createZodValues(firstOptions, zodRuntime, zodTypes)
: undefined;
}
case zodRuntime.ZodFirstPartyTypeKind.ZodDiscriminatedUnion: {
const options = (def as z.ZodDiscriminatedUnionDef<string>).options[0];
- return createZodValues(options, zodRuntime, zColorRuntime);
+ return createZodValues(options, zodRuntime, zodTypes);
}
case zodRuntime.ZodFirstPartyTypeKind.ZodLiteral: {
@@ -74,9 +74,9 @@ export const createZodValues = (
case zodRuntime.ZodFirstPartyTypeKind.ZodEffects: {
if (
- zColorRuntime &&
+ zodTypes &&
schema._def.description ===
- zColorRuntime.ZodZypesInternals.REMOTION_COLOR_BRAND
+ zodTypes.ZodZypesInternals.REMOTION_COLOR_BRAND
) {
return '#ffffff';
}
@@ -84,20 +84,20 @@ export const createZodValues = (
return createZodValues(
(def as z.ZodEffectsDef).schema,
zodRuntime,
- zColorRuntime
+ zodTypes
);
}
case zodRuntime.ZodFirstPartyTypeKind.ZodIntersection: {
const {left, right} = def as z.ZodIntersectionDef;
- const leftValue = createZodValues(left, zodRuntime, zColorRuntime);
+ const leftValue = createZodValues(left, zodRuntime, zodTypes);
if (typeof leftValue !== 'object') {
throw new Error(
'Cannot create value for type z.intersection: Left side is not an object'
);
}
- const rightValue = createZodValues(right, zodRuntime, zColorRuntime);
+ const rightValue = createZodValues(right, zodRuntime, zodTypes);
if (typeof rightValue !== 'object') {
throw new Error(
@@ -110,7 +110,7 @@ export const createZodValues = (
case zodRuntime.ZodFirstPartyTypeKind.ZodTuple: {
const items = (def as z.ZodTupleDef).items.map((item) =>
- createZodValues(item, zodRuntime, zColorRuntime)
+ createZodValues(item, zodRuntime, zodTypes)
);
return items;
}
@@ -119,35 +119,27 @@ export const createZodValues = (
const values = createZodValues(
(def as z.ZodRecordDef).valueType,
zodRuntime,
- zColorRuntime
+ zodTypes
);
return {key: values};
}
case zodRuntime.ZodFirstPartyTypeKind.ZodMap: {
const defType = def as z.ZodMapDef;
- const values = createZodValues(
- defType.valueType,
- zodRuntime,
- zColorRuntime
- );
- const key = createZodValues(defType.keyType, zodRuntime, zColorRuntime);
+ const values = createZodValues(defType.valueType, zodRuntime, zodTypes);
+ const key = createZodValues(defType.keyType, zodRuntime, zodTypes);
return new Map([[key, values]]);
}
case zodRuntime.ZodFirstPartyTypeKind.ZodLazy: {
const defType = def as z.ZodLazyDef;
const type = defType.getter();
- return createZodValues(type, zodRuntime, zColorRuntime);
+ return createZodValues(type, zodRuntime, zodTypes);
}
case zodRuntime.ZodFirstPartyTypeKind.ZodSet: {
const defType = def as z.ZodSetDef;
- const values = createZodValues(
- defType.valueType,
- zodRuntime,
- zColorRuntime
- );
+ const values = createZodValues(defType.valueType, zodRuntime, zodTypes);
return new Set([values]);
}
@@ -166,21 +158,13 @@ export const createZodValues = (
case zodRuntime.ZodFirstPartyTypeKind.ZodOptional: {
const defType = def as z.ZodOptionalDef;
- const value = createZodValues(
- defType.innerType,
- zodRuntime,
- zColorRuntime
- );
+ const value = createZodValues(defType.innerType, zodRuntime, zodTypes);
return value;
}
case zodRuntime.ZodFirstPartyTypeKind.ZodNullable: {
const defType = def as z.ZodNullableDef;
- const value = createZodValues(
- defType.innerType,
- zodRuntime,
- zColorRuntime
- );
+ const value = createZodValues(defType.innerType, zodRuntime, zodTypes);
return value;
}
@@ -191,31 +175,27 @@ export const createZodValues = (
case zodRuntime.ZodFirstPartyTypeKind.ZodCatch: {
const defType = def as z.ZodCatchDef;
- const value = createZodValues(
- defType.innerType,
- zodRuntime,
- zColorRuntime
- );
+ const value = createZodValues(defType.innerType, zodRuntime, zodTypes);
return value;
}
case zodRuntime.ZodFirstPartyTypeKind.ZodPromise: {
const defType = def as z.ZodPromiseDef;
- const value = createZodValues(defType.type, zodRuntime, zColorRuntime);
+ const value = createZodValues(defType.type, zodRuntime, zodTypes);
return Promise.resolve(value);
}
case zodRuntime.ZodFirstPartyTypeKind.ZodBranded: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const defType = def as z.ZodBrandedDef<any>;
- const value = createZodValues(defType.type, zodRuntime, zColorRuntime);
+ const value = createZodValues(defType.type, zodRuntime, zodTypes);
return value;
}
case zodRuntime.ZodFirstPartyTypeKind.ZodPipeline: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const defType = def as z.ZodPipelineDef<any, any>;
- const value = createZodValues(defType.out, zodRuntime, zColorRuntime);
+ const value = createZodValues(defType.out, zodRuntime, zodTypes);
return value;
}
diff --git a/packages/cli/src/test/color-math.test.ts b/packages/cli/src/test/color-math.test.ts
index e87db9db9c..f44df7c41e 100644
--- a/packages/cli/src/test/color-math.test.ts
+++ b/packages/cli/src/test/color-math.test.ts
@@ -1,7 +1,7 @@
import {expect, test} from 'vitest';
import {getZTypesIfPossible} from '../editor/components/get-zod-if-possible';
-const getZColor = async () => {
+const getZodTypes = async () => {
const z = await getZTypesIfPossible();
if (!z) {
throw new Error('@remotion/zod-types not found');
@@ -11,7 +11,7 @@ const getZColor = async () => {
};
test('Color math', async () => {
- const mod = await getZColor();
+ const mod = await getZodTypes();
expect(mod.ZodZypesInternals.parseColor('rgba(255, 255, 255, 0.5)')).toEqual({
a: 128,
r: 255,
diff --git a/packages/cli/src/test/create-zod-values.test.ts b/packages/cli/src/test/create-zod-values.test.ts
index de75d3c3c7..a9c3cdae2f 100644
--- a/packages/cli/src/test/create-zod-values.test.ts
+++ b/packages/cli/src/test/create-zod-values.test.ts
@@ -14,7 +14,7 @@ const getZ = async () => {
return z;
};
-const getZColor = async () => {
+const getZodTypes = async () => {
const z = await getZTypesIfPossible();
if (!z) {
throw new Error('@remotion/zod-types not found');
@@ -25,23 +25,23 @@ const getZColor = async () => {
test('Should be able to create a string', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
- const hi = createZodValues(z.string(), z, zColor);
+ const hi = createZodValues(z.string(), z, zodTypes);
expect(hi).toBe('');
});
test('Should be able to create a number', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
- const hi = createZodValues(z.number(), z, zColor);
+ const hi = createZodValues(z.number(), z, zodTypes);
expect(hi).toBe(0);
});
test('Should be able to create an object', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
const hi = createZodValues(
z.object({
@@ -49,41 +49,43 @@ test('Should be able to create an object', async () => {
b: z.number(),
}),
z,
- zColor
+ zodTypes
);
expect(hi).toEqual({a: '', b: 0});
});
test('Should be able to create an array', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
- expect(createZodValues(z.array(z.string()), z, zColor)).toEqual(['']);
- expect(createZodValues(z.array(z.number()), z, zColor)).toEqual([0]);
+ expect(createZodValues(z.array(z.string()), z, zodTypes)).toEqual(['']);
+ expect(createZodValues(z.array(z.number()), z, zodTypes)).toEqual([0]);
});
test('Should be able to create a union', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
- expect(createZodValues(z.union([z.string(), z.number()]), z, zColor)).toBe(
+ expect(createZodValues(z.union([z.string(), z.number()]), z, zodTypes)).toBe(
''
);
- expect(createZodValues(z.union([z.number(), z.string()]), z, zColor)).toBe(0);
+ expect(createZodValues(z.union([z.number(), z.string()]), z, zodTypes)).toBe(
+ 0
+ );
// @ts-expect-error union
- expect(createZodValues(z.union([]), z, zColor)).toBe(undefined);
+ expect(createZodValues(z.union([]), z, zodTypes)).toBe(undefined);
});
test('Zod literal', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
- expect(createZodValues(z.literal('hi'), z, zColor)).toBe('hi');
+ expect(createZodValues(z.literal('hi'), z, zodTypes)).toBe('hi');
});
test('Should be able to create a discriminated union', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
expect(
createZodValues(
@@ -92,7 +94,7 @@ test('Should be able to create a discriminated union', async () => {
z.object({status: z.literal('failed'), error: z.instanceof(Error)}),
]),
z,
- zColor
+ zodTypes
)
).toEqual({status: 'success', data: ''});
@@ -103,28 +105,28 @@ test('Should be able to create a discriminated union', async () => {
z.object({status: z.literal('success'), data: z.string()}),
]),
z,
- zColor
+ zodTypes
)
).toEqual({status: 'failed', error: 0});
expect(() =>
// @ts-expect-error invalid zod type
- createZodValues(z.discriminatedUnion('status', []), z, zColor)
+ createZodValues(z.discriminatedUnion('status', []), z, zodTypes)
).toThrow(/Invalid zod schema/);
});
test('Zod instanceof', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
- expect(() => createZodValues(z.instanceof(Error), z, zColor)).toThrow(
+ expect(() => createZodValues(z.instanceof(Error), z, zodTypes)).toThrow(
/Cannot create a value for type z.any()/
);
});
test('Zod intersection', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
const Person = z.object({
name: z.string(),
@@ -135,7 +137,7 @@ test('Zod intersection', async () => {
});
const EmployedPerson = z.intersection(Person, Employee);
- expect(createZodValues(EmployedPerson, z, zColor)).toEqual({
+ expect(createZodValues(EmployedPerson, z, zodTypes)).toEqual({
name: '',
role: '',
});
@@ -143,180 +145,180 @@ test('Zod intersection', async () => {
test('Zod tuples', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
const Tuple = z.tuple([z.string(), z.number()]);
- expect(createZodValues(Tuple, z, zColor)).toEqual(['', 0]);
+ expect(createZodValues(Tuple, z, zodTypes)).toEqual(['', 0]);
});
test('Zod record', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
const Record = z.record(z.string());
- expect(createZodValues(Record, z, zColor)).toEqual({key: ''});
+ expect(createZodValues(Record, z, zodTypes)).toEqual({key: ''});
});
test('Zod map', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
const map = z.map(z.string(), z.number());
- expect(createZodValues(map, z, zColor)).toEqual(new Map([['', 0]]));
+ expect(createZodValues(map, z, zodTypes)).toEqual(new Map([['', 0]]));
});
test('Zod lazy', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
const Lazy = z.lazy(() => z.string());
- expect(createZodValues(Lazy, z, zColor)).toBe('');
+ expect(createZodValues(Lazy, z, zodTypes)).toBe('');
});
test('Zod set', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
const set = z.set(z.string());
- expect(createZodValues(set, z, zColor)).toEqual(new Set(['']));
+ expect(createZodValues(set, z, zodTypes)).toEqual(new Set(['']));
const set2 = z.set(z.number());
- expect(createZodValues(set2, z, zColor)).toEqual(new Set([0]));
+ expect(createZodValues(set2, z, zodTypes)).toEqual(new Set([0]));
});
test('Zod function', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
const fn = z.function();
- expect(() => createZodValues(fn, z, zColor)).toThrow(
+ expect(() => createZodValues(fn, z, zodTypes)).toThrow(
/Cannot create a value for type function/
);
});
test('Zod undefined', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
const undef = z.void();
- expect(createZodValues(undef, z, zColor)).toBe(undefined);
+ expect(createZodValues(undef, z, zodTypes)).toBe(undefined);
});
test('Zod null', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
const undef = z.null();
- expect(createZodValues(undef, z, zColor)).toBe(null);
+ expect(createZodValues(undef, z, zodTypes)).toBe(null);
});
test('Zod enum', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
const Enum = z.enum(['a', 'b']);
- expect(createZodValues(Enum, z, zColor)).toBe('a');
+ expect(createZodValues(Enum, z, zodTypes)).toBe('a');
});
test('Zod nativeEnum', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
enum Fruits {
Apple,
Banana,
}
const Enum = z.nativeEnum(Fruits);
- expect(createZodValues(Enum, z, zColor)).toBe(Fruits.Apple);
+ expect(createZodValues(Enum, z, zodTypes)).toBe(Fruits.Apple);
});
test('Zod optional', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
const Optional = z.string().optional();
- expect(createZodValues(Optional, z, zColor)).toBe('');
+ expect(createZodValues(Optional, z, zodTypes)).toBe('');
});
test('Zod nullable', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
const Nullable = z.string().nullable();
- expect(createZodValues(Nullable, z, zColor)).toBe('');
+ expect(createZodValues(Nullable, z, zodTypes)).toBe('');
});
test('Zod undefined', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
const undef = z.string().default('tuna');
- expect(createZodValues(undef, z, zColor)).toBe('tuna');
+ expect(createZodValues(undef, z, zodTypes)).toBe('tuna');
});
test('Zod catch', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
const undef = z.string().catch('tuna');
- expect(createZodValues(undef, z, zColor)).toBe('');
+ expect(createZodValues(undef, z, zodTypes)).toBe('');
});
test('Zod promise', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
const undef = z.string().promise();
- (createZodValues(undef, z, zColor) as Promise<unknown>).then((v) => {
+ (createZodValues(undef, z, zodTypes) as Promise<unknown>).then((v) => {
expect(v).toBe('');
});
});
test('Zod transform', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
// 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, z, zColor)).toBe('abc');
+ expect(createZodValues(undef, z, zodTypes)).toBe('abc');
});
test('Zod branded', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
const Cat = z.object({name: z.string()}).brand<'Cat'>();
- expect(createZodValues(Cat, z, zColor)).toEqual({name: ''});
+ expect(createZodValues(Cat, z, zodTypes)).toEqual({name: ''});
});
test('Zod lazy', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
const datelike = z.union([z.number(), z.string(), z.date()]);
const datelikeToDate = datelike.pipe(z.coerce.date());
- expect(createZodValues(datelikeToDate, z, zColor)).toBeInstanceOf(Date);
+ expect(createZodValues(datelikeToDate, z, zodTypes)).toBeInstanceOf(Date);
});
test('Zod coerce', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
const datelike = z.union([z.number(), z.string(), z.date()]);
const datelikeToDate = datelike.pipe(z.coerce.date());
- expect(createZodValues(datelikeToDate, z, zColor)).toBeInstanceOf(Date);
+ expect(createZodValues(datelikeToDate, z, zodTypes)).toBeInstanceOf(Date);
});
test('Zod strict', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
const strict = z.strictObject({a: z.string()}).strict();
- expect(createZodValues(strict, z, zColor)).toEqual({a: ''});
+ expect(createZodValues(strict, z, zodTypes)).toEqual({a: ''});
});
test('Should create a color', async () => {
const z = await getZ();
- const zColor = await getZColor();
+ const zodTypes = await getZodTypes();
- expect(createZodValues(zColor.zColor(), z, zColor)).toBe('#ffffff');
+ expect(createZodValues(zodTypes.zColor(), z, zodTypes)).toBe('#ffffff');
});