@tunasand/sdk 0.0.0-stage → 0.6.0

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.
Files changed (168) hide show
  1. package/CHANGELOG.md +106 -0
  2. package/README.md +506 -2
  3. package/dist/analytics.d.ts +1 -0
  4. package/dist/analytics.js +43 -0
  5. package/dist/app.d.ts +1 -0
  6. package/dist/app.js +825 -0
  7. package/dist/apps-9u4XMi7E.js +114 -0
  8. package/dist/body.d.ts +1 -0
  9. package/dist/body.js +455 -0
  10. package/dist/bridge-DYAYGV7t.js +74 -0
  11. package/dist/bridge.d.ts +1 -0
  12. package/dist/bridge.js +189 -0
  13. package/dist/cli.d.ts +1 -0
  14. package/dist/cli.js +283 -0
  15. package/dist/client-Derrexwr.js +159 -0
  16. package/dist/customers-C_vJY2_v.js +84 -0
  17. package/dist/customers.d.ts +1 -0
  18. package/dist/customers.js +185 -0
  19. package/dist/declared-C_GvPiGF.js +397 -0
  20. package/dist/delivery.d.ts +1 -0
  21. package/dist/delivery.js +348 -0
  22. package/dist/dev.d.ts +1 -0
  23. package/dist/dev.js +93 -0
  24. package/dist/embed-R-OvDYAw.js +90 -0
  25. package/dist/errorLabels-BcyRbv_i.js +1510 -0
  26. package/dist/i18n.d.ts +1 -0
  27. package/dist/i18n.js +4 -0
  28. package/dist/integrations.d.ts +1 -0
  29. package/dist/integrations.js +62 -0
  30. package/dist/ja-C1HEWWTQ.js +241 -0
  31. package/dist/ja-C6lX0CeJ.js +213 -0
  32. package/dist/model-pmXzGX12.js +152 -0
  33. package/dist/model.d.ts +1 -0
  34. package/dist/model.js +345 -0
  35. package/dist/page-E_cYgZx-.js +138 -0
  36. package/dist/preview.d.ts +1 -0
  37. package/dist/preview.js +1 -0
  38. package/dist/profile-D6t1QMQh.js +223 -0
  39. package/dist/schema-BPHL7Qr-.js +115 -0
  40. package/dist/schema-UQJWRyfJ.js +995 -0
  41. package/dist/schema.d.ts +1 -0
  42. package/dist/schema.js +1 -0
  43. package/dist/src-sazhzYui.js +71 -0
  44. package/dist/types/access/src/handoff.d.ts +52 -0
  45. package/dist/types/access/src/index.d.ts +5 -0
  46. package/dist/types/access/src/jws.d.ts +5 -0
  47. package/dist/types/access/src/jwt.d.ts +35 -0
  48. package/dist/types/access/src/oidc.d.ts +50 -0
  49. package/dist/types/access/src/signed.d.ts +15 -0
  50. package/dist/types/analytics/src/day.d.ts +2 -0
  51. package/dist/types/analytics/src/index.d.ts +5 -0
  52. package/dist/types/analytics/src/schedule.d.ts +2 -0
  53. package/dist/types/analytics/src/shared.d.ts +21 -0
  54. package/dist/types/analytics/src/source.d.ts +19 -0
  55. package/dist/types/analytics/src/store.d.ts +24 -0
  56. package/dist/types/analytics/src/view.d.ts +33 -0
  57. package/dist/types/body/src/components.d.ts +25 -0
  58. package/dist/types/body/src/declared.d.ts +34 -0
  59. package/dist/types/body/src/derive.d.ts +13 -0
  60. package/dist/types/body/src/description.d.ts +8 -0
  61. package/dist/types/body/src/embed.d.ts +16 -0
  62. package/dist/types/body/src/href.d.ts +4 -0
  63. package/dist/types/body/src/html.d.ts +30 -0
  64. package/dist/types/body/src/index.d.ts +19 -0
  65. package/dist/types/body/src/inline.d.ts +20 -0
  66. package/dist/types/body/src/labels/en.d.ts +90 -0
  67. package/dist/types/body/src/labels/ja.d.ts +153 -0
  68. package/dist/types/body/src/library.d.ts +113 -0
  69. package/dist/types/body/src/markdown.d.ts +14 -0
  70. package/dist/types/body/src/normalize.d.ts +5 -0
  71. package/dist/types/body/src/problems.d.ts +103 -0
  72. package/dist/types/body/src/profile.d.ts +80 -0
  73. package/dist/types/body/src/render.d.ts +29 -0
  74. package/dist/types/body/src/schema.d.ts +6 -0
  75. package/dist/types/body/src/standard.d.ts +20 -0
  76. package/dist/types/body/src/types.d.ts +140 -0
  77. package/dist/types/cms-contract/src/apiKeys.d.ts +41 -0
  78. package/dist/types/cms-contract/src/app.d.ts +35 -0
  79. package/dist/types/cms-contract/src/appDependencies.d.ts +265 -0
  80. package/dist/types/cms-contract/src/appProblems.d.ts +22 -0
  81. package/dist/types/cms-contract/src/apps.d.ts +195 -0
  82. package/dist/types/cms-contract/src/assets.d.ts +12 -0
  83. package/dist/types/cms-contract/src/authorize.d.ts +146 -0
  84. package/dist/types/cms-contract/src/bridge.d.ts +318 -0
  85. package/dist/types/cms-contract/src/consents.d.ts +97 -0
  86. package/dist/types/cms-contract/src/customerLabels.d.ts +33 -0
  87. package/dist/types/cms-contract/src/customers.d.ts +266 -0
  88. package/dist/types/cms-contract/src/embeds.d.ts +165 -0
  89. package/dist/types/cms-contract/src/errorLabels.d.ts +269 -0
  90. package/dist/types/cms-contract/src/errorLabelsEn.d.ts +228 -0
  91. package/dist/types/cms-contract/src/errors.d.ts +105 -0
  92. package/dist/types/cms-contract/src/failureStatus.d.ts +124 -0
  93. package/dist/types/cms-contract/src/functionLabels.d.ts +5 -0
  94. package/dist/types/cms-contract/src/functions.d.ts +144 -0
  95. package/dist/types/cms-contract/src/identifiers.d.ts +117 -0
  96. package/dist/types/cms-contract/src/import.d.ts +277 -0
  97. package/dist/types/cms-contract/src/issueLabels.d.ts +435 -0
  98. package/dist/types/cms-contract/src/issueLabelsEn.d.ts +217 -0
  99. package/dist/types/cms-contract/src/migration.d.ts +103 -0
  100. package/dist/types/cms-contract/src/paths.d.ts +23 -0
  101. package/dist/types/cms-contract/src/permissionLabels.d.ts +8 -0
  102. package/dist/types/cms-contract/src/permissions.d.ts +77 -0
  103. package/dist/types/cms-contract/src/problems.d.ts +34 -0
  104. package/dist/types/cms-contract/src/roles.d.ts +57 -0
  105. package/dist/types/cms-contract/src/settingProblems.d.ts +13 -0
  106. package/dist/types/cms-contract/src/settings.d.ts +19 -0
  107. package/dist/types/cms-contract/src/siteArtifact.d.ts +45 -0
  108. package/dist/types/cms-contract/src/siteDeclaration.d.ts +70 -0
  109. package/dist/types/cms-contract/src/transfer.d.ts +57 -0
  110. package/dist/types/cms-contract/src/views.d.ts +21 -0
  111. package/dist/types/cms-contract/src/webhookLabels.d.ts +7 -0
  112. package/dist/types/cms-contract/src/webhooks.d.ts +139 -0
  113. package/dist/types/i18n/src/index.d.ts +50 -0
  114. package/dist/types/i18n/src/problems.d.ts +21 -0
  115. package/dist/types/model/src/asset.d.ts +111 -0
  116. package/dist/types/model/src/audience.d.ts +38 -0
  117. package/dist/types/model/src/brand.d.ts +25 -0
  118. package/dist/types/model/src/capabilities.d.ts +28 -0
  119. package/dist/types/model/src/changes.d.ts +34 -0
  120. package/dist/types/model/src/check.d.ts +15 -0
  121. package/dist/types/model/src/contact.d.ts +22 -0
  122. package/dist/types/model/src/image.d.ts +38 -0
  123. package/dist/types/model/src/index.d.ts +26 -0
  124. package/dist/types/model/src/input.d.ts +26 -0
  125. package/dist/types/model/src/integrity.d.ts +79 -0
  126. package/dist/types/model/src/json.d.ts +2 -0
  127. package/dist/types/model/src/labels/en.d.ts +169 -0
  128. package/dist/types/model/src/labels/ja.d.ts +198 -0
  129. package/dist/types/model/src/legacy.d.ts +2 -0
  130. package/dist/types/model/src/media.d.ts +5 -0
  131. package/dist/types/model/src/mediaUrl.d.ts +67 -0
  132. package/dist/types/model/src/model.d.ts +415 -0
  133. package/dist/types/model/src/page.d.ts +52 -0
  134. package/dist/types/model/src/problems.d.ts +184 -0
  135. package/dist/types/model/src/refs.d.ts +27 -0
  136. package/dist/types/model/src/rules.d.ts +15 -0
  137. package/dist/types/model/src/schema.d.ts +58 -0
  138. package/dist/types/model/src/screens.d.ts +76 -0
  139. package/dist/types/model/src/selfOps.d.ts +30 -0
  140. package/dist/types/model/src/time.d.ts +25 -0
  141. package/dist/types/model/src/typesSource.d.ts +8 -0
  142. package/dist/types/model/src/values.d.ts +24 -0
  143. package/dist/types/platform/src/defaultModel.d.ts +8 -0
  144. package/dist/types/sdk/src/analytics.d.ts +2 -0
  145. package/dist/types/sdk/src/app.d.ts +20 -0
  146. package/dist/types/sdk/src/body.d.ts +2 -0
  147. package/dist/types/sdk/src/bridge.d.ts +92 -0
  148. package/dist/types/sdk/src/cli.d.ts +2 -0
  149. package/dist/types/sdk/src/customers.d.ts +190 -0
  150. package/dist/types/sdk/src/delivery/cache.d.ts +18 -0
  151. package/dist/types/sdk/src/delivery/changes.d.ts +28 -0
  152. package/dist/types/sdk/src/delivery/client.d.ts +90 -0
  153. package/dist/types/sdk/src/delivery/fields.d.ts +48 -0
  154. package/dist/types/sdk/src/delivery/index.d.ts +9 -0
  155. package/dist/types/sdk/src/delivery/media.d.ts +14 -0
  156. package/dist/types/sdk/src/delivery/query.d.ts +85 -0
  157. package/dist/types/sdk/src/delivery/schema.d.ts +19 -0
  158. package/dist/types/sdk/src/delivery/values.d.ts +82 -0
  159. package/dist/types/sdk/src/delivery.d.ts +3 -0
  160. package/dist/types/sdk/src/dev.d.ts +29 -0
  161. package/dist/types/sdk/src/i18n.d.ts +12 -0
  162. package/dist/types/sdk/src/integrations.d.ts +33 -0
  163. package/dist/types/sdk/src/model.d.ts +5 -0
  164. package/dist/types/sdk/src/preview.d.ts +1 -0
  165. package/dist/types/sdk/src/schema.d.ts +21 -0
  166. package/dist/types/sdk/src/site.d.ts +43 -0
  167. package/dist/typesSource-bO0jFw1I.js +88 -0
  168. package/package.json +86 -3
@@ -0,0 +1,995 @@
1
+ import { i as CORE_COMPONENTS, t as declaredComponentsSchema } from "./declared-C_GvPiGF.js";
2
+ import { f as problemText, l as problemFactory } from "./ja-C1HEWWTQ.js";
3
+ import { n as PRODUCT_FIELD_LABELS, t as MODEL_PROBLEM_MESSAGES } from "./ja-C6lX0CeJ.js";
4
+ import { C as isSurveyShape, S as isSingleton, a as PII_CLASSES, c as acceptsRole, d as cascadeTargetsOf, i as ON_DELETE, l as audienceOf, m as declaredAudienceOf, n as CUSTOMER_FIELD_TYPE, o as SELF_OPS, r as FIELD_ROLES, s as SLUG_MODES, t as AUDIENCES, u as cascadeDepths, v as isListCollection, w as narrower, x as isSequenceField } from "./model-pmXzGX12.js";
5
+ import { z } from "zod";
6
+ // ../model/src/problems.ts
7
+ /** コンテンツスキーマの検査の問題を作る */
8
+ const modelProblem = problemFactory(MODEL_PROBLEM_MESSAGES);
9
+ /** 問題の英語の既定の文(zod の message と検査の issues の message に入れ、problemOf で戻す) */
10
+ const modelProblemText = (code, ...params) => problemText(modelProblem(code, ...params));
11
+ const PHONE_REGION_IDS = Object.keys({
12
+ JP: "81",
13
+ KR: "82",
14
+ TW: "886",
15
+ CN: "86",
16
+ GB: "44"
17
+ });
18
+ /** URL の項目が受け付ける scheme */
19
+ const URL_SCHEMES = ["https", "http"];
20
+ // ../model/src/check.ts
21
+ /** 配列でないオブジェクトか */
22
+ const isRecord$1 = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
23
+ /** 一意の組に含められる項目か(最上位・単数・入力する項目で、値を文字にできる種類) */
24
+ function acceptsUnique(f) {
25
+ if (f.computed || "multiple" in f && f.multiple) return false;
26
+ return [
27
+ "text",
28
+ "email",
29
+ "phone",
30
+ "url",
31
+ "number",
32
+ "select",
33
+ "reference",
34
+ "date",
35
+ "datetime",
36
+ CUSTOMER_FIELD_TYPE
37
+ ].includes(f.type);
38
+ }
39
+ /** コレクションの一意の組(宣言 unique と、unique を付けた顧客フィールドと、連番の項目) */
40
+ const uniqueDefsOf = (collection) => [...collection.unique ?? [], ...collection.fields.flatMap((f) => f.type === "customer" && f.unique || isSequenceField(f) ? [{
41
+ key: f.key,
42
+ fields: [f.key],
43
+ exact: true
44
+ }] : [])];
45
+ /** コレクションの整合の宣言の誤り(スキーマの検査、schema.ts) */
46
+ function integrityProblems(collection) {
47
+ const id = collection.slug;
48
+ const problems = [];
49
+ const defs = uniqueDefsOf(collection);
50
+ if (collection.unique?.length && !isListCollection(collection)) problems.push(modelProblemText("unique_list_only", { collection: id }));
51
+ const names = defs.map((d) => d.key);
52
+ for (const n of new Set(names)) if (names.filter((x) => x === n).length > 1) problems.push(modelProblemText("unique_name_duplicate", {
53
+ collection: id,
54
+ key: n
55
+ }));
56
+ for (const d of collection.unique ?? []) {
57
+ if (!d.fields.length || d.fields.length > 3) problems.push(modelProblemText("unique_field_count", {
58
+ collection: id,
59
+ key: d.key,
60
+ max: 3
61
+ }));
62
+ if (new Set(d.fields).size !== d.fields.length) problems.push(modelProblemText("unique_field_duplicate", {
63
+ collection: id,
64
+ key: d.key
65
+ }));
66
+ for (const key of d.fields) {
67
+ const f = collection.fields.find((x) => x.key === key);
68
+ if (!f) problems.push(modelProblemText("unique_field_not_top", {
69
+ collection: id,
70
+ key: d.key,
71
+ field: key
72
+ }));
73
+ else if (!acceptsUnique(f)) problems.push(modelProblemText("unique_field_unsupported", {
74
+ collection: id,
75
+ key: d.key,
76
+ field: key,
77
+ type: f.type
78
+ }));
79
+ }
80
+ }
81
+ const nested = (fields, path) => fields.flatMap((f) => [...isSequenceField(f) ? [modelProblemText("sequence_nested", {
82
+ collection: id,
83
+ field: `${path}${f.key}`
84
+ })] : [], ...f.type === "group" ? nested(f.fields, `${path}${f.key}.`) : []]);
85
+ for (const f of collection.fields) {
86
+ if (f.type === "group") problems.push(...nested(f.fields, `${f.key}.`));
87
+ if (!isSequenceField(f)) continue;
88
+ if (!isListCollection(collection)) problems.push(modelProblemText("sequence_list_only", {
89
+ collection: id,
90
+ field: f.key
91
+ }));
92
+ if (f.required || f.computed || f.when) problems.push(modelProblemText("sequence_plain", {
93
+ collection: id,
94
+ field: f.key
95
+ }));
96
+ }
97
+ return problems;
98
+ }
99
+ /** 遷移の宣言の誤り(スキーマの検査) */
100
+ function transitionProblems(collection) {
101
+ const id = collection.slug;
102
+ const nested = (fields, path) => fields.flatMap((f) => [...f.type === "select" && f.transitions ? [modelProblemText("transition_nested", {
103
+ collection: id,
104
+ field: `${path}${f.key}`
105
+ })] : [], ...f.type === "group" ? nested(f.fields, `${path}${f.key}.`) : []]);
106
+ return collection.fields.flatMap((f) => {
107
+ if (f.type === "group") return nested(f.fields, `${f.key}.`);
108
+ if (f.type !== "select" || !f.transitions) return [];
109
+ const values = new Set(f.options.map((o) => o.value));
110
+ const problems = [];
111
+ if (f.multiple || f.derive || f.fixedBy) problems.push(modelProblemText("transition_field_kind", {
112
+ collection: id,
113
+ field: f.key
114
+ }));
115
+ for (const [i, t] of f.transitions.entries()) {
116
+ const at = {
117
+ collection: id,
118
+ field: f.key,
119
+ index: i + 1
120
+ };
121
+ if (!values.has(t.to)) problems.push(modelProblemText("transition_to_not_option", {
122
+ ...at,
123
+ to: t.to
124
+ }));
125
+ if (!t.from.length) problems.push(modelProblemText("transition_from_empty", at));
126
+ for (const v of t.from) if (v !== null && !values.has(v)) problems.push(modelProblemText("transition_from_not_option", {
127
+ ...at,
128
+ value: v
129
+ }));
130
+ if (!t.by.roles?.length && !t.by.self && !t.by.apps?.length) problems.push(modelProblemText("transition_no_actor", at));
131
+ }
132
+ return problems;
133
+ });
134
+ }
135
+ // ../model/src/selfOps.ts
136
+ /** 本人の顧客フィールドか(コレクションの最上位の項目) */
137
+ const isSelfField = (f, collection) => f.type === "customer" && (f.self === true || f.self === void 0 && declaredAudienceOf(f) === void 0 && isSurveyShape(collection));
138
+ /** コレクションの本人の顧客フィールド */
139
+ const selfFieldsOf = (collection) => collection.fields.filter((f) => isSelfField(f, collection));
140
+ /** 本人が書ける印を付けられない項目の理由(付けられれば null) */
141
+ function selfWritableBlock(f, collection) {
142
+ if (f.type === "customer") return "customer";
143
+ if (f.type === "group" || f.type === "media" || f.type === "rich_text") return f.type;
144
+ if (audienceOf(f, collection) === "staff") return "staff";
145
+ if (f.adminOnly) return "admin_only";
146
+ if (f.computed) return "computed";
147
+ if (isSequenceField(f)) return "sequence";
148
+ return null;
149
+ }
150
+ /** 人の操作の宣言の誤り(配信しない一覧のコレクション、本人の顧客フィールドがちょうど1つで onDelete は cascade か anonymize、一意の組はすべて本人の顧客フィールドを含む) */
151
+ function selfOpsProblems(collection) {
152
+ if (!collection.selfOps?.length) return [];
153
+ const self = selfFieldsOf(collection);
154
+ const at = { collection: collection.slug };
155
+ const problems = [];
156
+ if (collection.private !== true) problems.push(modelProblemText("self_ops_private_only", at));
157
+ if (isSingleton(collection)) problems.push(modelProblemText("self_ops_list_only", at));
158
+ if ((collection.publishing ?? "none") !== "none" || collection.fields.some((f) => f.key === "published" && f.type === "boolean")) problems.push(modelProblemText("self_ops_unpublished", at));
159
+ if (self.length !== 1) problems.push(modelProblemText("self_ops_one_self", at));
160
+ const key = self.length === 1 ? self[0].key : null;
161
+ if (key && self[0].onDelete !== "cascade" && self[0].onDelete !== "anonymize") problems.push(modelProblemText("self_ops_on_delete", {
162
+ ...at,
163
+ field: key
164
+ }));
165
+ for (const d of uniqueDefsOf(collection)) if (key && !d.fields.includes(key)) problems.push(modelProblemText("self_ops_unique", {
166
+ ...at,
167
+ key: d.key,
168
+ field: key
169
+ }));
170
+ return problems;
171
+ }
172
+ /** 本人が書ける印の誤り(付けられない項目、まとまりの中、create_self か update_self を受けないコレクション) */
173
+ function selfWritableProblems(collection) {
174
+ const nested = (fields, path) => fields.flatMap((f) => [...f.selfWritable !== void 0 ? [modelProblemText("self_writable_nested", {
175
+ collection: collection.slug,
176
+ field: `${path}${f.key}`
177
+ })] : [], ...f.type === "group" ? nested(f.fields, `${path}${f.key}.`) : []]);
178
+ const writes = collection.selfOps?.some((op) => op === "create_self" || op === "update_self") ?? false;
179
+ return collection.fields.flatMap((f) => {
180
+ const inner = f.type === "group" ? nested(f.fields, `${f.key}.`) : [];
181
+ if (!f.selfWritable) return inner;
182
+ const block = selfWritableBlock(f, collection);
183
+ return [
184
+ ...block ? [modelProblemText("self_writable_blocked", {
185
+ collection: collection.slug,
186
+ field: f.key,
187
+ block
188
+ })] : [],
189
+ ...writes ? [] : [modelProblemText("self_writable_without_ops", {
190
+ collection: collection.slug,
191
+ field: f.key
192
+ })],
193
+ ...inner
194
+ ];
195
+ });
196
+ }
197
+ // ../model/src/capabilities.ts
198
+ /** 出典の項目と、内側の項目の key */
199
+ const SOURCES_FIELD = {
200
+ key: "sources",
201
+ source: "source",
202
+ note: "note"
203
+ };
204
+ // ../model/src/legacy.ts
205
+ const isRecord = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
206
+ /** 記事の表のコレクションだった目印(古いバージョンの table の値) */
207
+ const ARTICLES = "articles";
208
+ const PUBLISHED_AT = "published_at";
209
+ /** 本文から作成する値の計算の項目(from は本文の項目の key) */
210
+ const computedFields = (from) => [
211
+ {
212
+ key: "excerpt",
213
+ label: PRODUCT_FIELD_LABELS.excerpt,
214
+ type: "long_text",
215
+ computed: {
216
+ kind: "excerpt",
217
+ from
218
+ }
219
+ },
220
+ {
221
+ key: "read_min",
222
+ label: PRODUCT_FIELD_LABELS.readMin,
223
+ type: "number",
224
+ computed: {
225
+ kind: "read_min",
226
+ from
227
+ }
228
+ },
229
+ {
230
+ key: "links",
231
+ label: PRODUCT_FIELD_LABELS.links,
232
+ type: "text",
233
+ multiple: true,
234
+ computed: {
235
+ kind: "links",
236
+ from
237
+ }
238
+ }
239
+ ];
240
+ /** 出典の参照の項目を複数のまとまりに */
241
+ const sourcesGroup = (f) => ({
242
+ key: f.key,
243
+ label: f.label,
244
+ ...f.private !== void 0 && { private: f.private },
245
+ type: "group",
246
+ multiple: true,
247
+ fields: [{
248
+ key: SOURCES_FIELD.source,
249
+ label: f.label,
250
+ type: "reference",
251
+ to: f.to
252
+ }, {
253
+ key: SOURCES_FIELD.note,
254
+ label: PRODUCT_FIELD_LABELS.sourceNote,
255
+ type: "long_text"
256
+ }]
257
+ });
258
+ /** 古い形のコレクションを今の形に(core・table のないコレクションはそのまま) */
259
+ function normalizeCollection(t) {
260
+ if (!("core" in t) && !("table" in t)) return t;
261
+ const { core, table, ...rest } = t;
262
+ const articles = table === ARTICLES;
263
+ const coreFields = (Array.isArray(core) ? core : []).filter(isRecord).flatMap((f) => {
264
+ if (articles && f.key === PUBLISHED_AT) return [];
265
+ if (articles && f.key === SOURCES_FIELD.key && f.type === "reference") return [sourcesGroup(f)];
266
+ return [f];
267
+ });
268
+ const keys = new Set(coreFields.map((f) => f.key));
269
+ const fields = [...coreFields, ...(Array.isArray(t.fields) ? t.fields : []).filter((f) => !isRecord(f) || !keys.has(f.key))];
270
+ if (!articles) return {
271
+ ...rest,
272
+ fields
273
+ };
274
+ const body = fields.find((f) => isRecord(f) && f.type === "rich_text");
275
+ const present = new Set(fields.filter(isRecord).map((f) => f.key));
276
+ const computed = body ? computedFields(String(body.key)).filter((f) => !present.has(f.key)) : [];
277
+ return {
278
+ ...rest,
279
+ fields: [...fields, ...computed]
280
+ };
281
+ }
282
+ /** 古い形のスキーマの JSON を今の形に(形の分からない値はそのまま、検査は parseModel) */
283
+ function normalizeLegacyModel(json) {
284
+ if (!isRecord(json) || !Array.isArray(json.collections)) return json;
285
+ return {
286
+ ...json,
287
+ collections: json.collections.map((t) => isRecord(t) ? normalizeCollection(t) : t)
288
+ };
289
+ }
290
+ // ../model/src/schema.ts
291
+ /** 保持の日数の上限(100年) */
292
+ const RETENTION_DAYS_MAX = 36500;
293
+ const key = z.string().regex(/^[a-z][a-z0-9_]*$/, modelProblemText("invalid_collection_id"));
294
+ const fieldKey = z.string().regex(/^[a-z][a-zA-Z0-9_-]*$/, modelProblemText("invalid_field_key"));
295
+ const condition = z.object({
296
+ key: fieldKey,
297
+ in: z.array(z.string().min(1)).min(1)
298
+ });
299
+ const computed = z.object({
300
+ kind: z.enum([
301
+ "excerpt",
302
+ "read_min",
303
+ "links"
304
+ ]),
305
+ from: fieldKey
306
+ });
307
+ const base = {
308
+ key: fieldKey,
309
+ label: z.string().min(1),
310
+ description: z.string().optional(),
311
+ required: z.union([
312
+ z.literal("save"),
313
+ z.literal("submit"),
314
+ z.literal("page"),
315
+ z.literal(false)
316
+ ]).optional(),
317
+ when: condition.optional(),
318
+ audience: z.enum(AUDIENCES).optional(),
319
+ adminOnly: z.boolean().optional(),
320
+ selfWritable: z.boolean().optional(),
321
+ pendingUntil: fieldKey.optional(),
322
+ computed: computed.optional(),
323
+ pii: z.enum(PII_CLASSES).optional()
324
+ };
325
+ const frame = z.object({
326
+ when: condition.optional(),
327
+ ratio: z.string().min(1),
328
+ size: z.object({
329
+ width: z.number().int().positive(),
330
+ height: z.number().int().positive()
331
+ }),
332
+ label: z.string().min(1)
333
+ });
334
+ const pageRule = z.union([
335
+ z.object({
336
+ key: z.string().min(1),
337
+ in: z.array(z.string())
338
+ }),
339
+ z.object({
340
+ key: z.string().min(1),
341
+ notIn: z.array(z.string())
342
+ }),
343
+ z.object({
344
+ key: z.string().min(1),
345
+ truthy: z.literal(true)
346
+ }),
347
+ z.object({
348
+ key: z.string().min(1),
349
+ notFuture: z.literal(true)
350
+ })
351
+ ]);
352
+ const page = z.object({
353
+ when: condition.optional(),
354
+ path: z.string().startsWith("/", modelProblemText("page_path_slash")),
355
+ public: z.array(pageRule).optional()
356
+ });
357
+ const field = z.lazy(() => fieldUnion);
358
+ /** 項目の宣言の形(アプリのマニフェストのコレクションの項目の検査に使う) */
359
+ const fieldDefSchema = field;
360
+ const transition = z.object({
361
+ from: z.array(z.string().min(1).nullable()),
362
+ to: z.string().min(1),
363
+ by: z.object({
364
+ roles: z.array(z.string().min(1)).optional(),
365
+ self: z.boolean().optional(),
366
+ apps: z.array(z.string().min(1)).optional()
367
+ })
368
+ });
369
+ const counts = {
370
+ minItems: z.number().int().nonnegative().optional(),
371
+ maxItems: z.number().int().positive().optional()
372
+ };
373
+ const regex = z.string().refine((p) => {
374
+ try {
375
+ new RegExp(p, "u");
376
+ return true;
377
+ } catch {
378
+ return false;
379
+ }
380
+ }, modelProblemText("invalid_pattern"));
381
+ const anonymize = z.array(fieldKey).min(1).optional();
382
+ const fieldUnion = z.discriminatedUnion("type", [
383
+ z.object({
384
+ ...base,
385
+ ...counts,
386
+ type: z.enum(["text", "long_text"]),
387
+ externalId: z.boolean().optional(),
388
+ maxLength: z.number().int().positive().optional(),
389
+ minLength: z.number().int().positive().optional(),
390
+ pattern: regex.optional(),
391
+ guideLength: z.number().int().positive().optional(),
392
+ multiple: z.boolean().optional()
393
+ }),
394
+ z.object({
395
+ ...base,
396
+ type: z.literal("number"),
397
+ min: z.number().optional(),
398
+ max: z.number().optional(),
399
+ sequence: z.boolean().optional()
400
+ }),
401
+ z.object({
402
+ ...base,
403
+ type: z.literal("boolean")
404
+ }),
405
+ z.object({
406
+ ...base,
407
+ type: z.literal("email")
408
+ }),
409
+ z.object({
410
+ ...base,
411
+ type: z.literal("phone"),
412
+ region: z.enum(PHONE_REGION_IDS).optional()
413
+ }),
414
+ z.object({
415
+ ...base,
416
+ type: z.literal("url"),
417
+ schemes: z.array(z.enum(URL_SCHEMES)).min(1).optional()
418
+ }),
419
+ z.object({
420
+ ...base,
421
+ type: z.enum(["date", "datetime"])
422
+ }),
423
+ z.object({
424
+ ...base,
425
+ ...counts,
426
+ type: z.literal("select"),
427
+ options: z.array(z.object({
428
+ value: z.string().min(1),
429
+ label: z.string().min(1),
430
+ tone: z.enum([
431
+ "positive",
432
+ "caution",
433
+ "neutral"
434
+ ]).optional(),
435
+ restricted: z.boolean().optional()
436
+ })).min(1),
437
+ initial: z.string().optional(),
438
+ multiple: z.boolean().optional(),
439
+ derive: z.object({
440
+ from: fieldKey,
441
+ map: z.record(z.string(), z.string()),
442
+ otherwise: z.string()
443
+ }).optional(),
444
+ fixedBy: z.object({
445
+ ref: fieldKey,
446
+ key: fieldKey,
447
+ map: z.record(z.string(), z.string())
448
+ }).optional(),
449
+ transitions: z.array(transition).optional()
450
+ }).refine((f) => f.initial === void 0 || f.options.some((o) => o.value === f.initial), {
451
+ message: modelProblemText("initial_not_option"),
452
+ path: ["initial"]
453
+ }).refine((f) => !f.derive || [...Object.values(f.derive.map), f.derive.otherwise].every((v) => f.options.some((o) => o.value === v)), {
454
+ message: modelProblemText("derive_not_option"),
455
+ path: ["derive"]
456
+ }).refine((f) => !f.fixedBy || Object.values(f.fixedBy.map).every((v) => f.options.some((o) => o.value === v)), {
457
+ message: modelProblemText("fixed_not_option"),
458
+ path: ["fixedBy"]
459
+ }),
460
+ z.object({
461
+ ...base,
462
+ ...counts,
463
+ type: z.literal("reference"),
464
+ to: z.string().min(1),
465
+ multiple: z.boolean().optional(),
466
+ where: z.record(z.string(), z.string()).optional(),
467
+ requests: z.boolean().optional(),
468
+ emptyLabel: z.string().min(1).optional(),
469
+ unique: z.boolean().optional(),
470
+ published: z.boolean().optional(),
471
+ linkedBy: fieldKey.optional(),
472
+ onDelete: z.enum(ON_DELETE).optional(),
473
+ anonymize
474
+ }),
475
+ z.object({
476
+ ...base,
477
+ ...counts,
478
+ type: z.literal("media"),
479
+ accept: z.array(z.enum(["image", "video"])).optional(),
480
+ multiple: z.boolean().optional(),
481
+ frames: z.array(frame).optional(),
482
+ visibility: z.literal("private").optional()
483
+ }),
484
+ z.object({
485
+ ...base,
486
+ type: z.literal("rich_text"),
487
+ visibility: z.literal("private").optional(),
488
+ profile: z.object({
489
+ headings: z.array(z.number()),
490
+ marks: z.array(z.string()),
491
+ components: z.array(z.string())
492
+ }).passthrough()
493
+ }),
494
+ z.object({
495
+ ...base,
496
+ ...counts,
497
+ type: z.literal("group"),
498
+ fields: z.array(field),
499
+ multiple: z.boolean().optional()
500
+ }),
501
+ z.object({
502
+ ...base,
503
+ type: z.literal("customer"),
504
+ unique: z.boolean().optional(),
505
+ self: z.boolean().optional(),
506
+ onDelete: z.enum(ON_DELETE).optional(),
507
+ anonymize
508
+ }).refine((f) => f.onDelete !== "restrict", {
509
+ message: modelProblemText("customer_restrict"),
510
+ path: ["onDelete"]
511
+ })
512
+ ]);
513
+ /** 条件が一覧にある選択肢の項目と値を指すか */
514
+ const conditionValid = (when, fields) => {
515
+ const target = when && fields.find((g) => g.key === when.key && g.type === "select");
516
+ return !when || target !== void 0 && when.in.every((v) => target.options.some((o) => o.value === v));
517
+ };
518
+ /** 条件が同じ階層の選択肢の項目と値を指すか(まとまりの中も) */
519
+ const conditionsValid = (fields) => fields.every((f) => conditionValid(f.when, fields) && (!f.derive || fields.some((g) => g.key === f.derive.from && g.type === "select")) && (!f.fixedBy || fields.some((g) => g.key === f.fixedBy.ref && g.type === "reference" && !g.multiple)) && (f.frames ?? []).every((fr) => conditionValid(fr.when, fields)) && conditionsValid(f.fields ?? []));
520
+ /** まとまりの中で決まる値・参照先で固定を持つ項目の key(宣言の適用 rules.ts が最上位の項目だけのため) */
521
+ const nestedRules = (fields) => fields.flatMap((f) => (f.fields ?? []).flatMap((c) => [...c.derive || c.fixedBy ? [`${f.key}.${c.key}`] : [], ...nestedRules([c]).map((k) => `${f.key}.${k}`)]));
522
+ /**
523
+ * システムの値と同じ名前で、項目の key に使用できない名前(API・配信が項目の値を上書きするため)
524
+ * - すべてのコレクション: レコードの ID とスラッグ・作成と更新の日時・作成者、管理画面の一覧で参照する行の数 uses
525
+ * - 公開の管理があるコレクション: 公開の機能の値
526
+ * - 対象外: API・配信の値に出ない列 type、導入先の設定で決まるアカウントの email・account_role
527
+ */
528
+ const RESERVED = [
529
+ "id",
530
+ "slug",
531
+ "created_at",
532
+ "created_by",
533
+ "updated_at",
534
+ "uses"
535
+ ];
536
+ const RESERVED_EDITORIAL = [
537
+ "status",
538
+ "published_at",
539
+ "visibility",
540
+ "version",
541
+ "review_note",
542
+ "assignee"
543
+ ];
544
+ /** 配信 API の絞り込みの論理の演算(filter[or][0][key] など) */
545
+ const RESERVED_QUERY = [
546
+ "or",
547
+ "and",
548
+ "not"
549
+ ];
550
+ /** 項目の key に使えない名前の全部(公開の管理の有無によらない。アプリのマニフェストの検査) */
551
+ const RESERVED_FIELD_KEYS = [
552
+ ...RESERVED,
553
+ ...RESERVED_EDITORIAL,
554
+ ...RESERVED_QUERY
555
+ ];
556
+ /** 予約の名前と同じ最上位の key */
557
+ function reservedKeysOf(t) {
558
+ const reserved = [...RESERVED, ...(t.publishing ?? "none") !== "none" ? RESERVED_EDITORIAL : []];
559
+ return [...t.fields.map((f) => f.key), ...(t.counts ?? []).map((c) => c.key)].filter((k) => reserved.includes(k));
560
+ }
561
+ /** 計算の種類ごとに受け付ける項目の種類 */
562
+ const COMPUTED_TYPES = {
563
+ excerpt: (f) => (f.type === "text" || f.type === "long_text") && !f.multiple,
564
+ read_min: (f) => f.type === "number",
565
+ links: (f) => f.type === "text" && Boolean(f.multiple)
566
+ };
567
+ /** 計算の項目の誤り(最上位の項目と、まとまりの中にある計算の項目) */
568
+ function computedProblems(collection, fields) {
569
+ const nested = (fs, path) => fs.flatMap((f) => [...f.computed ? [modelProblemText("computed_nested", {
570
+ collection,
571
+ field: `${path}${f.key}`
572
+ })] : [], ...nested(f.fields ?? [], `${path}${f.key}.`)]);
573
+ return fields.flatMap((f) => {
574
+ const inner = nested(f.fields ?? [], `${f.key}.`);
575
+ if (!f.computed) return inner;
576
+ return [...[...fields.find((g) => g.key === f.computed.from)?.type === "rich_text" ? [] : [modelProblemText("computed_from_not_body", {
577
+ collection,
578
+ field: f.key,
579
+ from: f.computed.from
580
+ })], ...COMPUTED_TYPES[f.computed.kind]?.(f) ? [] : [modelProblemText("computed_type_mismatch", {
581
+ collection,
582
+ field: f.key,
583
+ kind: f.computed.kind,
584
+ type: f.type
585
+ })]], ...inner];
586
+ });
587
+ }
588
+ /** 役割の宣言の誤り(最上位にない key・役割が受け付けない項目) */
589
+ const roleProblems = (collection, roles, fields) => Object.entries(roles).flatMap(([role, key]) => {
590
+ const f = fields.find((g) => g.key === key);
591
+ if (!f) return [modelProblemText("role_field_not_top", {
592
+ collection,
593
+ role,
594
+ field: String(key)
595
+ })];
596
+ return acceptsRole(role, f) ? [] : [modelProblemText("role_field_mismatch", {
597
+ collection,
598
+ role,
599
+ field: f.key,
600
+ type: f.type
601
+ })];
602
+ });
603
+ /** 見える範囲の宣言の誤り(顧客フィールドは本人かスタッフ、本人の項目は本人の印を持つコレクション、まとまりの中は外側と同じかスタッフ、要る権利は配信する参照) */
604
+ function audienceProblems(collection, fields, grant) {
605
+ const hasSelf = fields.some((f) => f.type === "customer" && f.self);
606
+ const nested = (fs, path, outer) => fs.flatMap((f) => {
607
+ const own = f.audience ?? outer;
608
+ return [...own !== outer && own !== "staff" ? [modelProblemText("audience_nested_wider", {
609
+ collection,
610
+ field: `${path}${f.key}`
611
+ })] : [], ...nested(f.fields ?? [], `${path}${f.key}.`, own)];
612
+ });
613
+ const own = fields.flatMap((f) => [...f.type === "customer" && f.audience !== "self" && f.audience !== "staff" ? [modelProblemText("audience_customer_field", {
614
+ collection,
615
+ field: f.key
616
+ })] : [], ...nested(f.fields ?? [], `${f.key}.`, f.audience ?? "public")]);
617
+ const anySelf = (fs) => fs.some((f) => f.audience === "self" || anySelf(f.fields ?? []));
618
+ const self = !hasSelf && anySelf(fields) ? [modelProblemText("audience_self_without_marker", { collection })] : [];
619
+ if (grant === void 0) return [...own, ...self];
620
+ const ref = fields.find((f) => f.key === grant);
621
+ return [
622
+ ...own,
623
+ ...self,
624
+ ...ref?.type === "reference" && (ref.audience === "public" || ref.audience === "granted") ? [] : [modelProblemText("grant_not_reference", {
625
+ collection,
626
+ grant
627
+ })]
628
+ ];
629
+ }
630
+ /** 元の見える範囲 source が作る値の見える範囲 target より狭いか(読み替えたコレクション、宣言なしは公開) */
631
+ const narrowerThan = (source, target) => {
632
+ const a = source ?? "public";
633
+ const b = target ?? "public";
634
+ return a !== b && narrower(a, b) === a;
635
+ };
636
+ /** 雛形の {key} の key */
637
+ const placeholdersOf = (template) => [...template.matchAll(/\{([A-Za-z0-9_-]+)\}/g)].map((m) => m[1]);
638
+ /**
639
+ * 値から作る値の見える範囲の誤り(読み替えたコレクション、作る値は元より広くできない)
640
+ * - 計算の項目 computed の元、決まる値 derive の元、条件 when の元(条件に合わない値は保存で空になる): 作る項目より狭い元は不可
641
+ * - ページの雛形 pages.path と OG の雛形 og の {key}: 最上位の公開の項目(slug と被参照の件数は可)。{id} は不可(UUID の URL になる)
642
+ */
643
+ function derivedAudienceProblems(t) {
644
+ const byKey = new Map(t.fields.map((f) => [f.key, f]));
645
+ const narrowerSource = (f, from, source) => narrowerThan(byKey.get(from)?.audience, f.audience) && byKey.has(from) ? [modelProblemText("audience_wider_than_source", {
646
+ collection: t.slug,
647
+ field: f.key,
648
+ source,
649
+ from
650
+ })] : [];
651
+ const own = t.fields.flatMap((f) => [
652
+ ...f.computed ? narrowerSource(f, f.computed.from, "computed") : [],
653
+ ...f.derive ? narrowerSource(f, f.derive.from, "derive") : [],
654
+ ...f.when ? narrowerSource(f, f.when.key, "when") : []
655
+ ]);
656
+ const allowed = /* @__PURE__ */ new Set(["slug", ...(t.counts ?? []).map((c) => c.key)]);
657
+ const templates = [...(t.pages ?? []).map((p) => p.path), ...t.og ? [t.og] : []];
658
+ const ids = templates.filter((tpl) => placeholdersOf(tpl).includes("id")).map((tpl) => modelProblemText("template_id_placeholder", {
659
+ collection: t.slug,
660
+ template: tpl
661
+ }));
662
+ const paths = templates.flatMap((tpl) => placeholdersOf(tpl).filter((k) => k !== "id" && !allowed.has(k) && (byKey.get(k)?.audience ?? "public") !== "public").map((k) => modelProblemText("template_private_placeholder", {
663
+ collection: t.slug,
664
+ template: tpl,
665
+ key: k
666
+ })));
667
+ return [
668
+ ...own,
669
+ ...ids,
670
+ ...paths
671
+ ];
672
+ }
673
+ /** 参照先で固定 fixedBy と被参照の件数 counts の見える範囲の誤り(固定の元は固定する項目より狭くできない、件数は元のコレクションの公開の参照だけ) */
674
+ function crossCollectionAudienceProblems(collections) {
675
+ const fixed = collections.flatMap((t) => t.fields.flatMap((f) => {
676
+ if (!f.fixedBy) return [];
677
+ const ref = t.fields.find((g) => g.key === f.fixedBy.ref);
678
+ return [ref, ref?.to ? collections.find((x) => x.id === ref.to)?.fields.find((g) => g.key === f.fixedBy.key) : void 0].some((src) => src && narrowerThan(src.audience, f.audience)) ? [modelProblemText("fixed_by_wider", {
679
+ collection: t.slug,
680
+ field: f.key
681
+ })] : [];
682
+ }));
683
+ const counts = collections.flatMap((t) => (t.counts ?? []).flatMap((c) => {
684
+ const ref = collections.find((x) => x.id === c.from)?.fields.find((g) => g.key === c.field);
685
+ return ref && (ref.audience ?? "public") !== "public" ? [modelProblemText("count_reference_private", {
686
+ collection: t.slug,
687
+ key: c.key,
688
+ fromCollection: collections.find((x) => x.id === c.from)?.slug ?? c.from,
689
+ field: c.field
690
+ })] : [];
691
+ }));
692
+ return [...fixed, ...counts];
693
+ }
694
+ /** 顧客フィールドの誤り(まとまりの中に置かない) */
695
+ function customerFieldProblems(collection, fields) {
696
+ const nested = (fs, path) => fs.flatMap((f) => [...f.type === "customer" ? [modelProblemText("customer_field_nested", {
697
+ collection,
698
+ field: `${path}${f.key}`
699
+ })] : [], ...nested(f.fields ?? [], `${path}${f.key}.`)]);
700
+ return fields.flatMap((f) => nested(f.fields ?? [], `${f.key}.`));
701
+ }
702
+ /** anonymize で空にできない項目の種類(本文・まとまり・結び付き) */
703
+ const UNCLEARABLE = /* @__PURE__ */ new Set([
704
+ "rich_text",
705
+ "group",
706
+ "customer",
707
+ "reference"
708
+ ]);
709
+ const isRequired = (f) => f.required !== void 0 && f.required !== false;
710
+ /** 指す先が消えたときの扱いのコレクションの中の誤り(必須の項目の clear・anonymize、anonymize の項目、まとまりの中の宣言) */
711
+ function onDeleteProblems(collection, fields) {
712
+ const nested = (fs, path) => fs.flatMap((f) => [...f.onDelete !== void 0 || f.anonymize ? [modelProblemText("on_delete_nested", {
713
+ collection,
714
+ field: `${path}${f.key}`
715
+ })] : [], ...nested(f.fields ?? [], `${path}${f.key}.`)]);
716
+ return fields.flatMap((f) => {
717
+ const inner = nested(f.fields ?? [], `${f.key}.`);
718
+ if (f.type !== "customer" && f.type !== "reference") return inner;
719
+ return [...[
720
+ ...isRequired(f) && (f.onDelete === "clear" || f.onDelete === "anonymize") ? [modelProblemText("on_delete_required", {
721
+ collection,
722
+ field: f.key,
723
+ onDelete: f.onDelete
724
+ })] : [],
725
+ ...(f.minItems ?? 0) > 0 && (f.onDelete === "clear" || f.onDelete === "anonymize") ? [modelProblemText("on_delete_min_items", {
726
+ collection,
727
+ field: f.key,
728
+ onDelete: f.onDelete
729
+ })] : [],
730
+ ...f.anonymize && f.onDelete !== "anonymize" ? [modelProblemText("anonymize_without_on_delete", {
731
+ collection,
732
+ field: f.key
733
+ })] : [],
734
+ ...(f.anonymize ?? []).flatMap((k) => {
735
+ const g = fields.find((x) => x.key === k);
736
+ if (!g) return [modelProblemText("anonymize_field_missing", {
737
+ collection,
738
+ field: f.key,
739
+ target: k
740
+ })];
741
+ if (UNCLEARABLE.has(g.type) || g.computed) return [modelProblemText("anonymize_field_unclearable", {
742
+ collection,
743
+ field: f.key,
744
+ target: k,
745
+ type: g.type
746
+ })];
747
+ return isRequired(g) ? [modelProblemText("anonymize_field_required", {
748
+ collection,
749
+ field: f.key,
750
+ target: k
751
+ })] : [];
752
+ })
753
+ ], ...inner];
754
+ });
755
+ }
756
+ /** cascade の循環、人からの深さ、参照の cascade でたどるコレクションの本人の顧客フィールドの誤り */
757
+ function cascadeProblems(collections) {
758
+ const holders = (to) => collections.filter((collection) => cascadeTargetsOf(collection).includes(to));
759
+ const cycleFrom = (start) => {
760
+ const walk = (id, path) => {
761
+ for (const h of holders(id)) {
762
+ if (h.id === start) return [...path, h.id];
763
+ if (path.includes(h.id)) continue;
764
+ const found = walk(h.id, [...path, h.id]);
765
+ if (found) return found;
766
+ }
767
+ return null;
768
+ };
769
+ return walk(start, [start]);
770
+ };
771
+ const cyclic = /* @__PURE__ */ new Set();
772
+ const problems = [];
773
+ for (const collection of collections) {
774
+ const found = cyclic.has(collection.id) ? null : cycleFrom(collection.id);
775
+ if (!found) continue;
776
+ found.forEach((id) => cyclic.add(id));
777
+ problems.push(modelProblemText("cascade_cycle", { cycle: found.map((id) => collections.find((c) => c.id === id)?.slug ?? id).join(" → ") }));
778
+ }
779
+ if (problems.length) return problems;
780
+ const depth = cascadeDepths(collections);
781
+ const tooDeep = [...depth].filter(([, d]) => d > 4).map(([id, d]) => modelProblemText("cascade_too_deep", {
782
+ collection: collections.find((c) => c.id === id)?.slug ?? id,
783
+ depth: d,
784
+ max: 4
785
+ }));
786
+ const self = collections.filter((collection) => cascadeTargetsOf(collection).some((to) => depth.has(to))).flatMap((collection) => collection.fields.filter((f) => f.type === "customer" && f.self).map((f) => modelProblemText("cascade_self_customer", {
787
+ collection: collection.slug,
788
+ field: f.key
789
+ })));
790
+ return [...tooDeep, ...self];
791
+ }
792
+ /** 人から cascade でたどれるコレクションを restrict で指す参照の項目の誤り(undeclared は宣言のない参照も拒む) */
793
+ function restrictProblems(collections, undeclared) {
794
+ const reachable = cascadeDepths(collections);
795
+ const walk = (collectionId, fields, path) => fields.flatMap((f) => {
796
+ if (f.type === "group") return walk(collectionId, f.fields ?? [], `${path}${f.key}.`);
797
+ if (f.type !== "reference" || !f.to || !reachable.has(f.to)) return [];
798
+ if (f.onDelete === "restrict") return [modelProblemText("restrict_reachable", {
799
+ collection: collectionId,
800
+ field: `${path}${f.key}`,
801
+ to: f.to
802
+ })];
803
+ if (undeclared && f.onDelete === void 0) return [modelProblemText("on_delete_undeclared_reference", {
804
+ collection: collectionId,
805
+ field: `${path}${f.key}`,
806
+ to: f.to,
807
+ nested: path ? "yes" : "no"
808
+ })];
809
+ return [];
810
+ });
811
+ return collections.flatMap((t) => walk(t.id, t.fields, ""));
812
+ }
813
+ /** 非公開のコレクションの宣言の誤り(配信に関わる宣言と同時に付けない) */
814
+ function privateProblems(t) {
815
+ const collection = t.slug;
816
+ return [
817
+ ...t.format === "object" ? [modelProblemText("private_object", { collection })] : [],
818
+ ...(t.publishing ?? "none") !== "none" ? [modelProblemText("private_publishing", { collection })] : [],
819
+ ...t.pages?.length ? [modelProblemText("private_pages", { collection })] : [],
820
+ ...t.grant !== void 0 ? [modelProblemText("private_grant", { collection })] : []
821
+ ];
822
+ }
823
+ /** 本文の項目が使用する部品の名前(まとまりの中も、key は項目の key) */
824
+ const bodyComponents = (fields) => fields.flatMap((f) => f.type === "rich_text" ? f.profile.components.map((name) => ({
825
+ key: f.key,
826
+ name
827
+ })) : bodyComponents(f.fields ?? []));
828
+ /** 参照の項目の参照先(まとまりの中も) */
829
+ const referenceTargets = (fields) => fields.flatMap((f) => f.type === "reference" ? [f.to] : referenceTargets(f.fields ?? []));
830
+ const contentType = z.object({
831
+ id: z.string().min(1),
832
+ slug: key,
833
+ label: z.string().min(1),
834
+ description: z.string().optional(),
835
+ format: z.enum(["list", "object"]).optional(),
836
+ publishing: z.enum([
837
+ "none",
838
+ "draft",
839
+ "review"
840
+ ]).optional(),
841
+ fields: z.array(field),
842
+ pages: z.array(page).optional(),
843
+ counts: z.array(z.object({
844
+ key: fieldKey,
845
+ from: z.string().min(1),
846
+ field: fieldKey
847
+ })).optional(),
848
+ og: z.string().startsWith("/", modelProblemText("og_path_slash")).optional(),
849
+ slugMode: z.enum(SLUG_MODES).optional(),
850
+ roles: z.partialRecord(z.enum(FIELD_ROLES), fieldKey).optional(),
851
+ grant: fieldKey.optional(),
852
+ entitlement: z.never({ message: modelProblemText("entitlement_renamed") }).optional(),
853
+ private: z.boolean().optional(),
854
+ unique: z.array(z.object({
855
+ key: fieldKey,
856
+ fields: z.array(fieldKey),
857
+ exact: z.boolean().optional()
858
+ })).optional(),
859
+ selfOps: z.array(z.enum(SELF_OPS)).optional(),
860
+ retentionDays: z.number().int().positive().max(RETENTION_DAYS_MAX).optional()
861
+ }).refine((t) => new Set(t.fields.map((f) => f.key)).size === t.fields.length, { message: modelProblemText("duplicate_field_key") }).refine((t) => conditionsValid(t.fields), { message: modelProblemText("condition_invalid") }).refine((t) => (t.pages ?? []).every((p) => conditionValid(p.when, t.fields)), { message: modelProblemText("page_condition_invalid") }).superRefine((t, ctx) => {
862
+ const fields = t.fields;
863
+ const asCollection = t;
864
+ for (const k of reservedKeysOf(t)) ctx.addIssue({
865
+ code: "custom",
866
+ message: modelProblemText("reserved_key", {
867
+ collection: t.slug,
868
+ key: k
869
+ })
870
+ });
871
+ for (const f of fields) if (RESERVED_QUERY.includes(f.key)) ctx.addIssue({
872
+ code: "custom",
873
+ message: modelProblemText("reserved_query_key", {
874
+ collection: t.slug,
875
+ key: f.key
876
+ })
877
+ });
878
+ for (const k of nestedRules(fields)) ctx.addIssue({
879
+ code: "custom",
880
+ message: modelProblemText("nested_rule", {
881
+ collection: t.slug,
882
+ field: k
883
+ })
884
+ });
885
+ for (const m of roleProblems(t.slug, t.roles ?? {}, t.fields)) ctx.addIssue({
886
+ code: "custom",
887
+ message: m,
888
+ path: ["roles"]
889
+ });
890
+ const problems = [
891
+ ...computedProblems(t.slug, fields),
892
+ ...audienceProblems(t.slug, fields, t.grant),
893
+ ...derivedAudienceProblems({
894
+ ...t,
895
+ fields
896
+ }),
897
+ ...customerFieldProblems(t.slug, fields),
898
+ ...integrityProblems(asCollection),
899
+ ...transitionProblems(asCollection),
900
+ ...onDeleteProblems(t.slug, fields),
901
+ ...selfOpsProblems(asCollection),
902
+ ...selfWritableProblems(asCollection)
903
+ ];
904
+ for (const m of problems) ctx.addIssue({
905
+ code: "custom",
906
+ message: m
907
+ });
908
+ if (t.private) for (const m of privateProblems(t)) ctx.addIssue({
909
+ code: "custom",
910
+ message: m,
911
+ path: ["private"]
912
+ });
913
+ });
914
+ z.object({
915
+ components: declaredComponentsSchema.optional(),
916
+ collections: z.array(contentType).refine((ts) => new Set(ts.map((t) => t.id)).size === ts.length, { message: modelProblemText("duplicate_collection_id") }).refine((ts) => new Set(ts.map((t) => t.slug)).size === ts.length, { message: modelProblemText("duplicate_collection_slug") }).superRefine((ts, ctx) => {
917
+ const ids = new Set(ts.map((t) => t.id));
918
+ for (const t of ts) for (const to of referenceTargets(t.fields)) if (!ids.has(to)) ctx.addIssue({
919
+ code: "custom",
920
+ message: modelProblemText("reference_target_missing", {
921
+ collection: t.slug,
922
+ to
923
+ })
924
+ });
925
+ for (const t of ts) {
926
+ const keys = new Set(t.fields.map((f) => f.key));
927
+ for (const c of t.counts ?? []) {
928
+ const from = ts.find((x) => x.id === c.from);
929
+ const ref = from && from.fields.find((f) => f.key === c.field);
930
+ if (ref?.type !== "reference" || ref.to !== t.id) ctx.addIssue({
931
+ code: "custom",
932
+ message: modelProblemText("count_reference_mismatch", {
933
+ collection: t.slug,
934
+ key: c.key,
935
+ fromCollection: from?.slug ?? c.from,
936
+ field: c.field
937
+ })
938
+ });
939
+ if (keys.has(c.key)) ctx.addIssue({
940
+ code: "custom",
941
+ message: modelProblemText("count_key_duplicate", {
942
+ collection: t.slug,
943
+ key: c.key
944
+ })
945
+ });
946
+ }
947
+ }
948
+ const parsed = ts.map((t) => ({
949
+ id: t.id,
950
+ slug: t.slug,
951
+ fields: t.fields
952
+ }));
953
+ for (const m of crossCollectionAudienceProblems(ts.map((t) => ({
954
+ id: t.id,
955
+ slug: t.slug,
956
+ fields: t.fields,
957
+ counts: t.counts
958
+ })))) ctx.addIssue({
959
+ code: "custom",
960
+ message: m
961
+ });
962
+ const cascade = cascadeProblems(parsed);
963
+ for (const m of cascade) ctx.addIssue({
964
+ code: "custom",
965
+ message: m
966
+ });
967
+ if (!cascade.length) for (const m of restrictProblems(parsed, false)) ctx.addIssue({
968
+ code: "custom",
969
+ message: m
970
+ });
971
+ })
972
+ }).superRefine((m, ctx) => {
973
+ const known = /* @__PURE__ */ new Set([...CORE_COMPONENTS.map((c) => c.name), ...(m.components ?? []).map((c) => c.name)]);
974
+ for (const t of m.collections) for (const { key, name } of bodyComponents(t.fields)) if (!known.has(name)) ctx.addIssue({
975
+ code: "custom",
976
+ message: modelProblemText("component_unknown", {
977
+ collection: t.slug,
978
+ field: key,
979
+ name
980
+ })
981
+ });
982
+ });
983
+ /** コレクションの一覧として読めるか(コレクションごとに id・スラッグと項目の配列) */
984
+ const readable = (json) => isRecord$1(json) && Array.isArray(json.collections) && json.collections.every((t) => isRecord$1(t) && typeof t.id === "string" && typeof t.slug === "string" && Array.isArray(t.fields) && t.fields.every(isRecord$1));
985
+ /**
986
+ * 保存した版とファイルの読み込み(寛容: 旧い語を今の名前にした値をそのまま返し、規則で拒まない、既定を書き込まない)
987
+ * - コレクションの一覧として読めない値だけ例外(壊れた版)
988
+ * - 見える範囲などの読み替えは audienceOf と resolvedModel(保存の形には書き戻さない)
989
+ */
990
+ function parseModel(json) {
991
+ const normalized = normalizeLegacyModel(json);
992
+ if (!readable(normalized)) throw new Error("model_unreadable");
993
+ return normalized;
994
+ }
995
+ export { modelProblemText as i, fieldDefSchema as n, parseModel as r, RESERVED_FIELD_KEYS as t };