25 lines
718 B
TypeScript
25 lines
718 B
TypeScript
import { z } from "zod";
|
|
|
|
/**
|
|
* Compile a checked-in JSON Schema and assert a concrete artifact against it.
|
|
* Conversion failures are fatal too: an unsupported or malformed schema must
|
|
* not silently turn a release schema into documentation-only metadata.
|
|
*/
|
|
export function assertMatchesJsonSchema(
|
|
schemaDocument: unknown,
|
|
value: unknown,
|
|
label: string,
|
|
): void {
|
|
try {
|
|
const schema = z.fromJSONSchema(schemaDocument as never);
|
|
const result = schema.safeParse(value);
|
|
if (!result.success) {
|
|
throw new TypeError(z.prettifyError(result.error));
|
|
}
|
|
} catch (error) {
|
|
throw new TypeError(`${label} does not satisfy its checked-in JSON Schema.`, {
|
|
cause: error,
|
|
});
|
|
}
|
|
}
|