feat(supabase): publish gate on the Rust core's sheet.check, fail closed

publishSheetVersion(client, versionId, validate) takes the core's
sheet.check (the package never loads the module itself); anything but a
publishable verdict refuses publication before the RPC: invalid_shape,
not_publishable (same message as before: first error code), and
validator_unavailable when the core cannot run. Tests run on the real
Node core; the shape case now asserts the closed kind instead of any
throw. The package no longer depends on @faunosteo/domain.
This commit is contained in:
bchanot committed 2026-10-14 00:29:03 +02:00
1 parent 21906c4358
commit 6891f9ea25
6 files changed
+119 -18

No files matched your search

+73 -8
View File
@@ -1,18 +1,83 @@
import {
AnatomicalSheetDocumentSchema,
assertPublishable,
} from '@faunosteo/domain'
import type { FaunosteoClient } from './client'
/**
* Publication gate on the client side: the full Zod shape and every
* integrity rule run before the RPC, so the editor gets the complete issue
* list. The database re-checks the referential rules on its own
* Publication gate on the client side: the full shape and every integrity
* rule run before the RPC, so the editor gets the complete issue list. The
* rules run in the Rust core (R2): the caller passes its `sheet.check`
* operation, so this package never loads the module itself. The gate fails
* closed: anything but a publishable verdict refuses publication. The
* database re-checks the referential rules on its own
* (private.sheet_document_issues); this call never replaces that guard.
*/
export interface SheetIssue {
code: string
severity: 'error' | 'warning'
path: string
message: string
}
/** The core's verdict on a document (`sheet.check`), or why it could not give one. */
export type SheetCheckOutcome =
| { ok: true; value: { issues: SheetIssue[]; publishable: boolean } }
| { ok: false; kind: string; detail?: string }
export type SheetValidator = (content: unknown) => SheetCheckOutcome
export type SheetPublishErrorKind =
/** The document does not match the strict schema. */
| 'invalid_shape'
/** Integrity errors (warnings never block). */
| 'not_publishable'
/** The validator could not run (core unavailable, trap): publication refused. */
| 'validator_unavailable'
export class SheetPublishError extends Error {
constructor(
readonly kind: SheetPublishErrorKind,
readonly issues: SheetIssue[] = [],
detail = '',
) {
super(SheetPublishError.messageOf(kind, issues, detail))
this.name = 'SheetPublishError'
}
private static messageOf(
kind: SheetPublishErrorKind,
issues: SheetIssue[],
detail: string,
): string {
if (kind === 'not_publishable') {
const errors = issues.filter((issue) => issue.severity === 'error')
return (
`Sheet document is not publishable: ${errors.length} error(s), ` +
`first: ${errors[0]?.code ?? 'none'}`
)
}
if (kind === 'invalid_shape') {
return `Sheet document does not match the schema${detail ? `: ${detail}` : ''}`
}
return `Sheet validator unavailable${detail ? ` (${detail})` : ''}`
}
}
/** Throws unless the validator says the document is publishable. */
function assertPublishable(validate: SheetValidator, content: unknown): void {
const check = validate(content)
if (!check.ok) {
throw check.kind === 'invalid_shape'
? new SheetPublishError('invalid_shape', [], check.detail)
: new SheetPublishError('validator_unavailable', [], check.kind)
}
if (!check.value.publishable) {
throw new SheetPublishError('not_publishable', check.value.issues)
}
}
export async function publishSheetVersion(
client: FaunosteoClient,
versionId: string,
validate: SheetValidator,
): Promise<string> {
const draft = await client
.from('anatomical_sheet_versions')
@@ -21,7 +86,7 @@ export async function publishSheetVersion(
.single()
if (draft.error) throw draft.error
assertPublishable(AnatomicalSheetDocumentSchema.parse(draft.data.content))
assertPublishable(validate, draft.data.content)
const published = await client.rpc('publish_sheet_version', {
version_id: versionId,
+8 -1
View File
@@ -43,7 +43,14 @@ export {
} from './profiles'
/** owner | admin | moderator | support. Not a professional status. */
export { fetchOwnPlatformRoles, type PlatformRole } from './platform-roles'
export { publishSheetVersion } from './anatomical-sheets'
export {
publishSheetVersion,
SheetPublishError,
type SheetCheckOutcome,
type SheetIssue,
type SheetPublishErrorKind,
type SheetValidator,
} from './anatomical-sheets'
export {
ASSET_FAILURE_KINDS,
ASSET_OWNER_COLUMNS,