work-hub-server/openspec/changes/swap-superstruct-valibot/specs/validation/spec.md
Nicolas HOARAU 9314e72f3e feat(schema): add shared schema package with Drizzle ORM and Valibot validation
Create @workspace/schema package with Project and Task table definitions, Drizzle relations, Valibot validation schemas (select/insert/update), and wire as workspace dependency in API and Web apps.
2026-06-04 12:46:51 +04:00

48 lines
2.3 KiB
Markdown

## MODIFIED Requirements
### Requirement: Define Valibot validators derived from Drizzle tables
The package SHALL export Valibot schemas derived from Drizzle table definitions using `createInsertSchema` and `createUpdateSchema` from `drizzle-orm/valibot`, replacing the previous hand-written Superstruct schemas.
#### Scenario: Insert schema available for each table
- **WHEN** a table is defined in Drizzle
- **THEN** a `createInsertSchema` SHALL be exported for that table, with auto-generated defaults, nullability, and column type validation
#### Scenario: Update schema available for each table
- **WHEN** a table is defined in Drizzle
- **THEN** a `createUpdateSchema` SHALL be exported for that table, with all non-generated fields marked optional
#### Scenario: Enum fields validated via picklist
- **WHEN** a `text()` column has a CHECK constraint restricting allowed values
- **THEN** the corresponding Valibot schema SHALL use `picklist()` refinement derived from the matching constant array in `drizzle/enums.ts`
#### Scenario: Validator validates correct data
- **WHEN** data matching the schema is passed to `v.parse()`
- **THEN** the validator SHALL return the validated data without throwing
#### Scenario: Validator rejects invalid data
- **WHEN** data not matching the schema is passed to `v.parse()`
- **THEN** the validator SHALL throw a `ValiError` with details about the validation failure
## REMOVED Requirements
### Requirement: Define Superstruct validators for data shapes
**Reason**: Replaced by Valibot schemas auto-derived from Drizzle tables via `drizzle-orm/valibot`
**Migration**: Replace `import { ProjectCreate } from "@workspace/schema"` with `import { projectInsert } from "@workspace/schema"` or use the Valibot equivalent
### Requirement: Validator composability
**Reason**: Redundant — Valibot schemas derived from Drizzle already handle nested types through table relations
**Migration**: No migration needed; Valibot schemas are flat by design (one schema per table)
### Requirement: Optional and default fields
**Reason**: Handled natively by `createInsertSchema` (omits generated columns) and `createUpdateSchema` (makes all fields optional). Superstruct's `optional()` and `defaulted()` combinators no longer needed.
**Migration**: Use `createUpdateSchema` for partial-update shapes instead of manually wrapping with `optional()`