diff --git a/package.json b/package.json index e92b083..8ee94b0 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/converters/array.ts b/src/converters/array.ts index c317365..87b183c 100644 --- a/src/converters/array.ts +++ b/src/converters/array.ts @@ -1,4 +1,4 @@ -import { convertZodSchemaToField } from "../index"; +import { zodOneFieldSchema } from "../"; import { Opts, Ref, ZodToOneField } from "../converter-type"; import { ArrayCardinality, @@ -53,7 +53,7 @@ export const convertArraySchema = < ZodArray >; } - const items = convertZodSchemaToField( + const items = zodOneFieldSchema( innnerType, { currentPath: [...ref.currentPath, "0"] }, opts, diff --git a/src/converters/default.ts b/src/converters/default.ts index 235010e..533429b 100644 --- a/src/converters/default.ts +++ b/src/converters/default.ts @@ -1,4 +1,4 @@ -import { convertZodSchemaToField } from "../"; +import { zodOneFieldSchema } from "../"; import { Opts, Ref, ZodToOneField } from "../converter-type"; import { z, ZodDefault, ZodTypeAny } from "zod"; @@ -12,7 +12,7 @@ export const convertDefaultSchema = ( opts: Opts, ): ZodToOneField> => { 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 }; }; diff --git a/src/converters/nullable.ts b/src/converters/nullable.ts index dcf144a..257326a 100644 --- a/src/converters/nullable.ts +++ b/src/converters/nullable.ts @@ -1,4 +1,4 @@ -import { convertZodSchemaToField } from "../"; +import { zodOneFieldSchema } from "../"; import { Opts, Ref, ZodToOneField } from "../converter-type"; import { ZodNullable, ZodTypeAny } from "zod"; @@ -12,7 +12,7 @@ export const convertNullableSchema = ( ref: Ref, opts: Opts, ): ZodToOneField> => { - const innerField = convertZodSchemaToField( + const innerField = zodOneFieldSchema( zodSchema._def.innerType, ref, opts, diff --git a/src/converters/object.ts b/src/converters/object.ts index ab2b8c4..a27de73 100644 --- a/src/converters/object.ts +++ b/src/converters/object.ts @@ -1,4 +1,4 @@ -import { convertZodSchemaToField } from "../"; +import { zodOneFieldSchema } from "../"; import { Opts, Ref, ZodToOneField } from "../converter-type"; import { ZodObject, ZodRawShape, ZodTypeAny } from "zod"; @@ -30,7 +30,7 @@ export const convertObjectSchema = ( const schema = Object.entries(shape).reduce((acc, [propName, zodSchema]) => { return { ...acc, - [propName]: convertZodSchemaToField( + [propName]: zodOneFieldSchema( zodSchema as ZodTypeAny, { ...ref, currentPath: [...ref.currentPath, propName] }, opts, diff --git a/src/converters/optional.ts b/src/converters/optional.ts index 7f991e0..91e9d20 100644 --- a/src/converters/optional.ts +++ b/src/converters/optional.ts @@ -1,4 +1,4 @@ -import { convertZodSchemaToField } from "../"; +import { zodOneFieldSchema } from "../"; import { Opts, Ref, ZodToOneField } from "../converter-type"; import { ZodOptional, ZodTypeAny } from "zod"; @@ -12,7 +12,7 @@ export const convertOptionalSchema = ( ref: Ref, opts: Opts, ): ZodToOneField> => { - const innerField = convertZodSchemaToField( + const innerField = zodOneFieldSchema( zodSchema._def.innerType, ref, opts, diff --git a/src/index.ts b/src/index.ts index 51a1c64..7d44d59 100644 --- a/src/index.ts +++ b/src/index.ts @@ -87,13 +87,13 @@ const getConverterFunction = ( } }; -export const convertZodSchemaToField = ( +export const zodOneFieldSchema = ( zodSchema: T, - ref: Ref, + ref?: Ref, opts?: Opts, ): ZodToOneField => { const converterFunction = getConverterFunction(zodSchema); - return converterFunction(zodSchema, ref, opts ?? {}); + return converterFunction(zodSchema, ref ?? { currentPath: [] }, opts ?? {}); }; export const zodOneModelSchema = ( @@ -104,7 +104,7 @@ export const zodOneModelSchema = ( (acc, [propName, zodSchema]) => { return { ...acc, - [propName]: convertZodSchemaToField( + [propName]: zodOneFieldSchema( zodSchema, { currentPath: [propName] }, opts, diff --git a/test/converters/array.spec.ts b/test/converters/array.spec.ts index 0ad4eca..395197b 100644 --- a/test/converters/array.spec.ts +++ b/test/converters/array.spec.ts @@ -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"; @@ -64,7 +64,7 @@ describe("convertArraySchema", () => { expect(onefield).toEqual({ type: Array, required: true, - items: convertZodSchemaToField(schema, mockRefs, mockOpts), + items: zodOneFieldSchema(schema, mockRefs, mockOpts), }); }, ); diff --git a/test/converters/default.spec.ts b/test/converters/default.spec.ts index 434128b..c50f3a4 100644 --- a/test/converters/default.spec.ts +++ b/test/converters/default.spec.ts @@ -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"; @@ -36,7 +36,7 @@ describe("convertDefaultSchema", () => { // Assert expect(onefield).toEqual({ default: defaultValue, - ...convertZodSchemaToField(schema, mockRefs, mockOpts), + ...zodOneFieldSchema(schema, mockRefs, mockOpts), }); }, ); diff --git a/test/index.test.ts b/test/index.test.ts index 5bb0e80..912a06d 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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", () => { }); diff --git a/test/convertZodSchemaToField.spec.ts b/test/zodOneFieldSchema.spec.ts similarity index 86% rename from test/convertZodSchemaToField.spec.ts rename to test/zodOneFieldSchema.spec.ts index 1b21b56..6c1e5d1 100644 --- a/test/convertZodSchemaToField.spec.ts +++ b/test/zodOneFieldSchema.spec.ts @@ -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(); const mockOpts = { logger: mockLogger }; -const mockRefs = { currentPath: ["hello"] }; -describe("convertZodSchemaToField", () => { +describe("zodOneFieldSchema", () => { enum TestEnum { VALID = "VALID", } @@ -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(); @@ -71,7 +70,7 @@ describe("convertZodSchemaToField", () => { (schema) => { // Act const managedEffect = () => - convertZodSchemaToField(schema, mockRefs, mockOpts); + zodOneFieldSchema(schema, undefined, mockOpts); // Assert expect(managedEffect).toThrowError(