2023-01-19 16:24:55 +00:00
|
|
|
|
import { z } from 'astro/zod';
|
|
|
|
|
|
2022-05-03 22:26:13 +00:00
|
|
|
|
/** Normalize URL to its canonical form */
|
|
|
|
|
export function createCanonicalURL(url: string, base?: string): URL {
|
|
|
|
|
let pathname = url.replace(/\/index.html$/, ''); // index.html is not canonical
|
|
|
|
|
pathname = pathname.replace(/\/1\/?$/, ''); // neither is a trailing /1/ (impl. detail of collections)
|
2022-09-28 15:13:33 +00:00
|
|
|
|
if (!getUrlExtension(url)) pathname = pathname.replace(/(\/+)?$/, '/'); // add trailing slash if there’s no extension
|
2022-05-03 22:26:13 +00:00
|
|
|
|
pathname = pathname.replace(/\/+/g, '/'); // remove duplicate slashes (URL() won’t)
|
|
|
|
|
return new URL(pathname, base);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Check if a URL is already valid */
|
|
|
|
|
export function isValidURL(url: string): boolean {
|
|
|
|
|
try {
|
|
|
|
|
new URL(url);
|
|
|
|
|
return true;
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-09-28 15:13:33 +00:00
|
|
|
|
|
|
|
|
|
function getUrlExtension(url: string) {
|
|
|
|
|
const lastDot = url.lastIndexOf('.');
|
|
|
|
|
const lastSlash = url.lastIndexOf('/');
|
|
|
|
|
return lastDot > lastSlash ? url.slice(lastDot + 1) : '';
|
|
|
|
|
}
|
2023-01-19 16:24:55 +00:00
|
|
|
|
|
|
|
|
|
const flattenErrorPath = (errorPath: (string | number)[]) => errorPath.join('.');
|
|
|
|
|
|
|
|
|
|
export const errorMap: z.ZodErrorMap = (error, ctx) => {
|
|
|
|
|
if (error.code === 'invalid_type') {
|
|
|
|
|
const badKeyPath = JSON.stringify(flattenErrorPath(error.path));
|
|
|
|
|
if (error.received === 'undefined') {
|
|
|
|
|
return { message: `${badKeyPath} is required.` };
|
|
|
|
|
} else {
|
|
|
|
|
return { message: `${badKeyPath} should be ${error.expected}, not ${error.received}.` };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return { message: ctx.defaultError };
|
|
|
|
|
};
|