mirror of
https://github.com/morluto/rea.git
synced 2026-10-28 02:15:44 +00:00
refactor(javascript): parse runtime evidence into one verified owner
This commit is contained in:
1 parent
415145d04f
commit
006b295f1d
15 files changed
+839
-512
No files matched your search
@@ -0,0 +1,57 @@
|
||||
import { z } from "zod";
|
||||
import type { Evidence } from "../evidence.js";
|
||||
|
||||
/** Prefix caller paths on a nested Zod validation boundary. */
|
||||
export const parseAtPath = <Value>(
|
||||
parse: (input: unknown) => Value,
|
||||
input: unknown,
|
||||
path: readonly (string | number)[],
|
||||
): Value => {
|
||||
try {
|
||||
return parse(input);
|
||||
} catch (cause: unknown) {
|
||||
if (cause instanceof z.ZodError)
|
||||
throw new z.ZodError(
|
||||
cause.issues.map((issue) => ({
|
||||
...issue,
|
||||
path: [...path, ...issue.path],
|
||||
})),
|
||||
);
|
||||
throw cause;
|
||||
}
|
||||
};
|
||||
|
||||
/** Represent one semantic caller constraint as a standard validation issue. */
|
||||
export const invalidInput = (
|
||||
path: readonly (string | number)[],
|
||||
message: string,
|
||||
): z.ZodError => new z.ZodError([{ code: "custom", path: [...path], message }]);
|
||||
|
||||
/** Verify an authenticated Evidence record against one runtime workflow. */
|
||||
export const assertEvidenceIdentity = (
|
||||
evidence: Evidence,
|
||||
expected: {
|
||||
readonly operation: string;
|
||||
readonly predicate: string;
|
||||
readonly providerId: string;
|
||||
readonly providerName: string;
|
||||
readonly providerVersion: string;
|
||||
readonly authority: Evidence["authority"];
|
||||
readonly confidence: Evidence["confidence"];
|
||||
},
|
||||
path: readonly (string | number)[],
|
||||
): void => {
|
||||
if (
|
||||
evidence.operation !== expected.operation ||
|
||||
evidence.predicate_type !== expected.predicate ||
|
||||
evidence.provider.id !== expected.providerId ||
|
||||
evidence.provider.name !== expected.providerName ||
|
||||
evidence.provider.version !== expected.providerVersion ||
|
||||
evidence.authority !== expected.authority ||
|
||||
evidence.confidence !== expected.confidence
|
||||
)
|
||||
throw invalidInput(
|
||||
path,
|
||||
`Evidence does not match the supported ${expected.operation} contract`,
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user