Code-aware rules

Use --code to read the validators in your application code (Zod, Pydantic, Django, Laravel) and generate data that passes them. What is extracted and how it is matched to tables.

A database schema often allows more than your application does. A status column is plain text, while the code accepts only active or closed. --code <dir> (Pro and Max) reads the validators in your application and adds what it finds to the rules, so generated rows pass your own validation.

ghostrow seed --schema schema.sql --code ./app --out fixtures/
read code     8 validator rule(s) in ./app; 8 applied

Ghostrow reads the source text. It does not import, build or run your code. Without a Pro or Max license, --code stops with exit code 4: Reading application code (--code) needs a Pro or Max license.

What it reads

Ghostrow looks at .ts, .tsx, .js, .mjs, .cjs, .py and .php files, up to ten folders deep, and skips files over 2 MB. It skips node_modules, .git, dist, build, .next, vendor, venv, .venv, __pycache__, coverage, migrations, target and hidden folders.

LibraryWhereWhat is recognised
Zodz.object({...}) in TypeScript or JavaScriptz.enum, z.literal, .email(), .url(), .uuid(), .ip(), .min(), .max(), .length(), .regex(), .int(), .positive(), .nonnegative(), .gt, .gte, .lt, .lte
PydanticClasses based on BaseModel, SQLModel or BaseSettingsEmailStr, AnyUrl, HttpUrl, UUID, Literal[...], and min_length, max_length, ge, gt, le, lt, pattern or regex in Field, constr, conint, confloat, condecimal and StringConstraints
Djangomodels.Model classeschoices, max_length, email fields, and CHECK constraints of the form field >= n
LaravelForm request or controller validation arraysemail, url, uuid, integer, in:, min:, max:, size:, between:, digits: and regex:

What becomes a rule

Found in codeEffect on the data
A list of allowed valuesThe column draws only from that list, and a CHECK rule is added
email, url, uuid, ipThe column is treated as that kind of value
A regular expressionValues match the pattern
A minimum or maximum on a numberA CHECK rule such as stock ≥ 0
A minimum, maximum or exact length on a stringA CHECK rule such as first_name has at most 30 characters

Rules that come from code are marked source: code in the rules file, with the file and library as evidence, for example app/src/schemas.ts (Zod). They are checked on every generated row like any other rule.

A column you set yourself in the rules file (a pinned meaning or values) is not overridden by code.

How a validator is matched to a table

Ghostrow compares names after stripping case and separators. A schema called customerSchema and a model class Customer map to a table customers or customer. A field matches a column of the same name. If no model name matches, Ghostrow uses the field only when exactly one table has a column with that name. Anything else is left out, and the summary counts it: 8 applied, 2 didn't match a table.

Example

app/src/schemas.ts:

export const customerSchema = z.object({
  first_name: z.string().min(2).max(30),
  email: z.string().email(),
  status: z.enum(["active", "closed"]),
});
export const productSchema = z.object({
  stock: z.number().int().min(0).max(500),
  sku: z.string().regex(/^[A-Z]{3}-\d{4}$/),
});

With the shop schema, ghostrow seed --schema shop.sql --code app --out out/ reads 8 rules, applies all 8, and writes these into the rules file:

  - id: code:customers.status.values
    kind: check
    table: customers
    expr: status is one of active, closed
    enabled: true
    evidence: app/src/schemas.ts (Zod)
    source: code

The generated products.csv has SKUs such as KZA-7128, which match the pattern, and every customers.status is active or closed.

Notes

  • Code rules are re-read from your code on each run. Run with --code every time, for example in CI, or the extra rules are not applied.
  • If a code rule contradicts a rule from the schema, the run stops with exit code 2 and says which row could not be generated. Switch the rule off with enabled: false.
  • Validators that depend on custom functions, transforms or runtime values are not read.

Next