Skip to content

Commit

Permalink
fix: allow data uris for base64-format files
Browse files Browse the repository at this point in the history
  • Loading branch information
invakid404 committed Nov 28, 2024
1 parent dead18f commit dead549
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/generator/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
resourceTypeSchemaName,
} from "./resources.js";
import { once } from "../utils/once.js";
import dedent from "dedent";

export const runWithBuffer = async <T,>(cb: () => T) => {
const { allResourceTypes } = getContext()!;
Expand Down Expand Up @@ -89,8 +90,10 @@ export const schemaToZod = (
// library doesn't like that, so we need to delete it
if (schema.type === "string" && schema.enum == null) {
delete schema.enum;
}

return;
if (schema.type === 'string' && 'contentEncoding' in schema && schema.contentEncoding === 'base64') {
return base64FileZodSchema();
}

const resourceTypeOrFalse = extractResourceTypeFromSchema(
Expand Down Expand Up @@ -123,6 +126,33 @@ const resourceTypeToUnion = (resourceType: string) => {
return `z.union([${resourceReferencesSchemaName(resourceType)}, ${resourceTypeSchemaName(resourceType)}])`;
};

const base64FileZodSchema = once(() => {
const { deferWrite } = getContext()!;
const name = `$base64_file_type`;

const schema = dedent`
z.union([
z.string().base64(),
z.string().refine(
(value) => {
const base64Match = value.match(/^data:(.*?);base64,(.+)$/);
if (!base64Match) {
return false;
}
const [, mimeType, data] = base64Match;
if (!mimeType) {
return false;
}
return z.string().base64().safeParse(data).success;
},
),
])
`
deferWrite(`const ${name} = ${schema};`);

return name;
});

const s3ObjectZodSchema = once(() => {
const { deferWrite } = getContext()!;
const name = `$s3_object_type`;
Expand Down

0 comments on commit dead549

Please sign in to comment.