`registry[id]` walks the prototype chain: getApiOperation("constructor")
returned the Object function, getStorageDefinition("__proto__")
returned Object.prototype, validateWithRuntimeSchemaRegistry reported
an inherited id as SCHEMA_EXECUTION_FAILED instead of
SCHEMA_NOT_REGISTERED, and isAllowedOfflineTransition threw a TypeError
for a state that is not listed.
getApiOperation, getStorageDefinition, validateWithRuntimeSchemaRegistry
and mapWithBoundaryRegistry now check Object.hasOwn before reading, and
isAllowedOfflineTransition returns false for an unlisted state.
Verified: new tests/unit/contract-registry-lookup.test.ts (4 of 6 RED
before; the boundary-mapper case already failed closed and is kept as
characterization); gates green except the known removal-fixture test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
148 lines
4.4 KiB
TypeScript
148 lines
4.4 KiB
TypeScript
import type { RuntimeValidator } from "./external-contract-runtime.ts";
|
|
import { projectValidationIssues } from "./validation-issues.ts";
|
|
|
|
export type SchemaDefinition = Readonly<{
|
|
schemaId: string;
|
|
boundary:
|
|
| "route-params"
|
|
| "route-search"
|
|
| "route-search-api-request"
|
|
| "api-request"
|
|
| "api-response";
|
|
owner: string;
|
|
runtime: "zod";
|
|
schemaVersion?: number;
|
|
direction?: "REQUEST" | "RESPONSE";
|
|
unknownFieldPolicy?: "REJECT_UNKNOWN" | "STRIP_UNKNOWN";
|
|
}>;
|
|
|
|
export type RuntimeSchemaResult =
|
|
| Readonly<{ success: true; data: unknown }>
|
|
| Readonly<{
|
|
success: false;
|
|
issues: readonly Readonly<{ path: string; code: string }>[];
|
|
}>;
|
|
|
|
/**
|
|
* D-15. The registry-facing form of the canonical `RuntimeValidator`
|
|
* (`external-contract-runtime.ts`): same `schemaId`, `parse` instead of
|
|
* `safeParse`, and dotted string paths. Build one from a validator with
|
|
* `runtimeSchemaCodecFromValidator` rather than wrapping a schema twice.
|
|
*/
|
|
export type RuntimeSchemaCodec = Readonly<{
|
|
schemaId: string;
|
|
parse(value: unknown): RuntimeSchemaResult;
|
|
}>;
|
|
|
|
export function runtimeSchemaCodecFromValidator(
|
|
validator: RuntimeValidator<unknown>,
|
|
): RuntimeSchemaCodec {
|
|
return Object.freeze({
|
|
schemaId: validator.schemaId,
|
|
parse(value: unknown): RuntimeSchemaResult {
|
|
const result = validator.safeParse(value);
|
|
if (result.success) return result;
|
|
return Object.freeze({
|
|
success: false as const,
|
|
issues: Object.freeze(
|
|
result.issues.map((issue) =>
|
|
Object.freeze({ path: issue.path.join("."), code: issue.code }),
|
|
),
|
|
),
|
|
});
|
|
},
|
|
});
|
|
}
|
|
|
|
export function composeRuntimeSchemaCodecs(
|
|
contributions: readonly Readonly<Record<string, RuntimeSchemaCodec>>[],
|
|
): Readonly<Record<string, RuntimeSchemaCodec>> {
|
|
const result: Record<string, RuntimeSchemaCodec> = Object.create(null);
|
|
for (const contribution of contributions) {
|
|
for (const [registryId, codec] of Object.entries(contribution)) {
|
|
if (
|
|
registryId !== codec.schemaId ||
|
|
Object.hasOwn(result, registryId)
|
|
) {
|
|
throw new TypeError(
|
|
`Invalid or duplicate runtime schema codec: ${registryId}`,
|
|
);
|
|
}
|
|
result[registryId] = codec;
|
|
}
|
|
}
|
|
return Object.freeze(result);
|
|
}
|
|
|
|
export function validateWithRuntimeSchemaRegistry(
|
|
schemaId: string,
|
|
value: unknown,
|
|
registry: Readonly<Record<string, RuntimeSchemaCodec>>,
|
|
): RuntimeSchemaResult {
|
|
const codec = Object.hasOwn(registry, schemaId)
|
|
? registry[schemaId]
|
|
: undefined;
|
|
if (!codec) {
|
|
return Object.freeze({
|
|
success: false,
|
|
issues: Object.freeze([
|
|
Object.freeze({ path: "", code: "SCHEMA_NOT_REGISTERED" }),
|
|
]),
|
|
});
|
|
}
|
|
try {
|
|
const result = codec.parse(value);
|
|
if (result.success) return result;
|
|
return Object.freeze({
|
|
success: false,
|
|
issues: projectValidationIssues(result.issues),
|
|
});
|
|
} catch {
|
|
return Object.freeze({
|
|
success: false,
|
|
issues: Object.freeze([
|
|
Object.freeze({ path: "", code: "SCHEMA_EXECUTION_FAILED" }),
|
|
]),
|
|
});
|
|
}
|
|
}
|
|
|
|
export function composeSchemaRegistry(
|
|
contributions: readonly Readonly<Record<string, SchemaDefinition>>[],
|
|
): Readonly<Record<string, SchemaDefinition>> {
|
|
const result: Record<string, SchemaDefinition> = Object.create(null);
|
|
for (const contribution of contributions) {
|
|
for (const [registryId, definition] of Object.entries(contribution)) {
|
|
if (
|
|
registryId !== definition.schemaId ||
|
|
!definition.owner ||
|
|
(definition.schemaVersion !== undefined &&
|
|
(!Number.isSafeInteger(definition.schemaVersion) ||
|
|
definition.schemaVersion < 1)) ||
|
|
Object.hasOwn(result, registryId)
|
|
) {
|
|
throw new TypeError(`Invalid or duplicate schema definition: ${registryId}`);
|
|
}
|
|
result[registryId] = definition;
|
|
}
|
|
}
|
|
return Object.freeze(result);
|
|
}
|
|
|
|
export const PLATFORM_SCHEMA_REGISTRY: Readonly<
|
|
Record<string, SchemaDefinition>
|
|
> = Object.freeze({
|
|
none: Object.freeze({
|
|
schemaId: "none",
|
|
boundary: "route-params",
|
|
owner: "feature-frontend-routing-release-recovery-runtime",
|
|
runtime: "zod",
|
|
}),
|
|
NotFoundSplat: Object.freeze({
|
|
schemaId: "NotFoundSplat",
|
|
boundary: "route-params",
|
|
owner: "feature-frontend-routing-release-recovery-runtime",
|
|
runtime: "zod",
|
|
}),
|
|
});
|