Skip to content

Commit

Permalink
Merge pull request #2 from exsjabe/enum
Browse files Browse the repository at this point in the history
Feat: Support for enums
  • Loading branch information
marcesengel authored Aug 23, 2023
2 parents d2255d4 + 8d686e9 commit ec30cac
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions __tests__/testCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions src/defParsers/parseEnumDef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { type ZodEnumDef } from 'zod'

import { type DefParser } from '../DefParser'
import { type MongoSchema } from '../MongoSchema'

const parseEnumDef: DefParser<ZodEnumDef> = (def): MongoSchema => {
return {
bsonType: 'string',
enum: def.values,
}
}

export default parseEnumDef
2 changes: 2 additions & 0 deletions src/parseDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -46,4 +47,5 @@ const parseFnByKind: ParserFnByKind = {
}),
[ZodFirstPartyTypeKind.ZodArray]: parseArrayDef,
[ZodFirstPartyTypeKind.ZodNullable]: parseNullableDef,
[ZodFirstPartyTypeKind.ZodEnum]: parseEnumDef,
}

0 comments on commit ec30cac

Please sign in to comment.