diff --git a/README.md b/README.md index ab6101e..338af0d 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ - ✅ z.record - ✅ z.set - ✅ z.string[^1] +- ✅ z.symbol - ✅ z.tuple - ✅ z.undefined - ✅ z.union diff --git a/src/zod-symbol-faker.ts b/src/zod-symbol-faker.ts new file mode 100644 index 0000000..422de8b --- /dev/null +++ b/src/zod-symbol-faker.ts @@ -0,0 +1,14 @@ +import * as z from 'zod' +import { ZodTypeFaker } from './zod-type-faker' + +export class ZodSymbolFaker extends ZodTypeFaker { + fake(): z.infer { + return Symbol() + } + + static create(schema: T): ZodSymbolFaker { + return new ZodSymbolFaker(schema) + } +} + +export const zodSymbolFaker: typeof ZodSymbolFaker.create = ZodSymbolFaker.create diff --git a/src/zod-type-kind-to-zod-type-faker.ts b/src/zod-type-kind-to-zod-type-faker.ts index 4a9fc26..b398ec1 100644 --- a/src/zod-type-kind-to-zod-type-faker.ts +++ b/src/zod-type-kind-to-zod-type-faker.ts @@ -29,6 +29,7 @@ import { ZodReadonlyFaker } from './zod-readonly-faker' import { ZodRecordFaker } from './zod-record-faker' import { ZodSetFaker } from './zod-set-faker' import { ZodStringFaker } from './zod-string-faker' +import { ZodSymbolFaker } from './zod-symbol-faker' import { ZodTupleFaker } from './zod-tuple-faker' import { ZodTypeFaker } from './zod-type-faker' import { ZodUndefinedFaker } from './zod-undefined-faker' @@ -69,6 +70,7 @@ export function install(): void { zodTypeKindToZodTypeFaker.set(z.ZodFirstPartyTypeKind.ZodRecord, ZodRecordFaker) zodTypeKindToZodTypeFaker.set(z.ZodFirstPartyTypeKind.ZodSet, ZodSetFaker) zodTypeKindToZodTypeFaker.set(z.ZodFirstPartyTypeKind.ZodString, ZodStringFaker) + zodTypeKindToZodTypeFaker.set(z.ZodFirstPartyTypeKind.ZodSymbol, ZodSymbolFaker) zodTypeKindToZodTypeFaker.set(z.ZodFirstPartyTypeKind.ZodTuple, ZodTupleFaker) zodTypeKindToZodTypeFaker.set(z.ZodFirstPartyTypeKind.ZodUndefined, ZodUndefinedFaker) zodTypeKindToZodTypeFaker.set(z.ZodFirstPartyTypeKind.ZodUnion, ZodUnionFaker) diff --git a/tests/zod-symbol-faker.test.ts b/tests/zod-symbol-faker.test.ts new file mode 100644 index 0000000..39a6a52 --- /dev/null +++ b/tests/zod-symbol-faker.test.ts @@ -0,0 +1,41 @@ +import { expect, test } from 'vitest' +import * as z from 'zod' +import { zodSymbolFaker, ZodSymbolFaker } from '../src/zod-symbol-faker' +import { expectType, TypeEqual } from 'ts-expect' + +test('ZodSymbolFaker should assert parameters', () => { + const invalidSchema = void 0 as any + expect(() => zodSymbolFaker(invalidSchema)).toThrow() +}) + +test('ZodSymbolFaker should accepts a ZodSymbol schema', () => { + const schema = z.symbol() + expect(() => zodSymbolFaker(schema)).not.toThrow() +}) + +test('ZodSymbolFaker should return a ZodSymbolFaker instance', () => { + expect(typeof zodSymbolFaker).toBe('function') + + const schema = z.symbol() + const faker = zodSymbolFaker(schema) + expect(faker instanceof ZodSymbolFaker).toBe(true) +}) + +test('ZodSymbolFaker.fake should be a function', () => { + const schema = z.symbol() + const faker = zodSymbolFaker(schema) + expect(typeof faker.fake).toBe('function') +}) + +test('ZodSymbolFaker.fake should return the given type', () => { + const schema = z.symbol() + const faker = zodSymbolFaker(schema) + expectType, z.infer>>(true) +}) + +test('ZodSymbolFaker.fake should return a valid data', () => { + const schema = z.symbol() + const faker = zodSymbolFaker(schema) + const data = faker.fake() + expect(schema.safeParse(data).success).toBe(true) +})