Move docs/ package mirror into document-navigator plugin
Relocate the ~6k-file docs tree from the repo root into plugins/document-navigator/docs/ so the plugin is self-contained and portable. docs/superpowers/ (specs and plans) stays at the repo root. Moved via cp+rm rather than git mv because directory rename() fails with EPERM on this WSL /mnt DrvFs mount; git rename-detection pairs the identical-content files at commit time. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
# schemas
|
||||
|
||||
<!-- begin:auto-docs -->
|
||||
_(no submodules)_
|
||||
<!-- end:auto-docs -->
|
||||
|
||||
## Purpose
|
||||
|
||||
`schemas.ts` exposes the Zod validators that guard every public input
|
||||
boundary on the `@suaveplan/audience-rules` store API, plus the `parseOrThrow`
|
||||
helper that converts a `ZodError` into a typed `AudienceRulesError` with code
|
||||
`AUDIENCE_RULES.INVALID_INPUT`. Centralising the validators in one module
|
||||
keeps the constraint surface auditable and lets consumers reuse the same
|
||||
schemas (e.g. for HTTP-layer body parsing in a future `api-plugin-audience-rules`)
|
||||
without depending on internal helpers.
|
||||
|
||||
## Features
|
||||
|
||||
- `segmentOperatorSchema` — Zod enum mirroring the 14 `SegmentOperator`
|
||||
values.
|
||||
- `logicalOpSchema` — Zod enum for `"and" | "or"`.
|
||||
- `refreshModeSchema` — Zod enum for `"realtime" | "scheduled" | "manual"`.
|
||||
- `segmentRuleSchema` — validates `{ field, operator, value?, logicalOp? }`.
|
||||
- `createSegmentInputSchema` — validates the full `CreateSegmentInput`.
|
||||
- `updateSegmentInputSchema` — validates the partial `UpdateSegmentInput`
|
||||
and rejects an empty patch (all fields undefined).
|
||||
- `parseOrThrow(schema, input, label)` — runs `safeParse`, returns the typed
|
||||
value on success, throws `AudienceRulesError` with `INVALID_INPUT` and a
|
||||
human-readable `path: message` summary on failure.
|
||||
|
||||
## Usage: Basic Example
|
||||
|
||||
```ts
|
||||
import {
|
||||
createSegmentInputSchema,
|
||||
parseOrThrow,
|
||||
} from "@suaveplan/audience-rules";
|
||||
|
||||
const input = parseOrThrow(
|
||||
createSegmentInputSchema,
|
||||
{ tenantId: "t1", name: "VIPs", rules: [], refreshMode: "manual" },
|
||||
"createSegment"
|
||||
);
|
||||
```
|
||||
|
||||
## Usage: Advanced Example
|
||||
|
||||
```ts
|
||||
import {
|
||||
AUDIENCE_RULES_CODES,
|
||||
AudienceRulesError,
|
||||
parseOrThrow,
|
||||
segmentRuleSchema,
|
||||
} from "@suaveplan/audience-rules";
|
||||
|
||||
try {
|
||||
parseOrThrow(
|
||||
segmentRuleSchema,
|
||||
{ field: "", operator: "eq", value: "x" },
|
||||
"segmentRule"
|
||||
);
|
||||
} catch (err) {
|
||||
if (
|
||||
err instanceof AudienceRulesError &&
|
||||
err.code === AUDIENCE_RULES_CODES.INVALID_INPUT
|
||||
) {
|
||||
// err.cause is the original ZodError with the full issues array
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## API Reference
|
||||
|
||||
- `segmentOperatorSchema: z.ZodEnum<SegmentOperator>`
|
||||
- `logicalOpSchema: z.ZodEnum<LogicalOp>`
|
||||
- `refreshModeSchema: z.ZodEnum<RefreshMode>`
|
||||
- `segmentRuleSchema: z.ZodObject<{...}>` — produces a `SegmentRule`.
|
||||
- `createSegmentInputSchema: z.ZodObject<{...}>` — produces a
|
||||
`CreateSegmentInput`.
|
||||
- `updateSegmentInputSchema: z.ZodObject<{...}>` — produces an
|
||||
`UpdateSegmentInput`; refines that at least one optional field is present.
|
||||
- `parseOrThrow<T>(schema: z.ZodType<T>, input: unknown, label: string): T` —
|
||||
returns parsed value, or throws `AudienceRulesError` with code
|
||||
`AUDIENCE_RULES.INVALID_INPUT` and the original `ZodError` preserved in
|
||||
`cause`.
|
||||
|
||||
## Implementation Notes
|
||||
|
||||
- Schemas use `z.string().min(1)` for identity-like fields (`tenantId`,
|
||||
`name`, `field`) so empty strings are rejected at the boundary.
|
||||
- The `updateSegmentInputSchema` `.refine` predicate prevents callers from
|
||||
shipping an empty patch — every `update` MUST change at least one field, or
|
||||
it is rejected with `INVALID_INPUT`.
|
||||
- The error message produced by `parseOrThrow` lists every issue as
|
||||
`path: message; path: message` so server logs and OTel span events surface
|
||||
every validation failure, not just the first.
|
||||
Reference in New Issue
Block a user