json-schema-faker
Version:
Generate valid JSON data from JSON Schema definitions
205 lines • 8.09 kB
TypeScript
export type JsonSchema = JsonSchemaObject | boolean;
export interface JsonSchemaObject {
$schema?: string;
$id?: string;
$ref?: string;
$defs?: Record<string, JsonSchema>;
type?: string | string[];
enum?: unknown[];
const?: unknown;
minimum?: number;
maximum?: number;
exclusiveMinimum?: number;
exclusiveMaximum?: number;
multipleOf?: number;
autoIncrement?: boolean;
initialOffset?: number;
minLength?: number;
maxLength?: number;
pattern?: string;
format?: string;
/** Template string with #{propName} placeholders resolved from sibling properties */
template?: string;
items?: JsonSchema;
prefixItems?: JsonSchema[];
contains?: JsonSchema;
/** Internal: multiple contains constraints collected from allOf merging */
containsAll?: JsonSchema[];
minItems?: number;
maxItems?: number;
uniqueItems?: boolean;
minContains?: number;
maxContains?: number;
properties?: Record<string, JsonSchema>;
required?: string[];
additionalProperties?: JsonSchema;
patternProperties?: Record<string, JsonSchema>;
minProperties?: number;
maxProperties?: number;
propertyNames?: JsonSchema;
allOf?: JsonSchema[];
anyOf?: JsonSchema[];
oneOf?: JsonSchema[];
not?: JsonSchema;
if?: JsonSchema;
then?: JsonSchema;
else?: JsonSchema;
[key: string]: unknown;
}
export interface RefResolver {
(ref: string): Promise<JsonSchema> | JsonSchema;
}
export interface RemoteResolverOptions {
/** Base URL for resolving relative references */
baseUrl?: string;
/** Custom fetch function for HTTP requests */
fetch?: (url: string) => Promise<Response>;
/** Custom file reader function */
readFile?: (path: string) => Promise<string>;
/** Cache for resolved schemas */
cache?: Map<string, JsonSchema>;
}
export interface GenerateOptions {
seed?: number;
maxDepth?: number;
maxDefaultItems?: number;
/** Probability (0-1) of generating optional properties */
optionalsProbability?: number;
formats?: Record<string, (random: Random) => string>;
refResolver?: RefResolver;
/** Override schema minItems for array generation */
minItems?: number;
/** Override schema maxItems for array generation */
maxItems?: number;
/** Override schema minLength for string generation */
minLength?: number;
/** Override schema maxLength for string generation */
maxLength?: number;
/** Use schema default values when generating */
useDefaultValue?: boolean;
/** Always generate optional properties */
alwaysFakeOptionals?: boolean;
/** Use fixed/deterministic probabilities for testing */
fixedProbabilities?: boolean;
/** Fill properties beyond required (for nested required propagation) */
fillProperties?: boolean;
/** External extensions for generating values (e.g., faker, chance, yaml) */
extensions?: {
/** @faker-js/faker instance */
faker?: any;
/** chance instance */
chance?: any;
/** YAML library instance (must have stringify method) */
yaml?: any;
};
/** @deprecated Use extensions.yaml instead */
yaml?: any;
/** Enable jsonPath resolution for cross-references within generated data */
resolveJsonPath?: boolean;
/** Minimum reference depth for recursive schemas */
refDepthMin?: number;
/** Maximum reference depth for recursive schemas */
refDepthMax?: number;
/** Alias for setting both refDepthMin and refDepthMax to the same value */
refDepth?: number;
/** Use examples array values when generating */
useExamplesValue?: boolean;
/** Remove specified properties from generated objects */
pruneProperties?: string[];
/** Handle invalid types by returning a default product instead of throwing */
failOnInvalidTypes?: boolean;
/** Default value to return for invalid types when failOnInvalidTypes is false */
defaultInvalidTypeProduct?: unknown;
/** Minimum date/time for date-time format */
minDateTime?: string;
/** Maximum date/time for date-time format */
maxDateTime?: string;
/** Validate that $schema is 2020-12 (throws if not). Default: false for backwards compatibility */
validateSchemaVersion?: boolean;
/** Remap schema property names before processing. E.g. { "x-faker": "faker", "definitions": "$defs" } */
propAliases?: Record<string, string>;
/** Transform generated output values using the final output-tree JSON pointer path */
outputTransform?: (value: unknown, schema: JsonSchema, path: string) => unknown | Promise<unknown>;
/** Options to pass to JSON.stringify for generateJSON */
jsonStringifyOptions?: {
/** JSON.stringify space parameter */
space?: number | string;
/** JSON.stringify replacer function */
replacer?: (key: string, value: unknown) => unknown;
};
}
export interface GenerateContext {
random: Random;
maxDepth: number;
maxDefaultItems: number;
optionalsProbability: number;
depth: number;
refRegistry: Map<string, JsonSchema>;
refStack: Set<string>;
formatRegistry: Map<string, (random: Random) => string>;
refResolver?: RefResolver;
/** Override schema minItems for array generation */
minItems?: number;
/** Override schema maxItems for array generation */
maxItems?: number;
/** Override schema minLength for string generation */
minLength?: number;
/** Override schema maxLength for string generation */
maxLength?: number;
/** Use schema default values when generating */
useDefaultValue?: boolean;
/** Current path in the schema (JSON pointer) */
path: string;
/** Current path in the generated output (JSON pointer) */
outputPath: string;
/** Always generate optional properties */
alwaysFakeOptionals?: boolean;
/** Use fixed/deterministic probabilities for testing */
fixedProbabilities?: boolean;
/** Fill properties beyond required (for nested required propagation) */
fillProperties?: boolean;
/** External extensions for generating values (e.g., faker, chance) */
extensions?: {
faker?: any;
chance?: any;
};
/** Enable jsonPath resolution for cross-references within generated data */
resolveJsonPath?: boolean;
/** Counters for autoIncrement, keyed by path */
autoIncrementCounters?: Map<string, number>;
/** Current reference depth counter */
refDepth: number;
/** Flag indicating max ref depth has been reached */
refDepthReached?: boolean;
/** Minimum reference depth for recursive schemas */
refDepthMin?: number;
/** Maximum reference depth for recursive schemas */
refDepthMax?: number;
/** Use examples array values when generating */
useExamplesValue?: boolean;
/** Remove specified properties from generated objects */
pruneProperties?: string[];
/** Handle invalid types by returning a default product instead of throwing */
failOnInvalidTypes?: boolean;
/** Default value to return for invalid types when failOnInvalidTypes is false */
defaultInvalidTypeProduct?: unknown;
/** Minimum date/time for date-time format */
minDateTime?: string;
/** Maximum date/time for date-time format */
maxDateTime?: string;
/** Pre-resolved type (from type array) to ensure consistent type selection */
resolvedType?: string;
/** Remap schema property names before processing. E.g. { "x-faker": "faker" } */
propAliases?: Record<string, string>;
/** Transform generated output values using the final output-tree JSON pointer path */
outputTransform?: (value: unknown, schema: JsonSchema, path: string) => unknown | Promise<unknown>;
}
export interface Random {
next(): number;
int(min: number, max: number): number;
bool(probability?: number): boolean;
pick<T>(arr: readonly T[]): T;
shuffle<T>(arr: T[]): T[];
fork(): Random;
}
//# sourceMappingURL=types.d.ts.map