mirror of
https://github.com/outline/outline.git
synced 2026-11-29 16:40:27 +03:00
63 lines
1.6 KiB
TypeScript
63 lines
1.6 KiB
TypeScript
import isNil from "lodash/isNil";
|
|
import isUUID from "validator/lib/isUUID";
|
|
import { z } from "zod";
|
|
import { UrlHelper } from "@shared/utils/UrlHelper";
|
|
import { isUrl } from "@shared/utils/urls";
|
|
import { ValidateURL } from "@server/validation";
|
|
import { BaseSchema } from "../schema";
|
|
|
|
export const UrlsUnfurlSchema = BaseSchema.extend({
|
|
body: z
|
|
.object({
|
|
url: z.url().refine(
|
|
(val) => {
|
|
try {
|
|
const url = new URL(val);
|
|
if (url.protocol === "mention:") {
|
|
return ValidateURL.isValidMentionUrl(val);
|
|
}
|
|
return isUrl(val);
|
|
} catch (_err) {
|
|
return false;
|
|
}
|
|
},
|
|
{ message: ValidateURL.message }
|
|
),
|
|
documentId: z
|
|
.string()
|
|
.optional()
|
|
.refine(
|
|
(val) =>
|
|
val ? isUUID(val) || UrlHelper.SLUG_URL_REGEX.test(val) : true,
|
|
{
|
|
error: "must be uuid or url slug",
|
|
}
|
|
),
|
|
})
|
|
.refine(
|
|
(val) =>
|
|
!(ValidateURL.isValidMentionUrl(val.url) && isNil(val.documentId)),
|
|
{
|
|
error: "documentId required",
|
|
}
|
|
),
|
|
});
|
|
|
|
export type UrlsUnfurlReq = z.infer<typeof UrlsUnfurlSchema>;
|
|
|
|
export const UrlsCheckCnameSchema = BaseSchema.extend({
|
|
body: z.object({
|
|
hostname: z.string(),
|
|
}),
|
|
});
|
|
|
|
export type UrlsCheckCnameReq = z.infer<typeof UrlsCheckCnameSchema>;
|
|
|
|
export const UrlsCheckEmbedSchema = BaseSchema.extend({
|
|
body: z.object({
|
|
url: z.url().refine((val) => isUrl(val), { message: ValidateURL.message }),
|
|
}),
|
|
});
|
|
|
|
export type UrlsCheckEmbedReq = z.infer<typeof UrlsCheckEmbedSchema>;
|