@fleetless/contracts 13.3.0 → 15.0.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 (182) hide show
  1. package/CHANGELOG.md +304 -0
  2. package/artifacts/openapi.json +20156 -10513
  3. package/artifacts/routes.json +1429 -292
  4. package/artifacts/schema/access-warning.schema.json +51 -0
  5. package/artifacts/schema/action-config.schema.json +35 -0
  6. package/artifacts/schema/addon-catalogue-entry.schema.json +3 -1
  7. package/artifacts/schema/admin-plan-change-request.schema.json +26 -0
  8. package/artifacts/schema/app-access-response.schema.json +136 -0
  9. package/artifacts/schema/app-access.schema.json +80 -0
  10. package/artifacts/schema/app-deletion-summary.schema.json +15 -8
  11. package/artifacts/schema/app-invitation-list-response.schema.json +13 -6
  12. package/artifacts/schema/app-invitation.schema.json +5 -5
  13. package/artifacts/schema/app-list-response.schema.json +6 -6
  14. package/artifacts/schema/app-lock.schema.json +30 -0
  15. package/artifacts/schema/app-user-list-response.schema.json +13 -6
  16. package/artifacts/schema/app-user.schema.json +13 -6
  17. package/artifacts/schema/app.schema.json +6 -6
  18. package/artifacts/schema/asset-plan-limit-details.schema.json +3 -1
  19. package/artifacts/schema/auth-ok.schema.json +20 -6
  20. package/artifacts/schema/camera-list-response.schema.json +1 -1
  21. package/artifacts/schema/client-identity.schema.json +21 -6
  22. package/artifacts/schema/cloud-config.schema.json +359 -5
  23. package/artifacts/schema/collection-actor.schema.json +35 -0
  24. package/artifacts/schema/collection-definitions-response.schema.json +612 -0
  25. package/artifacts/schema/collection-event.schema.json +216 -0
  26. package/artifacts/schema/collection-export-query.schema.json +56 -0
  27. package/artifacts/schema/collection-filter.schema.json +9 -0
  28. package/artifacts/schema/collection-import-preview.schema.json +143 -0
  29. package/artifacts/schema/collection-import-request.schema.json +105 -0
  30. package/artifacts/schema/collection-import-result.schema.json +38 -0
  31. package/artifacts/schema/collection-list-response.schema.json +119 -0
  32. package/artifacts/schema/collection-record-get-query.schema.json +12 -0
  33. package/artifacts/schema/collection-record-history-query.schema.json +35 -0
  34. package/artifacts/schema/collection-record-history-response.schema.json +389 -0
  35. package/artifacts/schema/collection-record-list-query.schema.json +67 -0
  36. package/artifacts/schema/collection-record-list-response.schema.json +212 -0
  37. package/artifacts/schema/collection-record-restore-request.schema.json +14 -0
  38. package/artifacts/schema/collection-record-write.schema.json +31 -0
  39. package/artifacts/schema/collection-record.schema.json +180 -0
  40. package/artifacts/schema/collection-revision.schema.json +295 -0
  41. package/artifacts/schema/collection-subscribe-error.schema.json +31 -0
  42. package/artifacts/schema/collection-subscribe.schema.json +48 -0
  43. package/artifacts/schema/collection-subscribed.schema.json +22 -0
  44. package/artifacts/schema/collection-summary.schema.json +92 -0
  45. package/artifacts/schema/collection-unsubscribe.schema.json +22 -0
  46. package/artifacts/schema/collections-access-change.schema.json +34 -0
  47. package/artifacts/schema/collections-document.schema.json +732 -0
  48. package/artifacts/schema/collections-draft-response.schema.json +845 -0
  49. package/artifacts/schema/collections-effect.schema.json +384 -0
  50. package/artifacts/schema/collections-issue.schema.json +36 -0
  51. package/artifacts/schema/collections-publish-request.schema.json +18 -0
  52. package/artifacts/schema/collections-publish-response.schema.json +450 -0
  53. package/artifacts/schema/collections-purge-preview.schema.json +60 -0
  54. package/artifacts/schema/collections-purge-request.schema.json +27 -0
  55. package/artifacts/schema/collections-purge-response.schema.json +30 -0
  56. package/artifacts/schema/collections-restore-preview-query.schema.json +20 -0
  57. package/artifacts/schema/collections-restore-preview.schema.json +581 -0
  58. package/artifacts/schema/collections-restore-request.schema.json +27 -0
  59. package/artifacts/schema/collections-restore-response.schema.json +455 -0
  60. package/artifacts/schema/collections-schema-preview.schema.json +519 -0
  61. package/artifacts/schema/collections-schema-published.schema.json +22 -0
  62. package/artifacts/schema/collections-version-response.schema.json +869 -0
  63. package/artifacts/schema/collections-version-summary.schema.json +139 -0
  64. package/artifacts/schema/collections-versions-response.schema.json +167 -0
  65. package/artifacts/schema/config-draft-response.schema.json +359 -5
  66. package/artifacts/schema/config-version-response.schema.json +359 -5
  67. package/artifacts/schema/confirmation-required-details.schema.json +13 -0
  68. package/artifacts/schema/create-app-invitation-request.schema.json +4 -4
  69. package/artifacts/schema/create-app-user-request.schema.json +4 -4
  70. package/artifacts/schema/create-org-role-request.schema.json +33 -0
  71. package/artifacts/schema/dangling-reference.schema.json +130 -0
  72. package/artifacts/schema/datapoint-config.schema.json +35 -0
  73. package/artifacts/schema/datapoint-list-response.schema.json +2 -2
  74. package/artifacts/schema/effective-capability.schema.json +104 -0
  75. package/artifacts/schema/effective-rights-query.schema.json +19 -0
  76. package/artifacts/schema/effective-rights-response.schema.json +396 -0
  77. package/artifacts/schema/effective-robot-rights.schema.json +266 -0
  78. package/artifacts/schema/invoke-or-service-response.schema.json +1 -1
  79. package/artifacts/schema/invoke-response.schema.json +1 -1
  80. package/artifacts/schema/journal-intent-v2-body.schema.json +74 -0
  81. package/artifacts/schema/journal-intent-v2.schema.json +84 -0
  82. package/artifacts/schema/journal-replay-receipt-body.schema.json +1 -0
  83. package/artifacts/schema/journal-replay-receipt.schema.json +1 -0
  84. package/artifacts/schema/mcp-robot-datasheet.schema.json +2 -2
  85. package/artifacts/schema/org-plan.schema.json +29 -1
  86. package/artifacts/schema/org-quota-usage-counts.schema.json +18 -0
  87. package/artifacts/schema/org-quota-usage.schema.json +47 -1
  88. package/artifacts/schema/org-quotas.schema.json +29 -1
  89. package/artifacts/schema/org-role-list-response.schema.json +206 -0
  90. package/artifacts/schema/org-role.schema.json +59 -0
  91. package/artifacts/schema/patch-app-user-request.schema.json +4 -4
  92. package/artifacts/schema/patch-org-role-request.schema.json +38 -0
  93. package/artifacts/schema/plan-catalogue-entry.schema.json +29 -1
  94. package/artifacts/schema/plan-change-decision-status-response.schema.json +29 -1
  95. package/artifacts/schema/plan-limit-details.schema.json +3 -1
  96. package/artifacts/schema/plan-limits.schema.json +29 -1
  97. package/artifacts/schema/plan-overrides.schema.json +26 -0
  98. package/artifacts/schema/publish-config-response.schema.json +48 -1
  99. package/artifacts/schema/publisher-config.schema.json +35 -0
  100. package/artifacts/schema/put-collections-draft-request.schema.json +16 -0
  101. package/artifacts/schema/put-role-defaults-request.schema.json +127 -0
  102. package/artifacts/schema/record-changed-details.schema.json +16 -0
  103. package/artifacts/schema/record-referenced-details.schema.json +35 -0
  104. package/artifacts/schema/reference-invalid-details.schema.json +25 -0
  105. package/artifacts/schema/reference-place.schema.json +112 -0
  106. package/artifacts/schema/rename-slug-response.schema.json +4 -3
  107. package/artifacts/schema/required-missing-details.schema.json +19 -0
  108. package/artifacts/schema/robot-config-doc.schema.json +359 -5
  109. package/artifacts/schema/robot-deletion-summary.schema.json +33 -1
  110. package/artifacts/schema/role-collection-rights.schema.json +82 -0
  111. package/artifacts/schema/role-cycle-details.schema.json +18 -0
  112. package/artifacts/schema/role-defaults-response.schema.json +59 -0
  113. package/artifacts/schema/role-rename-preview.schema.json +147 -0
  114. package/artifacts/schema/service-config.schema.json +35 -0
  115. package/artifacts/schema/slug-usage-response.schema.json +4 -3
  116. package/artifacts/schema/update-app-request.schema.json +0 -12
  117. package/dist/app-users.d.ts +11 -8
  118. package/dist/app-users.js +18 -10
  119. package/dist/apps.d.ts +258 -71
  120. package/dist/apps.js +215 -151
  121. package/dist/audit.d.ts +35 -5
  122. package/dist/audit.js +40 -2
  123. package/dist/billing.d.ts +10 -10
  124. package/dist/client-auth.d.ts +5 -2
  125. package/dist/client-auth.js +11 -3
  126. package/dist/client-robots.d.ts +1 -1
  127. package/dist/client-robots.js +1 -1
  128. package/dist/collections-issues.d.ts +67 -0
  129. package/dist/collections-issues.js +340 -0
  130. package/dist/collections-records.d.ts +562 -0
  131. package/dist/collections-records.js +525 -0
  132. package/dist/collections-rights.d.ts +17 -0
  133. package/dist/collections-rights.js +41 -0
  134. package/dist/collections-schema.d.ts +1064 -0
  135. package/dist/collections-schema.js +386 -0
  136. package/dist/collections-templates.d.ts +55 -0
  137. package/dist/collections-templates.js +137 -0
  138. package/dist/collections-transfer.d.ts +101 -0
  139. package/dist/collections-transfer.js +206 -0
  140. package/dist/collections-values.d.ts +67 -0
  141. package/dist/collections-values.js +284 -0
  142. package/dist/collections.d.ts +568 -0
  143. package/dist/collections.js +332 -0
  144. package/dist/common.d.ts +1 -1
  145. package/dist/common.js +1 -1
  146. package/dist/config-issues.d.ts +1 -1
  147. package/dist/config-issues.js +1 -1
  148. package/dist/config.d.ts +78 -12
  149. package/dist/config.js +109 -15
  150. package/dist/errors.d.ts +63 -5
  151. package/dist/errors.js +152 -24
  152. package/dist/index.d.ts +40 -14
  153. package/dist/index.js +27 -7
  154. package/dist/journal-intents.d.ts +30 -0
  155. package/dist/journal-intents.js +3 -0
  156. package/dist/legal-acceptances.d.ts +2 -2
  157. package/dist/mcp.d.ts +5 -37
  158. package/dist/mcp.js +4 -14
  159. package/dist/org-roles.d.ts +186 -0
  160. package/dist/org-roles.js +199 -0
  161. package/dist/plans.d.ts +25 -7
  162. package/dist/plans.js +21 -1
  163. package/dist/protocol.d.ts +19 -1
  164. package/dist/realtime.d.ts +172 -3
  165. package/dist/realtime.js +164 -1
  166. package/dist/rest.d.ts +104 -53
  167. package/dist/rest.js +79 -55
  168. package/dist/rights.d.ts +197 -0
  169. package/dist/rights.js +396 -0
  170. package/dist/routes.d.ts +1 -1
  171. package/dist/routes.js +675 -150
  172. package/package.json +1 -1
  173. package/artifacts/schema/mcp-role-preview-response.schema.json +0 -134
  174. package/artifacts/schema/put-robot-details-request.schema.json +0 -41
  175. package/artifacts/schema/put-robot-details-response.schema.json +0 -43
  176. package/artifacts/schema/robot-details-doc.schema.json +0 -33
  177. package/artifacts/schema/role-delete-query.schema.json +0 -13
  178. package/artifacts/schema/role-in-use-details.schema.json +0 -28
  179. package/artifacts/schema/role-list-response.schema.json +0 -48
  180. package/artifacts/schema/role-permissions.schema.json +0 -61
  181. package/artifacts/schema/role-rename-request.schema.json +0 -16
  182. package/artifacts/schema/role.schema.json +0 -35
package/dist/billing.d.ts CHANGED
@@ -353,8 +353,8 @@ export declare const billingDecisionAmounts: z.ZodObject<{
353
353
  addon_lines: z.ZodArray<z.ZodObject<{
354
354
  addon: z.ZodEnum<{
355
355
  apps: "apps";
356
- seats: "seats";
357
356
  robots: "robots";
357
+ seats: "seats";
358
358
  app_user_packs: "app_user_packs";
359
359
  live_video_packs: "live_video_packs";
360
360
  }>;
@@ -839,8 +839,8 @@ export declare const billingDecisionSnapshot: z.ZodDiscriminatedUnion<[z.ZodObje
839
839
  addon_lines: z.ZodArray<z.ZodObject<{
840
840
  addon: z.ZodEnum<{
841
841
  apps: "apps";
842
- seats: "seats";
843
842
  robots: "robots";
843
+ seats: "seats";
844
844
  app_user_packs: "app_user_packs";
845
845
  live_video_packs: "live_video_packs";
846
846
  }>;
@@ -1369,8 +1369,8 @@ export declare const billingDecisionSnapshot: z.ZodDiscriminatedUnion<[z.ZodObje
1369
1369
  addon_lines: z.ZodArray<z.ZodObject<{
1370
1370
  addon: z.ZodEnum<{
1371
1371
  apps: "apps";
1372
- seats: "seats";
1373
1372
  robots: "robots";
1373
+ seats: "seats";
1374
1374
  app_user_packs: "app_user_packs";
1375
1375
  live_video_packs: "live_video_packs";
1376
1376
  }>;
@@ -1889,8 +1889,8 @@ export declare const billingDecisionSnapshot: z.ZodDiscriminatedUnion<[z.ZodObje
1889
1889
  addon_lines: z.ZodArray<z.ZodObject<{
1890
1890
  addon: z.ZodEnum<{
1891
1891
  apps: "apps";
1892
- seats: "seats";
1893
1892
  robots: "robots";
1893
+ seats: "seats";
1894
1894
  app_user_packs: "app_user_packs";
1895
1895
  live_video_packs: "live_video_packs";
1896
1896
  }>;
@@ -2420,8 +2420,8 @@ export declare const billingDecisionDisplayResponse: z.ZodObject<{
2420
2420
  addon_lines: z.ZodArray<z.ZodObject<{
2421
2421
  addon: z.ZodEnum<{
2422
2422
  apps: "apps";
2423
- seats: "seats";
2424
2423
  robots: "robots";
2424
+ seats: "seats";
2425
2425
  app_user_packs: "app_user_packs";
2426
2426
  live_video_packs: "live_video_packs";
2427
2427
  }>;
@@ -2950,8 +2950,8 @@ export declare const billingDecisionDisplayResponse: z.ZodObject<{
2950
2950
  addon_lines: z.ZodArray<z.ZodObject<{
2951
2951
  addon: z.ZodEnum<{
2952
2952
  apps: "apps";
2953
- seats: "seats";
2954
2953
  robots: "robots";
2954
+ seats: "seats";
2955
2955
  app_user_packs: "app_user_packs";
2956
2956
  live_video_packs: "live_video_packs";
2957
2957
  }>;
@@ -3470,8 +3470,8 @@ export declare const billingDecisionDisplayResponse: z.ZodObject<{
3470
3470
  addon_lines: z.ZodArray<z.ZodObject<{
3471
3471
  addon: z.ZodEnum<{
3472
3472
  apps: "apps";
3473
- seats: "seats";
3474
3473
  robots: "robots";
3474
+ seats: "seats";
3475
3475
  app_user_packs: "app_user_packs";
3476
3476
  live_video_packs: "live_video_packs";
3477
3477
  }>;
@@ -4005,8 +4005,8 @@ export declare const planChangePrepareResponse: z.ZodObject<{
4005
4005
  addon_lines: z.ZodArray<z.ZodObject<{
4006
4006
  addon: z.ZodEnum<{
4007
4007
  apps: "apps";
4008
- seats: "seats";
4009
4008
  robots: "robots";
4009
+ seats: "seats";
4010
4010
  app_user_packs: "app_user_packs";
4011
4011
  live_video_packs: "live_video_packs";
4012
4012
  }>;
@@ -4535,8 +4535,8 @@ export declare const planChangePrepareResponse: z.ZodObject<{
4535
4535
  addon_lines: z.ZodArray<z.ZodObject<{
4536
4536
  addon: z.ZodEnum<{
4537
4537
  apps: "apps";
4538
- seats: "seats";
4539
4538
  robots: "robots";
4539
+ seats: "seats";
4540
4540
  app_user_packs: "app_user_packs";
4541
4541
  live_video_packs: "live_video_packs";
4542
4542
  }>;
@@ -5055,8 +5055,8 @@ export declare const planChangePrepareResponse: z.ZodObject<{
5055
5055
  addon_lines: z.ZodArray<z.ZodObject<{
5056
5056
  addon: z.ZodEnum<{
5057
5057
  apps: "apps";
5058
- seats: "seats";
5059
5058
  robots: "robots";
5059
+ seats: "seats";
5060
5060
  app_user_packs: "app_user_packs";
5061
5061
  live_video_packs: "live_video_packs";
5062
5062
  }>;
@@ -499,7 +499,7 @@ export type McpConsentGrantListResponse = z.infer<typeof mcpConsentGrantListResp
499
499
  * client API and appears in the audit as the developer, and the console's own
500
500
  * live views subscribe on `/realtime` as one. A developer is **org-scoped, not
501
501
  * app-scoped** — they own the configuration of every robot in their org — so
502
- * `app_id` and `role_id` are null for them, and roles do not filter what they
502
+ * `app_id` and `role` are null for them, and roles do not filter what they
503
503
  * see. `kind` states this explicitly rather than leaving it to be inferred from
504
504
  * which id happens to be set.
505
505
  *
@@ -526,7 +526,10 @@ export declare const clientIdentity: z.ZodObject<{
526
526
  app_user_id: z.ZodNullable<z.ZodUUID>;
527
527
  server_key_id: z.ZodNullable<z.ZodUUID>;
528
528
  app_id: z.ZodNullable<z.ZodUUID>;
529
- role_id: z.ZodNullable<z.ZodUUID>;
529
+ role: z.ZodNullable<z.ZodObject<{
530
+ key: z.ZodString;
531
+ name: z.ZodString;
532
+ }, z.core.$strip>>;
530
533
  email: z.ZodNullable<z.ZodEmail>;
531
534
  two_factor_enabled: z.ZodNullable<z.ZodBoolean>;
532
535
  }, z.core.$strip>;
@@ -3,6 +3,7 @@ import { z } from 'zod';
3
3
  import { appIdentifier } from './apps.js';
4
4
  import { APP_USER_DISPLAY_NAME_MAX, appSignInMethods, appTwoFactorPolicy, providerSlug } from './app-users.js';
5
5
  import { loginCode, password, recoveryCode, recoveryCodesList, sessionTokens, totpCode } from './identity.js';
6
+ import { roleKey } from './rights.js';
6
7
  /**
7
8
  * **The client auth API: the whole of what an app user's browser talks to.**
8
9
  *
@@ -565,7 +566,7 @@ export const mcpConsentGrantListResponse = z.object({
565
566
  * client API and appears in the audit as the developer, and the console's own
566
567
  * live views subscribe on `/realtime` as one. A developer is **org-scoped, not
567
568
  * app-scoped** — they own the configuration of every robot in their org — so
568
- * `app_id` and `role_id` are null for them, and roles do not filter what they
569
+ * `app_id` and `role` are null for them, and roles do not filter what they
569
570
  * see. `kind` states this explicitly rather than leaving it to be inferred from
570
571
  * which id happens to be set.
571
572
  *
@@ -598,8 +599,15 @@ export const clientIdentity = z.object({
598
599
  app_id: z.uuid().nullable().meta({
599
600
  description: 'The app this session belongs to, and `null` for a developer — a developer is organisation-scoped and owns the configuration of every robot in the organisation rather than reaching one through an app.',
600
601
  }),
601
- role_id: z.uuid().nullable().meta({
602
- description: 'The role that decides what this caller may reach, and `null` for a developer. Roles are the only visibility filter: what a role does not grant does not exist for that user.',
602
+ role: z.object({
603
+ key: roleKey.meta({
604
+ description: 'The role\'s key, the same string `fleetless.yaml` and the organization\'s role list write.',
605
+ }),
606
+ name: z.string().meta({
607
+ description: 'The role\'s display name, for showing the caller what they hold. Changing it changes nothing about rights.',
608
+ }),
609
+ }).nullable().meta({
610
+ description: 'The organization role that decides what this caller may reach: its key and display name. `null` when there is no role to name: for a developer and for a server key, which are not filtered by a role, and for an app user whose key names no role (the role was deleted), who then reaches nothing.',
603
611
  }),
604
612
  email: z.email().nullable().meta({
605
613
  description: 'The address of the Fleetless user or app user behind this session, and `null` for a server key, which is not a person.',
@@ -7,7 +7,7 @@ import { z } from 'zod';
7
7
  *
8
8
  * Deliberately not `robotListItem`: that one carries `exposes`, the per-kind
9
9
  * counts a developer's list shows, which are a configuration fact rather than
10
- * something an app user's role grants. What an app user is entitled to is the
10
+ * something an app user's role decides. What an app user is entitled to is the
11
11
  * robot, its bridge state, and whether anything is published on it yet.
12
12
  */
13
13
  export declare const clientRobotListItem: z.ZodObject<{
@@ -10,7 +10,7 @@ import { robot } from './rest.js';
10
10
  *
11
11
  * Deliberately not `robotListItem`: that one carries `exposes`, the per-kind
12
12
  * counts a developer's list shows, which are a configuration fact rather than
13
- * something an app user's role grants. What an app user is entitled to is the
13
+ * something an app user's role decides. What an app user is entitled to is the
14
14
  * robot, its bridge state, and whether anything is published on it yet.
15
15
  */
16
16
  export const clientRobotListItem = z.object({
@@ -0,0 +1,67 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ import { z } from 'zod';
3
+ import { type CollectionsDocument } from './collections.js';
4
+ /**
5
+ * Everything wrong with a collections document that only the whole document
6
+ * can tell: the questions a zod schema cannot ask because they compare one
7
+ * part with another (does the reference point at a collection? does the
8
+ * default fit?) or count (how many collections?).
9
+ *
10
+ * It lives in contracts, as a pure function, so the cloud that refuses a
11
+ * document, the console that shows its problems while someone types, and the
12
+ * tools that write documents give the same answer in the same words.
13
+ */
14
+ /** One finding about a document. */
15
+ export declare const collectionsIssue: z.ZodObject<{
16
+ path: z.ZodString;
17
+ code: z.ZodString;
18
+ message: z.ZodString;
19
+ severity: z.ZodEnum<{
20
+ error: "error";
21
+ warning: "warning";
22
+ info: "info";
23
+ }>;
24
+ }, z.core.$strip>;
25
+ export type CollectionsIssue = z.infer<typeof collectionsIssue>;
26
+ export type CollectionsDocumentIssuesOptions = {
27
+ /**
28
+ * Returns what is wrong with an inline JSON Schema, or `null` when it is a
29
+ * usable JSON Schema (draft 2020-12). Contracts carries no JSON Schema
30
+ * validator, so a caller that has one passes it; without it inline schemas
31
+ * are not judged.
32
+ */
33
+ validateJsonSchema?: (schema: unknown) => string | null;
34
+ /** Checks a value against a JSON Schema, as for `checkCollectionValue`. Needed to judge a `default` on a `json` field. */
35
+ validateJsonValue?: (schema: unknown, value: unknown) => string | null;
36
+ /** Matches a text against a `pattern`, as for `checkCollectionValue`. Used to judge a `default` the way a write is judged. */
37
+ matchPattern?: (pattern: string, value: string) => boolean;
38
+ };
39
+ /**
40
+ * Finds every way a document is invalid, and the things worth knowing about a
41
+ * valid one.
42
+ *
43
+ * **Errors** make the document invalid: `too_many_collections`,
44
+ * `too_many_fields`, `reserved_key`, `unknown_reference_target`,
45
+ * `robot_reference_from_global`, `unknown_template`, `invalid_json_schema`,
46
+ * `invalid_pattern`, `display_template_invalid`,
47
+ * `display_placeholder_unresolved`, `duplicate_option`, `invalid_default`,
48
+ * `required_without_default` and `unique_not_allowed`.
49
+ *
50
+ * **Warnings** (`access_ignored`) and **information** (`no_role_has_access`)
51
+ * leave it valid. Nothing about stored data is judged here: a document is
52
+ * never invalid because of the records it would affect.
53
+ *
54
+ * Issues come in document order: each collection's fields, then its display,
55
+ * then its access. A field that is wrong in one way is not also reported for
56
+ * the knock-on effects of that (an unknown template does not also make its
57
+ * default invalid).
58
+ */
59
+ export declare function collectionsDocumentIssues(document: CollectionsDocument, options?: CollectionsDocumentIssuesOptions): CollectionsIssue[];
60
+ /**
61
+ * One warning for each role key in `access` that the organization does not
62
+ * have, per collection and action. The document stays valid: renaming or
63
+ * deleting a role never makes a published document fail, and a key that names
64
+ * no role grants nothing. `roleKeys` is the set of the organization's role
65
+ * keys.
66
+ */
67
+ export declare function collectionsAccessWarnings(document: CollectionsDocument, roleKeys: ReadonlySet<string>): CollectionsIssue[];
@@ -0,0 +1,340 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ import { z } from 'zod';
3
+ import { COLLECTION_LIMITS, COLLECTION_RESERVED_KEYS, } from './collections.js';
4
+ import { COLLECTION_JSON_TEMPLATES, parseDisplayTemplate } from './collections-templates.js';
5
+ import { checkCollectionValue } from './collections-values.js';
6
+ /**
7
+ * Everything wrong with a collections document that only the whole document
8
+ * can tell: the questions a zod schema cannot ask because they compare one
9
+ * part with another (does the reference point at a collection? does the
10
+ * default fit?) or count (how many collections?).
11
+ *
12
+ * It lives in contracts, as a pure function, so the cloud that refuses a
13
+ * document, the console that shows its problems while someone types, and the
14
+ * tools that write documents give the same answer in the same words.
15
+ */
16
+ /** One finding about a document. */
17
+ export const collectionsIssue = z
18
+ .object({
19
+ path: z.string().min(1).meta({ description: 'Where in the document, with dots between the keys: `collections.chargers.fields.location.to`.' }),
20
+ code: z.string().meta({ description: 'A stable name for the kind of finding, for code to branch on.' }),
21
+ message: z.string().meta({ description: 'What is wrong, in a sentence for a person.' }),
22
+ severity: z.enum(['error', 'warning', 'info']).meta({
23
+ description: 'An error makes the document invalid and it cannot be saved or published. A warning and information leave it valid.',
24
+ }),
25
+ })
26
+ .meta({ description: 'One finding about a collections document.' });
27
+ /** The keys every record has, for display placeholders and references. */
28
+ const SYSTEM_KEYS = ['id', 'created_at', 'created_by', 'updated_at', 'updated_by', 'revision'];
29
+ const RESERVED_KEYS = COLLECTION_RESERVED_KEYS;
30
+ const ACTIONS = ['read', 'create', 'update', 'delete'];
31
+ const isRobotBound = (collection) => collection.binding !== 'global';
32
+ /** The most unresolved display placeholders listed for one collection; the last issue counts the rest. */
33
+ const MAX_UNRESOLVED_LISTED = 20;
34
+ /** A piece of the document for a message: a very long one is cut, so a message never repeats a megabyte back. */
35
+ const clip = (text) => (text.length > 80 ? `${text.slice(0, 80)}…` : text);
36
+ /** Does a record of `collection` have a field or system value called `key`? */
37
+ function hasKey(collection, key) {
38
+ return (Object.hasOwn(collection.fields, key) ||
39
+ SYSTEM_KEYS.includes(key) ||
40
+ (key === 'robot_id' && isRobotBound(collection)));
41
+ }
42
+ /**
43
+ * Why a pattern cannot be used, or `null`.
44
+ *
45
+ * Patterns are written in the RE2 subset, and the server matches values with
46
+ * an RE2 engine, whose running time does not depend on the pattern (see
47
+ * `matchPattern` in `checkCollectionValue`). This function reads a pattern with
48
+ * the JavaScript engine in unicode mode, which agrees with RE2 on everything an
49
+ * author normally writes, and refuses the constructs RE2 lacks so that a
50
+ * pattern means the same in both: lookahead, lookbehind and backreferences.
51
+ * Finding them means reading the pattern the way the engine does, because the
52
+ * same characters are plain text inside a character class or after a backslash.
53
+ *
54
+ * It does not make matching with the JavaScript engine safe: a pattern such as
55
+ * `(a+)+$` is valid RE2 and slow there.
56
+ */
57
+ function patternProblem(pattern) {
58
+ let inClass = false;
59
+ for (let i = 0; i < pattern.length; i += 1) {
60
+ const char = pattern[i];
61
+ if (char === '\\') {
62
+ const next = pattern[i + 1];
63
+ if (!inClass && next !== undefined) {
64
+ if (next >= '1' && next <= '9')
65
+ return 'The pattern uses a backreference, which is not supported.';
66
+ if (next === 'k' && pattern[i + 2] === '<')
67
+ return 'The pattern uses a named backreference, which is not supported.';
68
+ }
69
+ i += 1;
70
+ }
71
+ else if (inClass) {
72
+ if (char === ']')
73
+ inClass = false;
74
+ }
75
+ else if (char === '[') {
76
+ inClass = true;
77
+ }
78
+ else if (char === '(' && pattern[i + 1] === '?') {
79
+ const kind = pattern.slice(i + 2, i + 4);
80
+ if (kind.startsWith('=') || kind.startsWith('!'))
81
+ return 'The pattern uses lookahead, which is not supported.';
82
+ if (kind === '<=' || kind === '<!')
83
+ return 'The pattern uses lookbehind, which is not supported.';
84
+ }
85
+ }
86
+ try {
87
+ new RegExp(pattern, 'u');
88
+ }
89
+ catch (error) {
90
+ return `The pattern is not a valid regular expression: ${error instanceof Error ? error.message : String(error)}`;
91
+ }
92
+ return null;
93
+ }
94
+ /**
95
+ * Why the engine that will match a pattern cannot use it, or `null`. `patternProblem`
96
+ * reads a pattern with the JavaScript engine; the server matches with another,
97
+ * and a pattern one compiles can be refused by the other (RE2 caps a repeat
98
+ * count at 1,000, for one). Asking the matcher once, with an empty value, turns
99
+ * that refusal into a finding at publish and not into an error on every write.
100
+ */
101
+ function engineProblem(pattern, matchPattern) {
102
+ if (matchPattern === undefined)
103
+ return null;
104
+ try {
105
+ matchPattern(pattern, '');
106
+ return null;
107
+ }
108
+ catch (error) {
109
+ return `The pattern is not a valid regular expression for the engine that matches it: ${clip(error instanceof Error ? error.message : String(error))}`;
110
+ }
111
+ }
112
+ /** Does the role value name at least one role? */
113
+ function namesARole(value) {
114
+ return typeof value === 'string' ? value.length > 0 : value !== undefined && value.length > 0;
115
+ }
116
+ /**
117
+ * Finds every way a document is invalid, and the things worth knowing about a
118
+ * valid one.
119
+ *
120
+ * **Errors** make the document invalid: `too_many_collections`,
121
+ * `too_many_fields`, `reserved_key`, `unknown_reference_target`,
122
+ * `robot_reference_from_global`, `unknown_template`, `invalid_json_schema`,
123
+ * `invalid_pattern`, `display_template_invalid`,
124
+ * `display_placeholder_unresolved`, `duplicate_option`, `invalid_default`,
125
+ * `required_without_default` and `unique_not_allowed`.
126
+ *
127
+ * **Warnings** (`access_ignored`) and **information** (`no_role_has_access`)
128
+ * leave it valid. Nothing about stored data is judged here: a document is
129
+ * never invalid because of the records it would affect.
130
+ *
131
+ * Issues come in document order: each collection's fields, then its display,
132
+ * then its access. A field that is wrong in one way is not also reported for
133
+ * the knock-on effects of that (an unknown template does not also make its
134
+ * default invalid).
135
+ */
136
+ export function collectionsDocumentIssues(document, options = {}) {
137
+ const issues = [];
138
+ const add = (severity, path, code, message) => {
139
+ issues.push({ path, code, message, severity });
140
+ };
141
+ const collections = Object.entries(document.collections);
142
+ function checkField(collectionKey, collection, fieldKey, field) {
143
+ const path = `collections.${collectionKey}.fields.${fieldKey}`;
144
+ if (RESERVED_KEYS.includes(fieldKey)) {
145
+ add('error', path, 'reserved_key', `"${fieldKey}" is reserved for the record itself and cannot be the key of a field. Reserved keys: ${COLLECTION_RESERVED_KEYS.join(', ')}.`);
146
+ }
147
+ if (field.unique && (field.type === 'boolean' || field.type === 'json')) {
148
+ add('error', `${path}.unique`, 'unique_not_allowed', `A ${field.type} field cannot be unique.`);
149
+ }
150
+ // Set when the field is already reported for something that makes its
151
+ // default meaningless to judge.
152
+ let defaultCheckable = true;
153
+ // The field as the default is judged against: a pattern that cannot be used is reported once, as itself.
154
+ let judged = field;
155
+ switch (field.type) {
156
+ case 'text': {
157
+ if (field.pattern !== undefined) {
158
+ const why = patternProblem(field.pattern) ?? engineProblem(field.pattern, options.matchPattern);
159
+ if (why !== null) {
160
+ add('error', `${path}.pattern`, 'invalid_pattern', why);
161
+ const { pattern: _unusable, ...withoutPattern } = field;
162
+ judged = withoutPattern;
163
+ }
164
+ }
165
+ break;
166
+ }
167
+ case 'select': {
168
+ const seen = new Set();
169
+ field.options.forEach((option, index) => {
170
+ if (seen.has(option.value)) {
171
+ add('error', `${path}.options.${index}.value`, 'duplicate_option', `The option value "${option.value}" is used more than once.`);
172
+ }
173
+ seen.add(option.value);
174
+ });
175
+ break;
176
+ }
177
+ case 'json': {
178
+ if (typeof field.schema === 'string') {
179
+ if (!Object.hasOwn(COLLECTION_JSON_TEMPLATES, field.schema)) {
180
+ defaultCheckable = false;
181
+ add('error', `${path}.schema`, 'unknown_template', `"${field.schema}" is not a JSON template. The templates are ${Object.keys(COLLECTION_JSON_TEMPLATES).join(', ')}; or write an inline JSON Schema.`);
182
+ }
183
+ }
184
+ else if (field.schema !== undefined && options.validateJsonSchema !== undefined) {
185
+ const why = options.validateJsonSchema(field.schema);
186
+ if (why !== null) {
187
+ defaultCheckable = false;
188
+ add('error', `${path}.schema`, 'invalid_json_schema', `The schema is not a valid JSON Schema: ${why}`);
189
+ }
190
+ }
191
+ break;
192
+ }
193
+ case 'reference': {
194
+ const hasTarget = Object.hasOwn(document.collections, field.to);
195
+ if (!hasTarget) {
196
+ add('error', `${path}.to`, 'unknown_reference_target', `"${field.to}" is not a collection of this document.`);
197
+ }
198
+ else if (!isRobotBound(collection) && isRobotBound(document.collections[field.to])) {
199
+ add('error', `${path}.to`, 'robot_reference_from_global', `A global collection cannot point at "${field.to}", which holds records of robots. Make ${collectionKey} per_robot, or point at a global collection.`);
200
+ }
201
+ break;
202
+ }
203
+ default:
204
+ break;
205
+ }
206
+ if (field.default !== undefined && defaultCheckable) {
207
+ const found = checkCollectionValue(judged, field.default, {
208
+ validateJsonValue: options.validateJsonValue,
209
+ matchPattern: options.matchPattern,
210
+ });
211
+ if (found !== null) {
212
+ add('error', `${path}.default`, 'invalid_default', `The default is not a valid value for this ${field.type} field: ${found.message}`);
213
+ }
214
+ }
215
+ if (collection.binding === 'one_per_robot' && field.required === true && field.default === undefined) {
216
+ add('error', `${path}.required`, 'required_without_default', `${fieldKey} is required, but Fleetless creates the records of a one_per_robot collection itself and cannot fill it. Give it a default or make it optional.`);
217
+ }
218
+ }
219
+ function checkDisplay(collectionKey, collection) {
220
+ if (collection.display === undefined)
221
+ return;
222
+ const path = `collections.${collectionKey}.display`;
223
+ const parsed = parseDisplayTemplate(collection.display);
224
+ if (!parsed.ok) {
225
+ add('error', path, 'display_template_invalid', parsed.message);
226
+ return;
227
+ }
228
+ // A placeholder used again is the same problem again: say it once for each collection.
229
+ // And a template full of different wrong ones is one problem told at length: the first
230
+ // few are listed and the last says how many more there are.
231
+ const reported = new Set();
232
+ let more = 0;
233
+ for (const placeholder of parsed.placeholders) {
234
+ if (!resolves(collection, placeholder.path)) {
235
+ const name = placeholder.path.join('.');
236
+ if (reported.has(name))
237
+ continue;
238
+ reported.add(name);
239
+ if (reported.size > MAX_UNRESOLVED_LISTED) {
240
+ more += 1;
241
+ continue;
242
+ }
243
+ const hint = placeholder.path[0] === 'robot'
244
+ ? ' robot.name is only available on collections of robots (per_robot and one_per_robot).'
245
+ : '';
246
+ add('error', path, 'display_placeholder_unresolved', `${clip(placeholder.raw)} does not name a field of this collection, a system field or a field reached through a reference.${hint}`);
247
+ }
248
+ }
249
+ if (more > 0) {
250
+ const last = issues[issues.length - 1];
251
+ last.message += ` ${more} more ${more === 1 ? 'placeholder does' : 'placeholders do'} not resolve either.`;
252
+ }
253
+ }
254
+ /** Can `path` (one or two keys of a display placeholder) be looked up on a record of `collection`? */
255
+ function resolves(collection, path) {
256
+ const [head, tail] = path;
257
+ if (tail === undefined)
258
+ return hasKey(collection, head);
259
+ if (head === 'robot')
260
+ return tail === 'name' && isRobotBound(collection);
261
+ const reference = Object.hasOwn(collection.fields, head) ? collection.fields[head] : undefined;
262
+ if (reference === undefined || reference.type !== 'reference')
263
+ return false;
264
+ // A missing target is already an error of its own; reporting the placeholder as well would say it twice.
265
+ if (!Object.hasOwn(document.collections, reference.to))
266
+ return true;
267
+ return hasKey(document.collections[reference.to], tail);
268
+ }
269
+ function checkAccess(collectionKey, collection) {
270
+ const access = collection.access;
271
+ const base = `collections.${collectionKey}.access`;
272
+ if (collection.binding === 'one_per_robot' && access !== undefined) {
273
+ for (const action of ['create', 'delete']) {
274
+ if (access[action] !== undefined) {
275
+ add('warning', `${base}.${action}`, 'access_ignored', `Records of a one_per_robot collection are never ${action === 'create' ? 'created' : 'deleted'} through the API, so access.${action} grants nothing.`);
276
+ }
277
+ }
278
+ }
279
+ if (!namesARole(access?.read)) {
280
+ add('info', `${base}.read`, 'no_role_has_access', `No role can read ${collectionKey}. Only developers and server keys reach its records.`);
281
+ }
282
+ }
283
+ if (collections.length > COLLECTION_LIMITS.collections) {
284
+ add('error', 'collections', 'too_many_collections', `A document holds at most ${COLLECTION_LIMITS.collections} collections; this one has ${collections.length}.`);
285
+ }
286
+ for (const [collectionKey, collection] of collections) {
287
+ const base = `collections.${collectionKey}`;
288
+ const fields = Object.entries(collection.fields);
289
+ if (fields.length > COLLECTION_LIMITS.fields) {
290
+ add('error', `${base}.fields`, 'too_many_fields', `A collection holds at most ${COLLECTION_LIMITS.fields} fields; ${collectionKey} has ${fields.length}.`);
291
+ }
292
+ for (const [fieldKey, field] of fields) {
293
+ checkField(collectionKey, collection, fieldKey, field);
294
+ }
295
+ checkDisplay(collectionKey, collection);
296
+ checkAccess(collectionKey, collection);
297
+ }
298
+ return issues;
299
+ }
300
+ /**
301
+ * The role keys in an access value, in the order they are written.
302
+ * `{ all, own }` is flattened, so a role named in both is one role.
303
+ */
304
+ function rolesIn(value) {
305
+ if (typeof value === 'string')
306
+ return [value];
307
+ if (Array.isArray(value))
308
+ return value.filter((role) => typeof role === 'string');
309
+ if (value !== null && typeof value === 'object') {
310
+ const { all, own } = value;
311
+ return [...rolesIn(all), ...rolesIn(own)];
312
+ }
313
+ return [];
314
+ }
315
+ /**
316
+ * One warning for each role key in `access` that the organization does not
317
+ * have, per collection and action. The document stays valid: renaming or
318
+ * deleting a role never makes a published document fail, and a key that names
319
+ * no role grants nothing. `roleKeys` is the set of the organization's role
320
+ * keys.
321
+ */
322
+ export function collectionsAccessWarnings(document, roleKeys) {
323
+ const warnings = [];
324
+ for (const [collectionKey, collection] of Object.entries(document.collections)) {
325
+ if (collection.access === undefined)
326
+ continue;
327
+ for (const action of ACTIONS) {
328
+ const unknown = new Set(rolesIn(collection.access[action]).filter((role) => !roleKeys.has(role)));
329
+ for (const role of unknown) {
330
+ warnings.push({
331
+ path: `collections.${collectionKey}.access.${action}`,
332
+ code: 'unknown_role',
333
+ message: `The role "${role}" is not a role of the organization, so it grants nothing.`,
334
+ severity: 'warning',
335
+ });
336
+ }
337
+ }
338
+ }
339
+ return warnings;
340
+ }