Skip to content

Commit

Permalink
feature: add extracgin options from z.array(z.enum([]) and z.enum([])…
Browse files Browse the repository at this point in the history
….array() via useEnumValues hook
  • Loading branch information
golebiowskib committed May 16, 2023
1 parent fe3102b commit b9e34ba
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dev": "jest --watchAll",
"test:cov": "jest --coverage",
"test": "jest",
"watch": "jest --watch",
"build": "npx rollup -c rollup.config.js",
"build-test:dryrun": "act -j build-test -P ubuntu-latest=ghcr.io/catthehacker/ubuntu:js-latest",
"pretty": "prettier --write \"./**/*.{js,jsx,ts,tsx,json}\""
Expand Down
21 changes: 21 additions & 0 deletions src/__tests__/getMetaInformationForZodType.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { z } from "zod";
import {
getEnumValues,
getMetaInformationForZodType,
SPLIT_DESCRIPTION_SYMBOL,
} from "../getMetaInformationForZodType";
Expand Down Expand Up @@ -58,4 +59,24 @@ describe("getMetaInformationForZodType", () => {
placeholder: undefined,
});
});

it("should get the enum values if the schema is an enum", () => {
const result = getEnumValues(z.enum(["a", "b", "c"]));
expect(result).toStrictEqual(["a", "b", "c"]);
})

it("should get the enum values if the schema is an array of enums", () => {
const result = getEnumValues(z.array(z.enum(["a", "b", "c"])));
expect(result).toStrictEqual(["a", "b", "c"]);
})

it("should get the enum values if the schema is an array of enums 2", () => {
const result = getEnumValues(z.enum(["a", "b", "c"]).array());
expect(result).toStrictEqual(["a", "b", "c"]);
})

it("should return undefined if the schema is not an enum", () => {
const result = getEnumValues(z.string());
expect(result).toBeUndefined();
})
});
3 changes: 2 additions & 1 deletion src/getMetaInformationForZodType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export function parseDescription(description?: string) {
};
}

export function getEnumValues(type: RTFSupportedZodTypes) {
export function getEnumValues(type: RTFSupportedZodTypes): readonly string[] | undefined {
if (type._def.typeName === z.ZodFirstPartyTypeKind.ZodArray) return getEnumValues(type._def.type)
if (!(type._def.typeName === z.ZodFirstPartyTypeKind.ZodEnum)) return;
return type._def.values as readonly string[];
}
Expand Down

0 comments on commit b9e34ba

Please sign in to comment.