Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(exports): export zodOneFieldSchema #20

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zod-to-dynamodb-onetable-schema",
"version": "0.0.6",
"version": "0.0.7",
"description": "Auto-generate `dynamodb-onetable` model schemas using `zod`, with best-in-class autocomplete",
"keywords": [
"dynamo",
Expand Down
4 changes: 2 additions & 2 deletions src/converters/array.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { convertZodSchemaToField } from "../index";
import { zodOneFieldSchema } from "../";
import { Opts, Ref, ZodToOneField } from "../converter-type";
import {
ArrayCardinality,
Expand Down Expand Up @@ -53,7 +53,7 @@ export const convertArraySchema = <
ZodArray<Schema, Cardinality>
>;
}
const items = convertZodSchemaToField(
const items = zodOneFieldSchema(
innnerType,
{ currentPath: [...ref.currentPath, "0"] },
opts,
Expand Down
4 changes: 2 additions & 2 deletions src/converters/default.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { convertZodSchemaToField } from "../";
import { zodOneFieldSchema } from "../";
import { Opts, Ref, ZodToOneField } from "../converter-type";
import { z, ZodDefault, ZodTypeAny } from "zod";

Expand All @@ -12,7 +12,7 @@ export const convertDefaultSchema = <T extends ZodTypeAny>(
opts: Opts,
): ZodToOneField<ZodDefault<T>> => {
const innerZodSchema = zodSchema._def.innerType;
const innerOneField = convertZodSchemaToField(innerZodSchema, ref, opts);
const innerOneField = zodOneFieldSchema(innerZodSchema, ref, opts);
const defaultValue = zodSchema._def.defaultValue();
return { ...innerOneField, default: defaultValue };
};
4 changes: 2 additions & 2 deletions src/converters/nullable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { convertZodSchemaToField } from "../";
import { zodOneFieldSchema } from "../";
import { Opts, Ref, ZodToOneField } from "../converter-type";
import { ZodNullable, ZodTypeAny } from "zod";

Expand All @@ -12,7 +12,7 @@ export const convertNullableSchema = <T extends ZodTypeAny>(
ref: Ref,
opts: Opts,
): ZodToOneField<ZodNullable<T>> => {
const innerField = convertZodSchemaToField(
const innerField = zodOneFieldSchema(
zodSchema._def.innerType,
ref,
opts,
Expand Down
4 changes: 2 additions & 2 deletions src/converters/object.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { convertZodSchemaToField } from "../";
import { zodOneFieldSchema } from "../";
import { Opts, Ref, ZodToOneField } from "../converter-type";
import { ZodObject, ZodRawShape, ZodTypeAny } from "zod";

Expand Down Expand Up @@ -30,7 +30,7 @@ export const convertObjectSchema = <Schema extends ZodRawShape>(
const schema = Object.entries(shape).reduce((acc, [propName, zodSchema]) => {
return {
...acc,
[propName]: convertZodSchemaToField(
[propName]: zodOneFieldSchema(
zodSchema as ZodTypeAny,
{ ...ref, currentPath: [...ref.currentPath, propName] },
opts,
Expand Down
4 changes: 2 additions & 2 deletions src/converters/optional.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { convertZodSchemaToField } from "../";
import { zodOneFieldSchema } from "../";
import { Opts, Ref, ZodToOneField } from "../converter-type";
import { ZodOptional, ZodTypeAny } from "zod";

Expand All @@ -12,7 +12,7 @@ export const convertOptionalSchema = <T extends ZodTypeAny>(
ref: Ref,
opts: Opts,
): ZodToOneField<ZodOptional<T>> => {
const innerField = convertZodSchemaToField(
const innerField = zodOneFieldSchema(
zodSchema._def.innerType,
ref,
opts,
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ const getConverterFunction = <T extends ZodSchema>(
}
};

export const convertZodSchemaToField = <T extends ZodSchema>(
export const zodOneFieldSchema = <T extends ZodSchema>(
zodSchema: T,
ref: Ref,
ref?: Ref,
opts?: Opts,
): ZodToOneField<T> => {
const converterFunction = getConverterFunction(zodSchema);
return converterFunction(zodSchema, ref, opts ?? {});
return converterFunction(zodSchema, ref ?? { currentPath: [] }, opts ?? {});
};

export const zodOneModelSchema = <T extends ZodRawShape>(
Expand All @@ -104,7 +104,7 @@ export const zodOneModelSchema = <T extends ZodRawShape>(
(acc, [propName, zodSchema]) => {
return {
...acc,
[propName]: convertZodSchemaToField(
[propName]: zodOneFieldSchema(
zodSchema,
{ currentPath: [propName] },
opts,
Expand Down
4 changes: 2 additions & 2 deletions test/converters/array.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { z } from "zod";
import { convertZodSchemaToField } from "../../src";
import { zodOneFieldSchema } from "../../src";
import { convertArraySchema } from "../../src/converters/array";
import { Logger } from "winston";
import { mock } from "vitest-mock-extended";
Expand Down Expand Up @@ -64,7 +64,7 @@ describe("convertArraySchema", () => {
expect(onefield).toEqual({
type: Array,
required: true,
items: convertZodSchemaToField(schema, mockRefs, mockOpts),
items: zodOneFieldSchema(schema, mockRefs, mockOpts),
});
},
);
Expand Down
4 changes: 2 additions & 2 deletions test/converters/default.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from "vitest";
import { z, ZodTypeAny } from "zod";
import { convertDefaultSchema } from "../../src/converters/default";
import { convertZodSchemaToField } from "../../src";
import { zodOneFieldSchema } from "../../src";
import { Logger } from "winston";
import { mock } from "vitest-mock-extended";

Expand Down Expand Up @@ -36,7 +36,7 @@ describe("convertDefaultSchema", () => {
// Assert
expect(onefield).toEqual({
default: defaultValue,
...convertZodSchemaToField(schema, mockRefs, mockOpts),
...zodOneFieldSchema(schema, mockRefs, mockOpts),
});
},
);
Expand Down
6 changes: 1 addition & 5 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import { it } from "vitest";
import { convertZodSchemaToField } from "../src/index.ts";
import { z } from "zod";

it("should pass", () => {
convertZodSchemaToField(z.date(), { currentPath: [] }, {});
});
it("should pass", () => { });
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { describe, expect, it } from "vitest";
import { z } from "zod";
import { Logger } from "winston";
import { mock } from "vitest-mock-extended";
import { convertZodSchemaToField } from "../src";
import { zodOneFieldSchema } from "../src";

const mockLogger = mock<Logger>();
const mockOpts = { logger: mockLogger };
const mockRefs = { currentPath: ["hello"] };

describe("convertZodSchemaToField", () => {
describe("zodOneFieldSchema", () => {
enum TestEnum {
VALID = "VALID",
}
Expand All @@ -31,7 +30,7 @@ describe("convertZodSchemaToField", () => {
"should return a schema for valid $_def.typeName type",
(schema) => {
// Assemble
const onefield = convertZodSchemaToField(schema, mockRefs, mockOpts);
const onefield = zodOneFieldSchema(schema, undefined, mockOpts);

// Act
expect(onefield).toBeDefined();
Expand Down Expand Up @@ -71,7 +70,7 @@ describe("convertZodSchemaToField", () => {
(schema) => {
// Act
const managedEffect = () =>
convertZodSchemaToField(schema, mockRefs, mockOpts);
zodOneFieldSchema(schema, undefined, mockOpts);

// Assert
expect(managedEffect).toThrowError(
Expand Down