1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
import { reference, z } from "astro:content";
import { isValidLocale } from "../../utils/lang.ts";
import { get } from "../../utils/anonymous.ts";
import type { CollectionEntry } from "astro:content";
export const KEYWORDS = [
"Portugal",
"democracy",
"test",
"health",
"gym",
"diet",
] as const;
export const KeywordsEnum = z.enum(KEYWORDS);
export const ENTITY_TYPES = ["author", "co-author", "translator"] as const;
export const EntityTypesEnum = z.enum(ENTITY_TYPES);
export const CREATIVE_COMMONS_LICENSES = [
"CC0",
"CC-BY",
"CC-BY-SA",
"CC-BY-ND",
"CC-BY-NC",
"CC-BY-NC-SA",
"CC-BY-NC-ND",
] as const;
export const LICENSES = [
...CREATIVE_COMMONS_LICENSES,
"WTFPL",
"public domain",
] as const;
export const LicensesEnum = z.enum(LICENSES);
export const Original = z.object({
kind: z.literal("original"),
title: z.string().trim(),
subtitle: z.string().trim().optional(),
description: z.string().trim().optional(),
keywords: z.array(KeywordsEnum).refine(
(keywords) => new Set(keywords).size === keywords.length,
{ message: "Keywords must be unique" },
),
dateCreated: z.coerce.date(),
dateUpdated: z.coerce.date().optional(),
locationCreated: z.string().trim().optional(),
relatedPosts: z.array(reference("blog")).default([]).refine(
(posts) => new Set(posts).size === posts.length,
{ message: "Related posts referenced multiple times" },
),
lang: z.string().trim().refine(isValidLocale),
signers: z.array(
z.object({ entity: reference("entity"), role: EntityTypesEnum }),
).default([]).refine(
(signers) => signers.filter((s) => s.role === "author").length <= 1,
{ message: "There can only be one author" },
).refine((signers) => {
const ids = signers.map(get("entity"));
return new Set(ids).size === ids.length;
}, { message: "Reusing signers" }).refine(
(signers) => signers.every(({ role }) => role !== "translator"),
{ message: "There can't be translator signers on non translated work" },
),
license: LicensesEnum.default("public domain"),
});
export const Translation = z.object({
kind: z.literal("translation"),
title: z.string().trim(),
subtitle: z.string().trim().optional(),
description: z.string().trim().optional(),
dateCreated: z.coerce.date(),
dateUpdated: z.coerce.date().optional(),
lang: z.string().trim().refine(isValidLocale),
translationOf: reference("blog"),
signers: z.array(
z.object({ entity: reference("entity"), role: EntityTypesEnum }),
).default([]).refine(
(signers) => signers.filter((s) => s.role === "author").length <= 1,
{ message: "There can only be one author" },
).refine((signers) => {
const ids = signers.map(get("entity"));
return new Set(ids).size === ids.length;
}, { message: "Reusing signers" }),
license: LicensesEnum.default("public domain"),
});
export const Micro = z.object({
kind: z.literal("micro"),
title: z.string().trim(),
keywords: z.array(KeywordsEnum).refine(
(keywords) => new Set(keywords).size === keywords.length,
{ message: "Keywords must be unique" },
),
dateCreated: z.coerce.date(),
dateUpdated: z.coerce.date().optional(),
locationCreated: z.string().trim().optional(),
lang: z.string().trim().refine(isValidLocale),
signers: z.array(
z.object({ entity: reference("entity"), role: EntityTypesEnum }),
).default([]).refine(
(signers) => signers.filter((s) => s.role === "author").length <= 1,
{ message: "There can only be one author" },
).refine((signers) => {
const ids = signers.map(get("entity"));
return new Set(ids).size === ids.length;
}, { message: "Reusing signers" }).refine(
(signers) => signers.every(({ role }) => role !== "translator"),
{ message: "There can't be translator signers on non translated work" },
),
license: LicensesEnum.default("public domain"),
});
export const Blog = z.discriminatedUnion("kind", [
Original,
Translation,
Micro,
]).refine(
({ dateCreated, dateUpdated }) =>
dateUpdated === undefined || dateCreated.getTime() <= dateUpdated.getTime(),
{ message: "Update before creation" },
).refine(
({ signers, license }) =>
!(signers.filter(({ role }) => role === "author").length <= 0 &&
license !== undefined),
{ message: "License cannot be defined if there are no signers." },
);
export type OriginalEntry = CollectionEntry<"blog"> & {
data: z.infer<typeof Original>;
};
export type TranslationEntry = CollectionEntry<"blog"> & {
data: z.infer<typeof Translation>;
};
export type MicroEntry = CollectionEntry<"blog"> & {
data: z.infer<typeof Micro>;
};
export type Entry = OriginalEntry | TranslationEntry | MicroEntry;
export const Entity = z.object({
websites: z.array(z.string().url().trim()).default([]).transform((websites) =>
websites.map((x) => new URL(x).href)
),
publickey: z.object({ armor: z.string().trim() }),
});
|