@ryhrm-gz/xincodo-lib
Version:
Utilities for working with Xincodo body documents.
84 lines (59 loc) • 1.85 kB
Markdown
Use this reference when implementing pre-save or pre-publish checks.
```ts
type BodyMigrationResult = BodyMigrationSuccess | BodyMigrationFailure;
type BodyMigrationSuccess = {
success: true;
output: Body;
migrated: boolean;
fromVersion: number;
toVersion: 1;
};
type BodyMigrationFailure = {
success: false;
issue: BodyMigrationIssue;
};
```
```ts
type BodyMigrationIssueCode = "invalid_body" | "unsupported_version";
```
`invalid_body` includes:
- non-object input
- missing version
- non-integer version
- current-version data that does not match the Body schema
- migrated data that still fails schema validation
`unsupported_version` includes:
- versions lower than the minimum supported Body version
- versions greater than the current `BODY_VERSION`
- missing migration steps between supported versions
```ts
migrateBody(input: unknown): Body
```
`migrateBody` throws `BodyMigrationError` on failure. Prefer
`safeMigrateBody` for user-facing save or publish checks.
## Default Save/Publish Policy
Block:
- `safeMigrateBody(...).success === false`
- `lintBody(...).valid === false`
- any issue in `report.errors`
Surface but do not block by default:
- `report.warnings`
- `report.infos`
The consuming application may choose stricter rules, such as blocking publish
on `missing_image_alt`, but that is not the default library policy.
```ts
const migrated = safeMigrateBody(value);
if (!migrated.success) {
return { ok: false as const, stage: "migration", issue: migrated.issue };
}
const report = lintBody(migrated.output);
if (!report.valid) {
return { ok: false as const, stage: "lint", report };
}
return { ok: true as const, body: migrated.output, warnings: report.warnings };
```