@mintlify/validation 0.1.955 → 0.1.957
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export const pageMetadataPersonSchema = z.object({
|
|
3
|
+
type: z.literal('user'),
|
|
4
|
+
id: z.string().min(1),
|
|
5
|
+
});
|
|
6
|
+
export const pageMetadataContributorSchema = z.union([
|
|
7
|
+
pageMetadataPersonSchema,
|
|
8
|
+
z.object({ type: z.literal('agent'), id: z.string().min(1) }),
|
|
9
|
+
]);
|
|
10
|
+
const propertyName = z.string().trim().min(1);
|
|
11
|
+
const option = z.string().min(1);
|
|
12
|
+
const options = z.array(option);
|
|
13
|
+
const webUrl = z
|
|
14
|
+
.string()
|
|
15
|
+
.url()
|
|
16
|
+
.regex(/^https?:\/\//i);
|
|
17
|
+
export const pageMetadataPropertySchema = z
|
|
18
|
+
.discriminatedUnion('type', [
|
|
19
|
+
z.object({ name: propertyName, type: z.literal('text'), value: z.string().optional() }),
|
|
20
|
+
z.object({
|
|
21
|
+
name: propertyName,
|
|
22
|
+
type: z.literal('number'),
|
|
23
|
+
value: z.number().finite().optional(),
|
|
24
|
+
}),
|
|
25
|
+
z.object({ name: propertyName, type: z.literal('date'), value: z.string().date().optional() }),
|
|
26
|
+
z.object({
|
|
27
|
+
name: propertyName,
|
|
28
|
+
type: z.literal('person'),
|
|
29
|
+
value: pageMetadataPersonSchema.optional(),
|
|
30
|
+
}),
|
|
31
|
+
z.object({ name: propertyName, type: z.literal('url'), value: webUrl.optional() }),
|
|
32
|
+
z.object({ name: propertyName, type: z.literal('checkbox'), value: z.boolean().optional() }),
|
|
33
|
+
z.object({ name: propertyName, type: z.literal('select'), options, value: option.optional() }),
|
|
34
|
+
z.object({
|
|
35
|
+
name: propertyName,
|
|
36
|
+
type: z.literal('multi_select'),
|
|
37
|
+
options,
|
|
38
|
+
value: z.array(option).optional(),
|
|
39
|
+
}),
|
|
40
|
+
])
|
|
41
|
+
.superRefine((property, context) => {
|
|
42
|
+
if (property.type !== 'select' && property.type !== 'multi_select')
|
|
43
|
+
return;
|
|
44
|
+
const values = property.value === undefined
|
|
45
|
+
? []
|
|
46
|
+
: typeof property.value === 'string'
|
|
47
|
+
? [property.value]
|
|
48
|
+
: property.value;
|
|
49
|
+
if (values.some((value) => !property.options.includes(value))) {
|
|
50
|
+
context.addIssue({
|
|
51
|
+
code: 'custom',
|
|
52
|
+
path: ['value'],
|
|
53
|
+
message: 'Value must be one of the property options',
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
export const pageMetadataFields = {
|
|
58
|
+
owner: pageMetadataPersonSchema,
|
|
59
|
+
reviewers: z.array(pageMetadataPersonSchema),
|
|
60
|
+
contributors: z.array(pageMetadataContributorSchema),
|
|
61
|
+
tags: z.array(z.string().min(1)),
|
|
62
|
+
relatedPages: z.array(z.object({ pageId: z.string().uuid() })),
|
|
63
|
+
resources: z.array(z.object({ title: z.string().min(1), url: webUrl })),
|
|
64
|
+
};
|
|
65
|
+
const displayPropertySchema = pageMetadataPropertySchema.optional().catch(undefined);
|
|
66
|
+
const propertiesSchema = z
|
|
67
|
+
.record(displayPropertySchema)
|
|
68
|
+
.transform((properties) => Object.fromEntries(Object.entries(properties).flatMap(([key, value]) => value === undefined ? [] : [[key, value]])));
|
|
69
|
+
export const pageMetadataDisplaySchema = z.object({
|
|
70
|
+
owner: pageMetadataFields.owner.optional().catch(undefined),
|
|
71
|
+
reviewers: pageMetadataFields.reviewers.optional().catch(undefined),
|
|
72
|
+
contributors: pageMetadataFields.contributors.optional().catch(undefined),
|
|
73
|
+
tags: pageMetadataFields.tags.optional().catch(undefined),
|
|
74
|
+
relatedPages: pageMetadataFields.relatedPages.optional().catch(undefined),
|
|
75
|
+
resources: pageMetadataFields.resources.optional().catch(undefined),
|
|
76
|
+
properties: propertiesSchema.optional().catch(undefined),
|
|
77
|
+
});
|
|
78
|
+
export function parsePageMetadata(value) {
|
|
79
|
+
const result = pageMetadataDisplaySchema.safeParse(value);
|
|
80
|
+
return result.success ? result.data : {};
|
|
81
|
+
}
|