From 1e94bcd37e20d370627f0a5674524eb7c8f74587 Mon Sep 17 00:00:00 2001 From: Exsjabe Date: Mon, 7 Aug 2023 10:00:41 +0200 Subject: [PATCH 1/2] Added enum --- src/defParsers/parseEnumDef.ts | 13 +++++++++++++ src/parseDef.ts | 2 ++ 2 files changed, 15 insertions(+) create mode 100644 src/defParsers/parseEnumDef.ts diff --git a/src/defParsers/parseEnumDef.ts b/src/defParsers/parseEnumDef.ts new file mode 100644 index 0000000..08d9e52 --- /dev/null +++ b/src/defParsers/parseEnumDef.ts @@ -0,0 +1,13 @@ +import { type ZodEnumDef } from 'zod' + +import { type DefParser } from '../DefParser' +import { type MongoSchema } from '../MongoSchema' + +const parseEnumDef: DefParser = (def): MongoSchema => { + return { + bsonType: 'string', + enum: def.values, + } +} + +export default parseEnumDef diff --git a/src/parseDef.ts b/src/parseDef.ts index b8b22b9..9901d5b 100644 --- a/src/parseDef.ts +++ b/src/parseDef.ts @@ -4,6 +4,7 @@ import { DefParser, ZodDef } from './DefParser' import { MongoSchema, MongoSchemaBool, MongoSchemaNull } from './MongoSchema' import parseArrayDef from './defParsers/parseArrayDef' import parseDateDef from './defParsers/parseDateDef' +import parseEnumDef from './defParsers/parseEnumDef' import parseNullableDef from './defParsers/parseNullableDef' import parseNumberDef from './defParsers/parseNumberDef' import parseObjectDef from './defParsers/parseObjectDef' @@ -46,4 +47,5 @@ const parseFnByKind: ParserFnByKind = { }), [ZodFirstPartyTypeKind.ZodArray]: parseArrayDef, [ZodFirstPartyTypeKind.ZodNullable]: parseNullableDef, + [ZodFirstPartyTypeKind.ZodEnum]: parseEnumDef, } From 8d686e9633dc05651f8bacad66331fc2d9ade594 Mon Sep 17 00:00:00 2001 From: Exsjabe Date: Mon, 7 Aug 2023 10:00:53 +0200 Subject: [PATCH 2/2] Added test case for enum --- __tests__/testCases.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/__tests__/testCases.ts b/__tests__/testCases.ts index 801f729..a88ea56 100644 --- a/__tests__/testCases.ts +++ b/__tests__/testCases.ts @@ -108,6 +108,16 @@ const testCases: TestCaseGroup[] = [ ], ], }, + { + name: 'z.enum()', + testCases: [ + [ + 'z.enum(["a", "b"])', + z.enum(['a', 'b']), + { bsonType: 'string', enum: ['a', 'b'] }, + ], + ], + }, ] export default testCases