Skip to content

Commit

Permalink
Fixed unwrapping in Documentation and Integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTail committed May 9, 2024
1 parent cee07ca commit 59a0d3f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/date-in-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ export const dateIn = () => {
.pipe(z.date().refine(isValidDate))
.brand(ezDateInBrand);
};

export type DateInSchema = ReturnType<typeof dateIn>;
2 changes: 2 additions & 0 deletions src/date-out-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export const dateOut = () =>
.refine(isValidDate)
.transform((date) => date.toISOString())
.brand(ezDateOutBrand);

export type DateOutSchema = ReturnType<typeof dateOut>;
35 changes: 19 additions & 16 deletions src/documentation-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ import {
ucFirst,
} from "./common-helpers";
import { InputSource, TagsConfig } from "./config-type";
import { ezDateInBrand } from "./date-in-schema";
import { ezDateOutBrand } from "./date-out-schema";
import { DateInSchema, ezDateInBrand } from "./date-in-schema";
import { DateOutSchema, ezDateOutBrand } from "./date-out-schema";
import { DocumentationError } from "./errors";
import { ezFileBrand } from "./file-schema";
import { FileSchema, ezFileBrand } from "./file-schema";
import { IOSchema } from "./io-schema";
import {
LogicalContainer,
Expand All @@ -72,7 +72,7 @@ import {
walkSchema,
} from "./schema-walker";
import { Security } from "./security";
import { ezUploadBrand } from "./upload-schema";
import { UploadSchema, ezUploadBrand } from "./upload-schema";

/* eslint-disable @typescript-eslint/no-use-before-define */

Expand Down Expand Up @@ -146,7 +146,7 @@ export const depictAny: Depicter<z.ZodAny> = () => ({
format: "any",
});

export const depictUpload: Depicter<z.ZodType> = (ctx) => {
export const depictUpload: Depicter<UploadSchema> = (ctx) => {
assert(
!ctx.isResponse,
new DocumentationError({
Expand All @@ -160,15 +160,18 @@ export const depictUpload: Depicter<z.ZodType> = (ctx) => {
};
};

export const depictFile: Depicter<z.ZodType> = ({ schema }) => ({
type: "string",
format:
schema instanceof z.ZodString
? schema._def.checks.find((check) => check.kind === "base64")
? "byte"
: "file"
: "binary",
});
export const depictFile: Depicter<FileSchema> = ({ schema }) => {
const subject = schema.unwrap();
return {
type: "string",
format:
subject instanceof z.ZodString
? subject._def.checks.find((check) => check.kind === "base64")
? "byte"
: "file"
: "binary",
};
};

export const depictUnion: Depicter<z.ZodUnion<z.ZodUnionOptions>> = ({
schema: { options },
Expand Down Expand Up @@ -317,7 +320,7 @@ export const depictObject: Depicter<z.ZodObject<z.ZodRawShape>> = ({
* */
export const depictNull: Depicter<z.ZodNull> = () => ({ type: "null" });

export const depictDateIn: Depicter<z.ZodType> = (ctx) => {
export const depictDateIn: Depicter<DateInSchema> = (ctx) => {
assert(
!ctx.isResponse,
new DocumentationError({
Expand All @@ -336,7 +339,7 @@ export const depictDateIn: Depicter<z.ZodType> = (ctx) => {
};
};

export const depictDateOut: Depicter<z.ZodType> = (ctx) => {
export const depictDateOut: Depicter<DateOutSchema> = (ctx) => {
assert(
ctx.isResponse,
new DocumentationError({
Expand Down
2 changes: 2 additions & 0 deletions src/file-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ export function file<K extends Variant>(variant: K): ReturnType<Variants[K]>;
export function file<K extends Variant>(variant?: K) {
return variants[variant || "string"]();
}

export type FileSchema = ReturnType<typeof file>;
2 changes: 2 additions & 0 deletions src/upload-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ export const upload = () =>
}),
)
.brand(ezUploadBrand);

export type UploadSchema = ReturnType<typeof upload>;
9 changes: 5 additions & 4 deletions src/zts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { z } from "zod";
import { hasCoercion, tryToTransform } from "./common-helpers";
import { ezDateInBrand } from "./date-in-schema";
import { ezDateOutBrand } from "./date-out-schema";
import { ezFileBrand } from "./file-schema";
import { FileSchema, ezFileBrand } from "./file-schema";
import { RawSchema, ezRawBrand } from "./raw-schema";
import { HandlingRules, walkSchema } from "./schema-walker";
import {
Expand Down Expand Up @@ -216,13 +216,14 @@ const onLazy: Producer<z.ZodLazy<z.ZodTypeAny>> = ({
);
};

const onFile: Producer<z.ZodType> = ({ schema }) => {
const onFile: Producer<FileSchema> = ({ schema }) => {
const subject = schema.unwrap();
const stringType = f.createKeywordTypeNode(ts.SyntaxKind.StringKeyword);
const bufferType = f.createTypeReferenceNode("Buffer");
const unionType = f.createUnionTypeNode([stringType, bufferType]);
return schema instanceof z.ZodString
return subject instanceof z.ZodString
? stringType
: schema instanceof z.ZodUnion
: subject instanceof z.ZodUnion
? unionType
: bufferType;
};
Expand Down

0 comments on commit 59a0d3f

Please sign in to comment.