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

Fixes to allow const assertions in nativeEnum #24

Merged
merged 1 commit into from
Nov 13, 2022
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
22 changes: 10 additions & 12 deletions src/parsers/nativeEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@ export type JsonSchema7NativeEnumType = {
export function parseNativeEnumDef(
def: ZodNativeEnumDef
): JsonSchema7NativeEnumType {
const numberValues = Object.values(def.values)
.filter((value) => typeof value === "number")
.map(toString);
const actualValues = Object.values(def.values).filter(
(_, i) => i >= numberValues.length
);
const object = def.values;
const actualKeys = Object.keys(def.values).filter((key: string) => {
return typeof object[object[key]] !== 'number'
});

const actualValues = actualKeys.map((key: string) => object[key]);

const parsedTypes = Array.from(new Set(actualValues.map((values: string | number) => (typeof values))));

return {
type:
numberValues.length === 0
? "string"
: numberValues.length === actualValues.length
? "number"
: ["string", "number"],
type: parsedTypes.length === 1 ? parsedTypes[0] === 'string' ? "string" : "number" : ["string", "number"],
enum: actualValues,
};
}
46 changes: 46 additions & 0 deletions test/parsers/nativeEnum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,50 @@ describe('Native enums', () => {
};
expect(parsedSchema).toStrictEqual(jsonSchema);
});

it('should be possible to convert a native const assertion object', () => {
const MyConstAssertionObject = {
val1: 0,
val2: 1,
val3: 2
} as const

const parsedSchema = parseNativeEnumDef(z.nativeEnum(MyConstAssertionObject)._def);
const jsonSchema: JSONSchema7Type = {
type: 'number',
enum: [0, 1, 2],
};
expect(parsedSchema).toStrictEqual(jsonSchema);
})

it('should be possible to convert a native const assertion string object', () => {
const MyConstAssertionObject = {
val1: 'a',
val2: 'b',
val3: 'c'
} as const

const parsedSchema = parseNativeEnumDef(z.nativeEnum(MyConstAssertionObject)._def);
const jsonSchema: JSONSchema7Type = {
type: 'string',
enum: ['a', 'b', 'c'],
};
expect(parsedSchema).toStrictEqual(jsonSchema);
})


it('should be possible to convert a mixed value native const assertion string object', () => {
const MyConstAssertionObject = {
val1: 'a',
val2: 1,
val3: 'c'
} as const

const parsedSchema = parseNativeEnumDef(z.nativeEnum(MyConstAssertionObject)._def);
const jsonSchema: JSONSchema7Type = {
type: ['string', 'number'],
enum: ['a', 1, 'c'],
};
expect(parsedSchema).toStrictEqual(jsonSchema);
})
});