@ghostry/fabricator
v0.0.8
Published
Fabricate typed data from composable schemas.
Downloads
893
Maintainers
Readme
@ghostry/fabricator
Fabricate typed data from composable schemas.
Install
npm install @ghostry/fabricatorExample
import { initialize } from "@ghostry/fabricator";
const { T, Fabricator } = initialize();
const ProductSchema = T.object({
name: T.string.whereby({ length: { min: 1, max: 25 } }),
price: T.number.whereby({ min: 1, max: 500 }),
inStock: T.boolean,
createdAt: T.date.past,
tags: T.array(T.string.whereby({ length: { max: 15 } })).whereby({
length: { max: 5 },
}),
});
const Product = new Fabricator(ProductSchema);
const item = Product.fabricate();
// { name: "...", price: ..., inStock: ..., createdAt: ..., tags: [...] }
/**
* Fabricate with everything random except a few fields you care about.
*/
const widget = Product.fabricate({ name: "Widget", inStock: true });initialize() takes no required configuration. Its clock (by default, the wall-clock instant of the call) is the run's entropy, so every run differs, and passing that one number back as clock replays the run exactly. See the docs site for the full guide: the mental model behind Schemas and Fabricators, composing and overriding schemas, custom types, distributions, and the TypeBox adapter.
