UNPKG

zod-schema-faker

Version:

Generates mock data from zod schema. Powered by @faker-js/faker and randexp.js

82 lines (68 loc) 2.36 kB
import { Faker } from '@faker-js/faker'; import { z } from 'zod/v3'; /** * Generate fake data based on schema. * * @throws when a corresponding faker is not registered. */ export declare const fake: <T extends z.ZodType>(schema: T) => z.infer<T>; /** * Get the current faker instance. * * @default fakerEN */ export declare function getFaker(): Faker; /** * Install fakers for built-in types, must be called before using {@link fake}. */ export declare function install(): void; /** * Install fakers for custom schemas, must be called before using {@link fake}. */ export declare function installCustom<T extends z.ZodTypeAny>(schema: T, faker: typeof ZodTypeFakerConcrete<T>): void; /** * This exists for compatibility with the previous version. Will be removed in next major version. * * @deprecated Use {@link setFaker} instead. * * @todo Remove in next major version. */ export declare const installFaker: typeof setFaker; /** * Create random strings that match a given regular expression. */ export declare const randexp: (pattern: string | RegExp, flags?: string) => string; /** * This exists for compatibility with the previous version. Will be removed in next major version. * * @deprecated Use {@link getFaker} instead. * * @todo Remove in next major version. */ export declare const runFake: <Runner extends (faker: Faker) => any>(runner: Awaited<ReturnType<Runner>> extends ReturnType<Runner> ? Runner : never) => ReturnType<Runner>; /** * Sets the seed or generates a new one. * * This method is intended to allow for consistent values in tests, so you might want to use hardcoded values as the seed. */ export declare const seed: (value?: number) => void; /** * Use given faker instance instead of the default one. * * @see https://fakerjs.dev/guide/localization.html for more information. */ export declare function setFaker(faker: Faker): void; export declare class ZodSchemaFakerError extends Error { } /** * Base class for fakers. */ export declare abstract class ZodTypeFaker<T extends z.ZodTypeAny> { protected schema: T; constructor(schema: T); abstract fake(): z.infer<T>; } declare class ZodTypeFakerConcrete<T extends z.ZodTypeAny> extends ZodTypeFaker<T> { fake(): z.infer<T>; } export { }