Skip to content

Commit

Permalink
Moving proprietary schemas into ez namespace. (#850)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTail authored Mar 8, 2023
1 parent e84b8e4 commit 172690b
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 60 deletions.
6 changes: 3 additions & 3 deletions example/endpoints/update-user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createHttpError, withMeta, z } from "../../src";
import { createHttpError, ez, withMeta, z } from "../../src";
import { keyAndTokenAuthenticatedEndpointsFactory } from "../factories";

export const updateUserEndpoint =
Expand All @@ -17,7 +17,7 @@ export const updateUserEndpoint =
"should be greater than or equal to 0"
),
name: z.string().min(1),
birthday: z.dateIn(),
birthday: ez.dateIn(),
})
).example({
id: "12",
Expand All @@ -27,7 +27,7 @@ export const updateUserEndpoint =
output: withMeta(
z.object({
name: z.string(),
createdAt: z.dateOut(),
createdAt: ez.dateOut(),
})
).example({
name: "John Doe",
Expand Down
4 changes: 2 additions & 2 deletions example/endpoints/upload-avatar.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { z } from "../../src";
import { ez, z } from "../../src";
import crypto from "crypto";
import { taggedEndpointsFactory } from "../factories";

Expand All @@ -8,7 +8,7 @@ export const uploadAvatarEndpoint = taggedEndpointsFactory.build({
description: "Handles a file upload.",
input: z
.object({
avatar: z
avatar: ez
.upload()
.refine(
(file) => file.mimetype.match(/image\/.+/),
Expand Down
3 changes: 2 additions & 1 deletion example/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
EndpointsFactory,
createResultHandler,
defaultResultHandler,
ez,
z,
} from "../src";
import { config } from "./config";
Expand Down Expand Up @@ -46,7 +47,7 @@ export const fileStreamingEndpointsFactory = new EndpointsFactory({
config,
resultHandler: createResultHandler({
getPositiveResponse: () => ({
schema: z.file().binary(),
schema: ez.file().binary(),
mimeType: "image/*",
}),
getNegativeResponse: () => ({
Expand Down
17 changes: 0 additions & 17 deletions src/extend-zod.ts

This file was deleted.

11 changes: 11 additions & 0 deletions src/ez-namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ZodDateIn } from "./date-in-schema";
import { ZodDateOut } from "./date-out-schema";
import { ZodFile } from "./file-schema";
import { ZodUpload } from "./upload-schema";

export namespace ez {
export const file = ZodFile.create;
export const upload = ZodUpload.create;
export const dateIn = ZodDateIn.create;
export const dateOut = ZodDateOut.create;
}
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export { withMeta } from "./metadata";
export { testEndpoint } from "./mock";
export { Client } from "./client";

import * as z from "./extend-zod";
import createHttpError from "http-errors";
export { z } from "zod";
export { ez } from "./ez-namespace";

export { createHttpError, z };
import createHttpError from "http-errors";
export { createHttpError };
52 changes: 29 additions & 23 deletions tests/unit/common-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ import {
isValidDate,
makeErrorFromAnything,
} from "../../src/common-helpers";
import { InputValidationError, createHttpError, withMeta, z } from "../../src";
import {
InputValidationError,
createHttpError,
ez,
withMeta,
z,
} from "../../src";
import { Request } from "express";

describe("Common Helpers", () => {
Expand Down Expand Up @@ -441,29 +447,29 @@ describe("Common Helpers", () => {

describe("hasUpload()", () => {
test("should return true for z.upload()", () => {
expect(hasUpload(z.upload())).toBeTruthy();
expect(hasUpload(ez.upload())).toBeTruthy();
});
test("should return true for wrapped z.upload()", () => {
expect(hasUpload(z.object({ test: z.upload() }))).toBeTruthy();
expect(hasUpload(z.upload().or(z.boolean()))).toBeTruthy();
expect(
hasUpload(
z.object({ test: z.boolean() }).and(z.object({ test2: z.upload() }))
)
).toBeTruthy();
expect(hasUpload(z.optional(z.upload()))).toBeTruthy();
expect(hasUpload(z.upload().nullable())).toBeTruthy();
expect(hasUpload(z.upload().default({} as UploadedFile))).toBeTruthy();
expect(hasUpload(z.record(z.upload()))).toBeTruthy();
expect(hasUpload(z.upload().refine(() => true))).toBeTruthy();
expect(hasUpload(z.array(z.upload()))).toBeTruthy();
});
test("should return false in other cases", () => {
expect(hasUpload(z.object({}))).toBeFalsy();
expect(hasUpload(z.any())).toBeFalsy();
expect(hasUpload(z.literal("test"))).toBeFalsy();
expect(hasUpload(z.boolean().and(z.literal(true)))).toBeFalsy();
expect(hasUpload(z.number().or(z.string()))).toBeFalsy();
test.each([
z.object({ test: ez.upload() }),
ez.upload().or(z.boolean()),
z.object({ test: z.boolean() }).and(z.object({ test2: ez.upload() })),
z.optional(ez.upload()),
ez.upload().nullable(),
ez.upload().default({} as UploadedFile),
z.record(ez.upload()),
ez.upload().refine(() => true),
z.array(ez.upload()),
])("should return true for wrapped z.upload() %#", (subject) => {
expect(hasUpload(subject)).toBeTruthy();
});
test.each([
z.object({}),
z.any(),
z.literal("test"),
z.boolean().and(z.literal(true)),
z.number().or(z.string()),
])("should return false in other cases %#", (subject) => {
expect(hasUpload(subject)).toBeFalsy();
});
});

Expand Down
3 changes: 2 additions & 1 deletion tests/unit/endpoint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
createResultHandler,
defaultEndpointsFactory,
defaultResultHandler,
ez,
testEndpoint,
z,
} from "../../src";
Expand Down Expand Up @@ -755,7 +756,7 @@ describe("Endpoint", () => {
test("should avoid double parsing, should not mutate input", async () => {
const dateInputMiddleware = createMiddleware({
input: z.object({
middleware_date_input: z.dateIn().optional(),
middleware_date_input: ez.dateIn().optional(),
}),
middleware: async ({ input: { middleware_date_input }, logger }) => {
logger.debug("date in mw handler", typeof middleware_date_input);
Expand Down
17 changes: 9 additions & 8 deletions tests/unit/open-api-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IOSchemaError } from "../../src/errors";
import {
OpenAPIError,
defaultEndpointsFactory,
ez,
withMeta,
z,
} from "../../src/index";
Expand Down Expand Up @@ -218,7 +219,7 @@ describe("Open API helpers", () => {
test("should set format:binary and type:string", () => {
expect(
depictUpload({
schema: z.upload(),
schema: ez.upload(),
...requestContext,
next: makeNext(requestContext),
})
Expand All @@ -227,7 +228,7 @@ describe("Open API helpers", () => {
test("should throw when using in response", () => {
try {
depictUpload({
schema: z.upload(),
schema: ez.upload(),
...responseContext,
next: makeNext(responseContext),
});
Expand All @@ -240,7 +241,7 @@ describe("Open API helpers", () => {
});

describe("depictFile()", () => {
test.each([z.file(), z.file().binary(), z.file().base64()])(
test.each([ez.file(), ez.file().binary(), ez.file().base64()])(
"should set type:string and format accordingly %#",
(schema) => {
expect(
Expand All @@ -255,7 +256,7 @@ describe("Open API helpers", () => {
test("should throw when using in input", () => {
try {
depictFile({
schema: z.file().binary(),
schema: ez.file().binary(),
...requestContext,
next: makeNext(requestContext),
});
Expand Down Expand Up @@ -768,7 +769,7 @@ describe("Open API helpers", () => {
test("should set type:string, pattern and format", () => {
expect(
depictDateIn({
schema: z.dateIn(),
schema: ez.dateIn(),
...requestContext,
next: makeNext(requestContext),
})
Expand All @@ -777,7 +778,7 @@ describe("Open API helpers", () => {
test("should throw when ZodDateIn in response", () => {
try {
depictDateIn({
schema: z.dateIn(),
schema: ez.dateIn(),
...responseContext,
next: makeNext(responseContext),
});
Expand All @@ -793,7 +794,7 @@ describe("Open API helpers", () => {
test("should set type:string, description and format", () => {
expect(
depictDateOut({
schema: z.dateOut(),
schema: ez.dateOut(),
...responseContext,
next: makeNext(responseContext),
})
Expand All @@ -802,7 +803,7 @@ describe("Open API helpers", () => {
test("should throw when ZodDateOut in request", () => {
try {
depictDateOut({
schema: z.dateOut(),
schema: ez.dateOut(),
...requestContext,
next: makeNext(requestContext),
});
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/open-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
createMiddleware,
createResultHandler,
defaultEndpointsFactory,
ez,
withMeta,
z,
} from "../../src";
Expand Down Expand Up @@ -266,11 +267,11 @@ describe("Open API generator", () => {
input: z.object({
bigint: z.bigint(),
boolean: z.boolean(),
dateIn: z.dateIn(),
dateIn: ez.dateIn(),
}),
output: z.object({
null: z.null(),
dateOut: z.dateOut(),
dateOut: ez.dateOut(),
}),
handler: async () => ({
null: null,
Expand Down

0 comments on commit 172690b

Please sign in to comment.