Files
Sebastian Poxhofer 171a591d84 feat(git): validate commit shas (#43988)
* feat(git): validate length commit shas

* test: adapt test mocks

* chore: update import paths

* feat(git): make the validation more strict and support repositories using sha256

* test: add fakeSha function for deterministic git commit shas in tests
2026-06-15 06:36:57 +00:00

20 lines
559 B
TypeScript

import { z } from 'zod/v4';
import { regEx } from '../regex.ts';
export const LongCommitSha = z
.string()
.regex(regEx(/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/))
.brand('LongCommitSha');
export type LongCommitSha = z.infer<typeof LongCommitSha>;
export function isLongCommitSha(value: unknown): value is LongCommitSha {
return LongCommitSha.safeParse(value).success;
}
export function toLongCommitSha(value: unknown): LongCommitSha {
if (!isLongCommitSha(value)) {
throw new Error(`Invalid long commit SHA: ${String(value)}`);
}
return value;
}