refactor(schemas): share portable identifier and base64 patterns (#1227)

* refactor(schemas): share identifier and canonical base64 patterns

* test: hold the offset datetime schema to the validator it replaces

The module documents that its accepted set matches z.string().datetime({ offset: true }), and nothing in the suite checked that claim. Adds table-driven accept and reject cases (leap years including the 1900/2000 century pair, month, day, hour and offset ranges, fractional and omitted seconds, missing offset, basic format) plus a differential assertion that classifies every case exactly as the replaced validator does, and asserts the emitted pattern compiles under u and v while the replaced pattern does not compile under v.

---------

Co-authored-by: felipeatp <felipe.troncosopt@gmail.com>
This commit is contained in:
morluto
2026-10-09 16:47:09 +08:00
committed by GitHub
co-authored by felipeatp
parent fef38e4d22
commit 954badbaaa
14 changed files with 224 additions and 29 deletions
+9 -9
View File
@@ -122,8 +122,9 @@ const cspSource = (
allowedOrigins: ReadonlySet<string>,
): ResponseMetadata["csp"]["directives"][number]["sources"][number] => {
const lower = token.toLowerCase();
if (/^'[a-z0-9-]+'$/u.test(lower)) return { kind: "keyword", value: lower };
if (/^[a-z][a-z0-9+.-]*:$/u.test(lower))
if (/^'[a-z0-9\x2d]+'$/u.test(lower))
return { kind: "keyword", value: lower };
if (/^[a-z][a-z0-9+.\x2d]*:$/u.test(lower))
return { kind: "scheme", value: lower };
const hostOrigin = cspHostSourceOrigin(token, baseUrl);
if (hostOrigin === undefined) return { kind: "other", value: null };
@@ -143,16 +144,15 @@ const cspHostSourceOrigin = (
token: string,
baseUrl: string,
): string | null | undefined => {
const match = /^(?:([a-z][a-z0-9+.-]*):\/\/)?([^/?#]+)(?:\/[^?#]*)?$/iu.exec(
token,
);
const match =
/^(?:([a-z][a-z0-9+.\x2d]*):\/\/)?([^\x2f?#]+)(?:\/[^?#]*)?$/iu.exec(token);
const authority = match?.[2];
if (authority === undefined || authority.length === 0) return undefined;
const scheme = (match?.[1] ?? schemeOf(baseUrl))?.toLowerCase();
if (scheme === undefined || !/^[a-z][a-z0-9+.-]*$/u.test(scheme))
if (scheme === undefined || !/^[a-z][a-z0-9+.\x2d]*$/u.test(scheme))
return undefined;
if (
!/^(?:\*|(?:\*\.)?[a-z0-9-]+(?:\.[a-z0-9-]+)*\.?)(?::(?:\*|\d+))?$/iu.test(
!/^(?:\*|(?:\*\.)?[a-z0-9\x2d]+(?:\.[a-z0-9\x2d]+)*\.?)(?::(?:\*|\d+))?$/iu.test(
authority,
)
)
@@ -332,7 +332,7 @@ const referrerPolicy = (value: string | undefined): string | null => {
for (const raw of (value ?? "").split(",")) {
const token = raw.trim().toLowerCase();
if (token === "") continue;
if (!/^[a-z-]+$/u.test(token)) return null;
if (!/^[a-z\x2d]+$/u.test(token)) return null;
if (referrerPolicies.has(token)) policy = token;
}
return policy;
@@ -340,7 +340,7 @@ const referrerPolicy = (value: string | undefined): string | null => {
const policyToken = (value: string | undefined): string | null => {
const token = (value ?? "").trim().toLowerCase();
return /^[a-z][a-z0-9_.-]*$/u.test(token) ? token : null;
return /^[a-z][a-z0-9_.\x2d]*$/u.test(token) ? token : null;
};
const nonnegativeInteger = (value: string | undefined): number | null => {