dify/web/app/components/base/markdown-with-directive/components/markdown-with-directive-schema.ts
Joel 2b1d1e9587
feat: support in site message (#33255)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com>
2026-03-11 18:32:14 +08:00

57 lines
1.6 KiB
TypeScript

import * as z from 'zod'
const commonSchema = {
className: z.string().min(1).optional(),
}
export const withIconCardListPropsSchema = z.object(commonSchema).strict()
const HTTP_URL_REGEX = /^https?:\/\//i
export const withIconCardItemPropsSchema = z.object({
...commonSchema,
icon: z.string().trim().url().refine(
value => HTTP_URL_REGEX.test(value),
'icon must be a http/https URL',
),
}).strict()
export const directivePropsSchemas = {
withiconcardlist: withIconCardListPropsSchema,
withiconcarditem: withIconCardItemPropsSchema,
} as const
export type DirectiveName = keyof typeof directivePropsSchemas
function isDirectiveName(name: string): name is DirectiveName {
return Object.hasOwn(directivePropsSchemas, name)
}
export function validateDirectiveProps(name: string, attributes: Record<string, string>): boolean {
if (!isDirectiveName(name)) {
console.error('[markdown-with-directive] Unknown directive name.', {
attributes,
directive: name,
})
return false
}
const parsed = directivePropsSchemas[name].safeParse(attributes)
if (!parsed.success) {
console.error('[markdown-with-directive] Invalid directive props.', {
attributes,
directive: name,
issues: parsed.error.issues.map(issue => ({
code: issue.code,
message: issue.message,
path: issue.path.join('.'),
})),
})
return false
}
return true
}
export type WithIconCardListProps = z.infer<typeof withIconCardListPropsSchema>
export type WithIconCardItemProps = z.infer<typeof withIconCardItemPropsSchema>