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

refactor #317

Merged
merged 1 commit into from
Jan 4, 2025
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ npm install --save-dev zod-schema-faker
## Usage

```ts
import * as z from 'zod'
import { z } from 'zod'
import { install, fake } from 'zod-schema-faker'

const schema = z.number()
Expand Down Expand Up @@ -269,7 +269,7 @@ const data = {
- `function seed(value)`: sets the seed to use

```ts
import * as z from 'zod'
import { z } from 'zod'
import { install, fake, seed } from 'zod-schema-faker'

install()
Expand Down
2 changes: 1 addition & 1 deletion e2e/issue-189/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as z from 'zod'
import { z } from 'zod'
import { install, fake } from 'zod-schema-faker'
import { expectType, TypeEqual } from 'ts-expect'

Expand Down
2 changes: 1 addition & 1 deletion playground/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as z from 'zod'
import { z } from 'zod'
import { install, fake } from 'zod-schema-faker'

const main = async () => {
Expand Down
6 changes: 3 additions & 3 deletions src/fake.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as z from 'zod'
import { z } from 'zod'
import { ZodSchemaFakerError } from './error'
import { assertZodSchema } from './utils'
import { assertsZodSchema } from './utils'

export const zodTypeKindToZodTypeFaker: Map<z.ZodFirstPartyTypeKind, any /* TODO: should not use any */> = new Map()
export const zodTypeToZodTypeFaker: Map<z.ZodType, any /* TODO: should not use any */> = new Map()
Expand All @@ -9,7 +9,7 @@ export const zodTypeToZodTypeFaker: Map<z.ZodType, any /* TODO: should not use a
* generate fake data based on schema
*/
export const fake = <T extends z.ZodType>(schema: T): z.infer<T> => {
assertZodSchema(schema)
assertsZodSchema(schema)

const typeName = (schema._def as any).typeName
const faker = zodTypeToZodTypeFaker.get(schema) ?? zodTypeKindToZodTypeFaker.get(typeName)
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { z } from 'zod'
import { fake as _fake } from './fake'
import { resetSeed } from './faker'
import { resetSeed } from './random'

export const fake = <T extends z.ZodType>(schema: T): z.infer<T> => {
const result = _fake(schema)
resetSeed()
return result
}

export { seed, runFake, randexp } from './faker'
export { seed, runFake, randexp } from './random'
export { install, installCustom } from './zod-type-kind-to-zod-type-faker'
export { ZodSchemaFakerError } from './error'
export { ZodTypeFaker } from './zod-type-faker'
File renamed without changes.
10 changes: 8 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { z } from 'zod'
import { ZodSchemaFakerError } from './error'

export const assertZodSchema = (schema: any): void | never => {
if (typeof schema !== 'object' || schema === null || typeof schema._parse !== 'function') {
export function assertsZodSchema(schema: unknown): asserts schema is z.ZodTypeAny {
if (
typeof schema !== 'object' ||
schema === null ||
'_parse' in schema === false ||
typeof schema._parse !== 'function'
) {
throw new ZodSchemaFakerError(`Expected a zod schema, but got ${schema}`)
}
}
4 changes: 2 additions & 2 deletions src/zod-any-faker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as z from 'zod'
import { z } from 'zod'
import { fake } from './fake'
import { runFake } from './faker'
import { runFake } from './random'
import { ZodTypeFaker } from './zod-type-faker'

const schemas = (() => {
Expand Down
4 changes: 2 additions & 2 deletions src/zod-array-faker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as z from 'zod'
import { z } from 'zod'
import { fake } from './fake'
import { runFake } from './faker'
import { runFake } from './random'
import { ZodTypeFaker } from './zod-type-faker'

export class ZodArrayFaker<T extends z.ZodArray<any, any>> extends ZodTypeFaker<T> {
Expand Down
4 changes: 2 additions & 2 deletions src/zod-bigint-faker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as z from 'zod'
import { runFake } from './faker'
import { z } from 'zod'
import { runFake } from './random'
import { ZodTypeFaker } from './zod-type-faker'

export class ZodBigIntFaker extends ZodTypeFaker<z.ZodBigInt> {
Expand Down
4 changes: 2 additions & 2 deletions src/zod-boolean-faker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as z from 'zod'
import { runFake } from './faker'
import { z } from 'zod'
import { runFake } from './random'
import { ZodTypeFaker } from './zod-type-faker'

export class ZodBooleanFaker extends ZodTypeFaker<z.ZodBoolean> {
Expand Down
2 changes: 1 addition & 1 deletion src/zod-branded-faker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as z from 'zod'
import { z } from 'zod'
import { fake } from './fake'
import { ZodTypeFaker } from './zod-type-faker'

Expand Down
4 changes: 2 additions & 2 deletions src/zod-date-faker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as z from 'zod'
import { runFake } from './faker'
import { z } from 'zod'
import { runFake } from './random'
import { ZodTypeFaker } from './zod-type-faker'

export class ZodDateFaker extends ZodTypeFaker<z.ZodDate> {
Expand Down
4 changes: 2 additions & 2 deletions src/zod-default-faker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as z from 'zod'
import { z } from 'zod'
import { fake } from './fake'
import { runFake } from './faker'
import { runFake } from './random'
import { ZodTypeFaker } from './zod-type-faker'

export class ZodDefaultFaker<T extends z.ZodDefault<any>> extends ZodTypeFaker<T> {
Expand Down
4 changes: 2 additions & 2 deletions src/zod-discriminated-union-faker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as z from 'zod'
import { z } from 'zod'
import { fake } from './fake'
import { runFake } from './faker'
import { runFake } from './random'
import { ZodTypeFaker } from './zod-type-faker'

export class ZodDiscriminatedUnionFaker<T extends z.ZodDiscriminatedUnion<any, any>> extends ZodTypeFaker<T> {
Expand Down
2 changes: 1 addition & 1 deletion src/zod-effects-faker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as z from 'zod'
import { z } from 'zod'
import { fake } from './fake'
import { ZodTypeFaker } from './zod-type-faker'

Expand Down
4 changes: 2 additions & 2 deletions src/zod-enum-faker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as z from 'zod'
import { runFake } from './faker'
import { z } from 'zod'
import { runFake } from './random'
import { ZodTypeFaker } from './zod-type-faker'

export class ZodEnumFaker<T extends z.ZodEnum<any>> extends ZodTypeFaker<T> {
Expand Down
2 changes: 1 addition & 1 deletion src/zod-function-faker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as z from 'zod'
import { z } from 'zod'
import { fake } from './fake'
import { ZodTypeFaker } from './zod-type-faker'

Expand Down
2 changes: 1 addition & 1 deletion src/zod-intersection-faker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as z from 'zod'
import { z } from 'zod'
import { ZodSchemaFakerError } from './error'
import { fake } from './fake'
import { ZodTypeFaker } from './zod-type-faker'
Expand Down
2 changes: 1 addition & 1 deletion src/zod-lazy-faker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as z from 'zod'
import { z } from 'zod'
import { ZodSchemaFakerError } from './error'
import { fake } from './fake'
import { ZodTypeFaker } from './zod-type-faker'
Expand Down
2 changes: 1 addition & 1 deletion src/zod-literal-faker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as z from 'zod'
import { z } from 'zod'
import { ZodTypeFaker } from './zod-type-faker'

export class ZodLiteralFaker<T extends z.ZodLiteral<any>> extends ZodTypeFaker<T> {
Expand Down
4 changes: 2 additions & 2 deletions src/zod-map-faker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as z from 'zod'
import { z } from 'zod'
import { fake } from './fake'
import { ZodTypeFaker } from './zod-type-faker'
import { runFake } from './faker'
import { runFake } from './random'

export class ZodMapFaker<T extends z.ZodMap<any, any>> extends ZodTypeFaker<T> {
fake(): z.infer<T> {
Expand Down
2 changes: 1 addition & 1 deletion src/zod-nan-faker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as z from 'zod'
import { z } from 'zod'
import { ZodTypeFaker } from './zod-type-faker'

export class ZodNaNFaker extends ZodTypeFaker<z.ZodNaN> {
Expand Down
4 changes: 2 additions & 2 deletions src/zod-native-enum-faker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as z from 'zod'
import { runFake } from './faker'
import { z } from 'zod'
import { runFake } from './random'
import { ZodTypeFaker } from './zod-type-faker'

const getValidEnumValues = (obj: any) => {
Expand Down
2 changes: 1 addition & 1 deletion src/zod-never-faker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as z from 'zod'
import { z } from 'zod'
import { ZodTypeFaker } from './zod-type-faker'

export class ZodNeverFaker extends ZodTypeFaker<z.ZodNever> {
Expand Down
2 changes: 1 addition & 1 deletion src/zod-null-faker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as z from 'zod'
import { z } from 'zod'
import { ZodTypeFaker } from './zod-type-faker'

export class ZodNullFaker extends ZodTypeFaker<z.ZodNull> {
Expand Down
4 changes: 2 additions & 2 deletions src/zod-nullable-faker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as z from 'zod'
import { z } from 'zod'
import { fake } from './fake'
import { runFake } from './faker'
import { runFake } from './random'
import { ZodTypeFaker } from './zod-type-faker'

export class ZodNullableFaker<T extends z.ZodNullable<any>> extends ZodTypeFaker<T> {
Expand Down
4 changes: 2 additions & 2 deletions src/zod-number-faker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as z from 'zod'
import { runFake } from './faker'
import { z } from 'zod'
import { runFake } from './random'
import { ZodTypeFaker } from './zod-type-faker'

export class ZodNumberFaker extends ZodTypeFaker<z.ZodNumber> {
Expand Down
2 changes: 1 addition & 1 deletion src/zod-object-faker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as z from 'zod'
import { z } from 'zod'
import { fake } from './fake'
import { ZodTypeFaker } from './zod-type-faker'

Expand Down
4 changes: 2 additions & 2 deletions src/zod-optional-faker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as z from 'zod'
import { z } from 'zod'
import { fake } from './fake'
import { runFake } from './faker'
import { runFake } from './random'
import { ZodTypeFaker } from './zod-type-faker'

export class ZodOptionalFaker<T extends z.ZodOptional<any>> extends ZodTypeFaker<T> {
Expand Down
2 changes: 1 addition & 1 deletion src/zod-pipe-faker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as z from 'zod'
import { z } from 'zod'
import { fake } from './fake'
import { ZodTypeFaker } from './zod-type-faker'

Expand Down
4 changes: 2 additions & 2 deletions src/zod-promise-faker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as z from 'zod'
import { z } from 'zod'
import { fake } from './fake'
import { ZodTypeFaker } from './zod-type-faker'
import { runFake } from './faker'
import { runFake } from './random'

export class ZodPromiseFaker<T extends z.ZodPromise<any>> extends ZodTypeFaker<T> {
fake(): z.infer<T> {
Expand Down
2 changes: 1 addition & 1 deletion src/zod-readonly-faker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as z from 'zod'
import { z } from 'zod'
import { fake } from './fake'
import { ZodTypeFaker } from './zod-type-faker'

Expand Down
4 changes: 2 additions & 2 deletions src/zod-record-faker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as z from 'zod'
import { z } from 'zod'
import { fake } from './fake'
import { ZodTypeFaker } from './zod-type-faker'
import { runFake } from './faker'
import { runFake } from './random'
import { ZodSchemaFakerError } from './error'

export class ZodRecordFaker<T extends z.ZodRecord<any, any>> extends ZodTypeFaker<T> {
Expand Down
4 changes: 2 additions & 2 deletions src/zod-set-faker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as z from 'zod'
import { z } from 'zod'
import { fake } from './fake'
import { runFake } from './faker'
import { runFake } from './random'
import { ZodTypeFaker } from './zod-type-faker'

export class ZodSetFaker<T extends z.ZodSet<any>> extends ZodTypeFaker<T> {
Expand Down
6 changes: 3 additions & 3 deletions src/zod-string-faker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as z from 'zod'
import { ZodSchemaFakerError } from './error'
import { randexp, runFake } from './faker'
import { z } from 'zod'
import { ZodTypeFaker } from './zod-type-faker'
import { ZodSchemaFakerError } from './error'
import { randexp, runFake } from './random'

const averageWordLength = 5
const averageSentenceLength = averageWordLength * 15
Expand Down
2 changes: 1 addition & 1 deletion src/zod-symbol-faker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as z from 'zod'
import { z } from 'zod'
import { ZodTypeFaker } from './zod-type-faker'

export class ZodSymbolFaker<T extends z.ZodSymbol> extends ZodTypeFaker<T> {
Expand Down
4 changes: 2 additions & 2 deletions src/zod-tuple-faker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as z from 'zod'
import { fake } from './fake'
import { z } from 'zod'
import { ZodTypeFaker } from './zod-type-faker'
import { fake } from './fake'

export class ZodTupleFaker<T extends z.ZodTuple<any, any>> extends ZodTypeFaker<T> {
fake(): z.infer<T> {
Expand Down
14 changes: 5 additions & 9 deletions src/zod-type-faker.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import * as z from 'zod'
import { assertZodSchema } from './utils'
import { z } from 'zod'
import { assertsZodSchema } from './utils'

export abstract class ZodTypeFaker<T extends z.ZodType<any, any, any>> {
protected schema: T

constructor(schema: T) {
assertZodSchema(schema)

this.schema = schema
export abstract class ZodTypeFaker<T extends z.ZodTypeAny> {
constructor(protected schema: T) {
assertsZodSchema(schema)
}

abstract fake(): z.infer<T>
Expand Down
2 changes: 1 addition & 1 deletion src/zod-type-kind-to-zod-type-faker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as z from 'zod'
import { z } from 'zod'
import { zodTypeKindToZodTypeFaker, zodTypeToZodTypeFaker } from './fake'
import { ZodAnyFaker } from './zod-any-faker'
import { ZodArrayFaker } from './zod-array-faker'
Expand Down
2 changes: 1 addition & 1 deletion src/zod-undefined-faker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as z from 'zod'
import { z } from 'zod'
import { ZodTypeFaker } from './zod-type-faker'

export class ZodUndefinedFaker extends ZodTypeFaker<z.ZodUndefined> {
Expand Down
6 changes: 3 additions & 3 deletions src/zod-union-faker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as z from 'zod'
import { fake } from './fake'
import { runFake } from './faker'
import { z } from 'zod'
import { ZodTypeFaker } from './zod-type-faker'
import { fake } from './fake'
import { runFake } from './random'

export class ZodUnionFaker<T extends z.ZodUnion<any>> extends ZodTypeFaker<T> {
fake(): z.infer<T> {
Expand Down
4 changes: 2 additions & 2 deletions src/zod-unknown-faker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as z from 'zod'
import { fake } from './fake'
import { z } from 'zod'
import { ZodTypeFaker } from './zod-type-faker'
import { fake } from './fake'

export class ZodUnknownFaker extends ZodTypeFaker<z.ZodUnknown> {
fake(): z.infer<z.ZodUnknown> {
Expand Down
3 changes: 1 addition & 2 deletions src/zod-void-faker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as z from 'zod'
import { fake } from './fake'
import { z } from 'zod'
import { ZodTypeFaker } from './zod-type-faker'

export class ZodVoidFaker extends ZodTypeFaker<z.ZodVoid> {
Expand Down
2 changes: 1 addition & 1 deletion tests/fake.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, test } from 'vitest'
import * as z from 'zod'
import { z } from 'zod'
import { fake, install } from '../src'
import { uninstall } from '../src/zod-type-kind-to-zod-type-faker'

Expand Down
5 changes: 2 additions & 3 deletions tests/faker.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { expect, test } from 'vitest'
import * as z from 'zod'
import { install, fake, seed } from '../src'
import { runFake } from '../src/faker'
import { z } from 'zod'
import { install, fake, seed, runFake } from '../src'

test('seed', () => {
install()
Expand Down
Loading
Loading