@originals/sdk 1.2.0 → 1.4.2

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 (244) hide show
  1. package/package.json +1 -1
  2. package/src/did/DIDManager.ts +1 -1
  3. package/src/did/WebVHManager.ts +11 -2
  4. package/src/examples/create-module-original.ts +435 -0
  5. package/src/examples/full-lifecycle-flow.ts +514 -0
  6. package/src/examples/run.ts +59 -4
  7. package/src/index.ts +69 -3
  8. package/src/kinds/KindRegistry.ts +290 -0
  9. package/src/kinds/index.ts +74 -0
  10. package/src/kinds/types.ts +470 -0
  11. package/src/kinds/validators/AgentValidator.ts +257 -0
  12. package/src/kinds/validators/AppValidator.ts +211 -0
  13. package/src/kinds/validators/DatasetValidator.ts +242 -0
  14. package/src/kinds/validators/DocumentValidator.ts +311 -0
  15. package/src/kinds/validators/MediaValidator.ts +269 -0
  16. package/src/kinds/validators/ModuleValidator.ts +225 -0
  17. package/src/kinds/validators/base.ts +276 -0
  18. package/src/kinds/validators/index.ts +12 -0
  19. package/src/lifecycle/LifecycleManager.ts +909 -1
  20. package/src/resources/ResourceManager.ts +655 -0
  21. package/src/resources/index.ts +21 -0
  22. package/src/resources/types.ts +202 -0
  23. package/src/types/common.ts +1 -1
  24. package/src/vc/CredentialManager.ts +647 -2
  25. package/tests/integration/createTypedOriginal.test.ts +379 -0
  26. package/tests/performance/BatchOperations.perf.test.ts +2 -2
  27. package/tests/unit/kinds/KindRegistry.test.ts +329 -0
  28. package/tests/unit/kinds/types.test.ts +409 -0
  29. package/tests/unit/kinds/validators.test.ts +651 -0
  30. package/tests/unit/lifecycle/LifecycleManager.cleanapi.test.ts +441 -0
  31. package/tests/unit/resources/ResourceManager.test.ts +740 -0
  32. package/tests/unit/vc/CredentialManager.helpers.test.ts +527 -0
  33. package/.turbo/turbo-build.log +0 -1
  34. package/dist/adapters/FeeOracleMock.d.ts +0 -6
  35. package/dist/adapters/FeeOracleMock.js +0 -8
  36. package/dist/adapters/index.d.ts +0 -4
  37. package/dist/adapters/index.js +0 -4
  38. package/dist/adapters/providers/OrdHttpProvider.d.ts +0 -56
  39. package/dist/adapters/providers/OrdHttpProvider.js +0 -110
  40. package/dist/adapters/providers/OrdMockProvider.d.ts +0 -70
  41. package/dist/adapters/providers/OrdMockProvider.js +0 -75
  42. package/dist/adapters/types.d.ts +0 -71
  43. package/dist/adapters/types.js +0 -1
  44. package/dist/bitcoin/BitcoinManager.d.ts +0 -15
  45. package/dist/bitcoin/BitcoinManager.js +0 -262
  46. package/dist/bitcoin/BroadcastClient.d.ts +0 -30
  47. package/dist/bitcoin/BroadcastClient.js +0 -35
  48. package/dist/bitcoin/OrdinalsClient.d.ts +0 -21
  49. package/dist/bitcoin/OrdinalsClient.js +0 -105
  50. package/dist/bitcoin/PSBTBuilder.d.ts +0 -24
  51. package/dist/bitcoin/PSBTBuilder.js +0 -80
  52. package/dist/bitcoin/fee-calculation.d.ts +0 -14
  53. package/dist/bitcoin/fee-calculation.js +0 -31
  54. package/dist/bitcoin/providers/OrdNodeProvider.d.ts +0 -38
  55. package/dist/bitcoin/providers/OrdNodeProvider.js +0 -67
  56. package/dist/bitcoin/providers/OrdinalsProvider.d.ts +0 -33
  57. package/dist/bitcoin/providers/OrdinalsProvider.js +0 -50
  58. package/dist/bitcoin/providers/types.d.ts +0 -63
  59. package/dist/bitcoin/providers/types.js +0 -1
  60. package/dist/bitcoin/transactions/commit.d.ts +0 -89
  61. package/dist/bitcoin/transactions/commit.js +0 -311
  62. package/dist/bitcoin/transactions/index.d.ts +0 -7
  63. package/dist/bitcoin/transactions/index.js +0 -8
  64. package/dist/bitcoin/transfer.d.ts +0 -9
  65. package/dist/bitcoin/transfer.js +0 -26
  66. package/dist/bitcoin/utxo-selection.d.ts +0 -78
  67. package/dist/bitcoin/utxo-selection.js +0 -237
  68. package/dist/bitcoin/utxo.d.ts +0 -26
  69. package/dist/bitcoin/utxo.js +0 -78
  70. package/dist/contexts/credentials-v1.json +0 -195
  71. package/dist/contexts/credentials-v2-examples.json +0 -5
  72. package/dist/contexts/credentials-v2.json +0 -301
  73. package/dist/contexts/credentials.json +0 -195
  74. package/dist/contexts/data-integrity-v2.json +0 -81
  75. package/dist/contexts/dids.json +0 -57
  76. package/dist/contexts/ed255192020.json +0 -93
  77. package/dist/contexts/ordinals-plus.json +0 -23
  78. package/dist/contexts/originals.json +0 -22
  79. package/dist/core/OriginalsSDK.d.ts +0 -158
  80. package/dist/core/OriginalsSDK.js +0 -274
  81. package/dist/crypto/Multikey.d.ts +0 -30
  82. package/dist/crypto/Multikey.js +0 -149
  83. package/dist/crypto/Signer.d.ts +0 -21
  84. package/dist/crypto/Signer.js +0 -196
  85. package/dist/crypto/noble-init.d.ts +0 -18
  86. package/dist/crypto/noble-init.js +0 -106
  87. package/dist/did/BtcoDidResolver.d.ts +0 -57
  88. package/dist/did/BtcoDidResolver.js +0 -166
  89. package/dist/did/DIDManager.d.ts +0 -101
  90. package/dist/did/DIDManager.js +0 -493
  91. package/dist/did/Ed25519Verifier.d.ts +0 -30
  92. package/dist/did/Ed25519Verifier.js +0 -59
  93. package/dist/did/KeyManager.d.ts +0 -17
  94. package/dist/did/KeyManager.js +0 -207
  95. package/dist/did/WebVHManager.d.ts +0 -100
  96. package/dist/did/WebVHManager.js +0 -304
  97. package/dist/did/createBtcoDidDocument.d.ts +0 -10
  98. package/dist/did/createBtcoDidDocument.js +0 -42
  99. package/dist/did/providers/OrdinalsClientProviderAdapter.d.ts +0 -23
  100. package/dist/did/providers/OrdinalsClientProviderAdapter.js +0 -51
  101. package/dist/events/EventEmitter.d.ts +0 -115
  102. package/dist/events/EventEmitter.js +0 -198
  103. package/dist/events/index.d.ts +0 -7
  104. package/dist/events/index.js +0 -6
  105. package/dist/events/types.d.ts +0 -286
  106. package/dist/events/types.js +0 -9
  107. package/dist/examples/basic-usage.d.ts +0 -3
  108. package/dist/examples/basic-usage.js +0 -62
  109. package/dist/examples/run.d.ts +0 -1
  110. package/dist/examples/run.js +0 -4
  111. package/dist/index.d.ts +0 -39
  112. package/dist/index.js +0 -47
  113. package/dist/lifecycle/BatchOperations.d.ts +0 -147
  114. package/dist/lifecycle/BatchOperations.js +0 -251
  115. package/dist/lifecycle/LifecycleManager.d.ts +0 -116
  116. package/dist/lifecycle/LifecycleManager.js +0 -971
  117. package/dist/lifecycle/OriginalsAsset.d.ts +0 -164
  118. package/dist/lifecycle/OriginalsAsset.js +0 -380
  119. package/dist/lifecycle/ProvenanceQuery.d.ts +0 -126
  120. package/dist/lifecycle/ProvenanceQuery.js +0 -220
  121. package/dist/lifecycle/ResourceVersioning.d.ts +0 -73
  122. package/dist/lifecycle/ResourceVersioning.js +0 -127
  123. package/dist/migration/MigrationManager.d.ts +0 -86
  124. package/dist/migration/MigrationManager.js +0 -412
  125. package/dist/migration/audit/AuditLogger.d.ts +0 -51
  126. package/dist/migration/audit/AuditLogger.js +0 -156
  127. package/dist/migration/checkpoint/CheckpointManager.d.ts +0 -31
  128. package/dist/migration/checkpoint/CheckpointManager.js +0 -96
  129. package/dist/migration/checkpoint/CheckpointStorage.d.ts +0 -26
  130. package/dist/migration/checkpoint/CheckpointStorage.js +0 -89
  131. package/dist/migration/index.d.ts +0 -22
  132. package/dist/migration/index.js +0 -27
  133. package/dist/migration/operations/BaseMigration.d.ts +0 -48
  134. package/dist/migration/operations/BaseMigration.js +0 -83
  135. package/dist/migration/operations/PeerToBtcoMigration.d.ts +0 -25
  136. package/dist/migration/operations/PeerToBtcoMigration.js +0 -67
  137. package/dist/migration/operations/PeerToWebvhMigration.d.ts +0 -19
  138. package/dist/migration/operations/PeerToWebvhMigration.js +0 -46
  139. package/dist/migration/operations/WebvhToBtcoMigration.d.ts +0 -25
  140. package/dist/migration/operations/WebvhToBtcoMigration.js +0 -67
  141. package/dist/migration/rollback/RollbackManager.d.ts +0 -29
  142. package/dist/migration/rollback/RollbackManager.js +0 -146
  143. package/dist/migration/state/StateMachine.d.ts +0 -25
  144. package/dist/migration/state/StateMachine.js +0 -76
  145. package/dist/migration/state/StateTracker.d.ts +0 -36
  146. package/dist/migration/state/StateTracker.js +0 -123
  147. package/dist/migration/types.d.ts +0 -306
  148. package/dist/migration/types.js +0 -33
  149. package/dist/migration/validation/BitcoinValidator.d.ts +0 -13
  150. package/dist/migration/validation/BitcoinValidator.js +0 -83
  151. package/dist/migration/validation/CredentialValidator.d.ts +0 -13
  152. package/dist/migration/validation/CredentialValidator.js +0 -46
  153. package/dist/migration/validation/DIDCompatibilityValidator.d.ts +0 -16
  154. package/dist/migration/validation/DIDCompatibilityValidator.js +0 -127
  155. package/dist/migration/validation/LifecycleValidator.d.ts +0 -10
  156. package/dist/migration/validation/LifecycleValidator.js +0 -52
  157. package/dist/migration/validation/StorageValidator.d.ts +0 -10
  158. package/dist/migration/validation/StorageValidator.js +0 -65
  159. package/dist/migration/validation/ValidationPipeline.d.ts +0 -29
  160. package/dist/migration/validation/ValidationPipeline.js +0 -180
  161. package/dist/storage/LocalStorageAdapter.d.ts +0 -11
  162. package/dist/storage/LocalStorageAdapter.js +0 -53
  163. package/dist/storage/MemoryStorageAdapter.d.ts +0 -6
  164. package/dist/storage/MemoryStorageAdapter.js +0 -21
  165. package/dist/storage/StorageAdapter.d.ts +0 -16
  166. package/dist/storage/StorageAdapter.js +0 -1
  167. package/dist/storage/index.d.ts +0 -2
  168. package/dist/storage/index.js +0 -2
  169. package/dist/types/bitcoin.d.ts +0 -84
  170. package/dist/types/bitcoin.js +0 -1
  171. package/dist/types/common.d.ts +0 -82
  172. package/dist/types/common.js +0 -1
  173. package/dist/types/credentials.d.ts +0 -75
  174. package/dist/types/credentials.js +0 -1
  175. package/dist/types/did.d.ts +0 -26
  176. package/dist/types/did.js +0 -1
  177. package/dist/types/index.d.ts +0 -5
  178. package/dist/types/index.js +0 -5
  179. package/dist/types/network.d.ts +0 -78
  180. package/dist/types/network.js +0 -145
  181. package/dist/utils/EventLogger.d.ts +0 -71
  182. package/dist/utils/EventLogger.js +0 -232
  183. package/dist/utils/Logger.d.ts +0 -106
  184. package/dist/utils/Logger.js +0 -257
  185. package/dist/utils/MetricsCollector.d.ts +0 -110
  186. package/dist/utils/MetricsCollector.js +0 -264
  187. package/dist/utils/bitcoin-address.d.ts +0 -38
  188. package/dist/utils/bitcoin-address.js +0 -113
  189. package/dist/utils/cbor.d.ts +0 -2
  190. package/dist/utils/cbor.js +0 -9
  191. package/dist/utils/encoding.d.ts +0 -37
  192. package/dist/utils/encoding.js +0 -120
  193. package/dist/utils/hash.d.ts +0 -1
  194. package/dist/utils/hash.js +0 -5
  195. package/dist/utils/retry.d.ts +0 -10
  196. package/dist/utils/retry.js +0 -35
  197. package/dist/utils/satoshi-validation.d.ts +0 -60
  198. package/dist/utils/satoshi-validation.js +0 -156
  199. package/dist/utils/serialization.d.ts +0 -14
  200. package/dist/utils/serialization.js +0 -76
  201. package/dist/utils/telemetry.d.ts +0 -17
  202. package/dist/utils/telemetry.js +0 -24
  203. package/dist/utils/validation.d.ts +0 -5
  204. package/dist/utils/validation.js +0 -98
  205. package/dist/vc/CredentialManager.d.ts +0 -22
  206. package/dist/vc/CredentialManager.js +0 -227
  207. package/dist/vc/Issuer.d.ts +0 -27
  208. package/dist/vc/Issuer.js +0 -70
  209. package/dist/vc/Verifier.d.ts +0 -16
  210. package/dist/vc/Verifier.js +0 -50
  211. package/dist/vc/cryptosuites/bbs.d.ts +0 -44
  212. package/dist/vc/cryptosuites/bbs.js +0 -213
  213. package/dist/vc/cryptosuites/bbsSimple.d.ts +0 -9
  214. package/dist/vc/cryptosuites/bbsSimple.js +0 -12
  215. package/dist/vc/cryptosuites/eddsa.d.ts +0 -30
  216. package/dist/vc/cryptosuites/eddsa.js +0 -81
  217. package/dist/vc/documentLoader.d.ts +0 -16
  218. package/dist/vc/documentLoader.js +0 -59
  219. package/dist/vc/proofs/data-integrity.d.ts +0 -21
  220. package/dist/vc/proofs/data-integrity.js +0 -15
  221. package/dist/vc/utils/jsonld.d.ts +0 -2
  222. package/dist/vc/utils/jsonld.js +0 -15
  223. package/test/logs/did_webvh_QmNTn9Kkp8dQ75WrF9xqJ2kuDp9QhKc3aPiERRMj8XoTBN_example_com.jsonl +0 -1
  224. package/test/logs/did_webvh_QmNu4MNr8Lr5txx5gYNhuhZDchXsZEu3hJXKYuphpWTPDp_example_com_users_etc_passwd.jsonl +0 -1
  225. package/test/logs/did_webvh_QmR9MrGZACzjKETA8SBRNCKG11HxU85c4bVR2qN5eDCfsD_example_com.jsonl +0 -1
  226. package/test/logs/did_webvh_QmUc5suaqRM2P4nrXxZwqYMfqzhdMqjuL7oJaJbEpCQVCd_example_com_users_etc_passwd.jsonl +0 -1
  227. package/test/logs/did_webvh_QmUkiB2RCV2VZ1RTXsCebWN25Eiy9TLvpzDWAJNjhgvB4X_example_com_etc_passwd.jsonl +0 -1
  228. package/test/logs/did_webvh_QmUoRTe8UMwpAQXZSAW7pjAgZK1tq2X3C6Kfxq3UXGcaGy_example_com_secret.jsonl +0 -1
  229. package/test/logs/did_webvh_QmWWot3chx1t6KwTmcE5i2FeDZ5JMkQw3qXycsKDVmJ9Be_example_com_users_alice.jsonl +0 -1
  230. package/test/logs/did_webvh_QmWvVgALL5kjZdpgR7KZay7J8UiiUr834kkRmWeFAxjAuC_example_com_users_etc_passwd.jsonl +0 -1
  231. package/test/logs/did_webvh_QmWwaRQHUZAFcKihFC6xR6tRTTrQhHPTku6azf1egWbpy1_example_com_users_alice.jsonl +0 -1
  232. package/test/logs/did_webvh_QmXJLtkz23r7AozbtXsZMKWnVU6rd38CkVtjdWuATU3Yp6_example_com_users_alice123_profile.jsonl +0 -1
  233. package/test/logs/did_webvh_QmYsce448po14oDE1wXbyaP6wY9HQgHSKLwdezn1k577SF_example_com_my_org_user_name_test_123.jsonl +0 -1
  234. package/test/logs/did_webvh_QmZBeNzzqajxdfwcDUPZ4P8C5YSXyRztrAwmPiKuKUxmAK_example_com.jsonl +0 -1
  235. package/test/logs/did_webvh_QmZhJsqxizwVbRtqCUkmE6XQunSxtxMt3gbTYadVBNAaEq_example_com.jsonl +0 -1
  236. package/test/logs/did_webvh_QmZk7NHU2D57RzzbMq4tWW9gBa9AqtVTWfiRM6RFdwGVj2_example_com.jsonl +0 -1
  237. package/test/logs/did_webvh_QmZshSXp9w8ovH62zGGBS1b5pGGPsuYiu1VQ935sga2hWF_example_com_level1_level2.jsonl +0 -1
  238. package/test/logs/did_webvh_QmbWAmw7HQL7vKJyCsctZihXf1rmT4sGvggKCPKWcUWjw1_example_com.jsonl +0 -1
  239. package/test/logs/did_webvh_QmbdLUMbYs3juR39TLB6hhrFWLcNg45ybUzeBJCS1MhCh1_example_com_C_Windows_System32.jsonl +0 -1
  240. package/test/logs/did_webvh_QmcaQ1Ma4gkSbae85aCm8Mv4rvdT2Sb2RR3JzYwrm5XBq8_example_com_etc_passwd.jsonl +0 -1
  241. package/test/logs/did_webvh_QmcbA7WQhsBqZSoDpKJHjV8Q5o53h8vmgJhQfo6rqTY5ho_example_com.jsonl +0 -1
  242. package/test/logs/did_webvh_Qmdy8uWr2gkUJrXsThynAug3DASTWwb3onEj89LKmMGZYB_example_com.jsonl +0 -1
  243. package/tests/e2e/README.md +0 -97
  244. package/tests/e2e/example.spec.ts +0 -78
@@ -0,0 +1,311 @@
1
+ /**
2
+ * Document Kind Validator
3
+ *
4
+ * Validates manifests for text documents with formatting and sections.
5
+ */
6
+
7
+ import { OriginalKind, type OriginalManifest, type ValidationResult, type DocumentMetadata } from '../types';
8
+ import { BaseKindValidator, ValidationUtils } from './base';
9
+
10
+ /**
11
+ * Valid document formats
12
+ */
13
+ const VALID_FORMATS = ['markdown', 'html', 'pdf', 'docx', 'txt', 'asciidoc', 'rst', 'latex'];
14
+
15
+ /**
16
+ * Valid document statuses
17
+ */
18
+ const VALID_STATUSES = ['draft', 'review', 'published', 'archived'];
19
+
20
+ /**
21
+ * Validator for Document Originals
22
+ */
23
+ export class DocumentValidator extends BaseKindValidator<OriginalKind.Document> {
24
+ readonly kind = OriginalKind.Document;
25
+
26
+ protected validateKind(manifest: OriginalManifest<OriginalKind.Document>): ValidationResult {
27
+ const errors: ValidationResult['errors'] = [];
28
+ const warnings: ValidationResult['warnings'] = [];
29
+ const metadata = manifest.metadata as DocumentMetadata;
30
+
31
+ // Validate metadata exists
32
+ if (!metadata || typeof metadata !== 'object') {
33
+ return ValidationUtils.failure([
34
+ ValidationUtils.error('MISSING_METADATA', 'Document manifest must have metadata', 'metadata'),
35
+ ]);
36
+ }
37
+
38
+ // Validate format (required)
39
+ if (!metadata.format) {
40
+ errors.push(ValidationUtils.error(
41
+ 'MISSING_FORMAT',
42
+ 'Document must specify a format',
43
+ 'metadata.format',
44
+ ));
45
+ } else if (!VALID_FORMATS.includes(metadata.format)) {
46
+ errors.push(ValidationUtils.error(
47
+ 'INVALID_FORMAT',
48
+ `Invalid document format: "${metadata.format}". Must be one of: ${VALID_FORMATS.join(', ')}`,
49
+ 'metadata.format',
50
+ metadata.format,
51
+ ));
52
+ }
53
+
54
+ // Validate content (required)
55
+ if (!metadata.content || typeof metadata.content !== 'string') {
56
+ errors.push(ValidationUtils.error(
57
+ 'MISSING_CONTENT',
58
+ 'Document must specify a content resource',
59
+ 'metadata.content',
60
+ ));
61
+ } else {
62
+ // Check if content references an existing resource
63
+ if (!ValidationUtils.resourceExists(metadata.content, manifest.resources)) {
64
+ warnings.push(ValidationUtils.warning(
65
+ 'CONTENT_NOT_RESOURCE',
66
+ `Content "${metadata.content}" does not match a resource ID`,
67
+ 'metadata.content',
68
+ 'Ensure the content field references a valid resource ID',
69
+ ));
70
+ }
71
+ }
72
+
73
+ // Validate language if specified
74
+ if (metadata.language) {
75
+ if (typeof metadata.language !== 'string') {
76
+ errors.push(ValidationUtils.error(
77
+ 'INVALID_LANGUAGE',
78
+ 'Language must be a string',
79
+ 'metadata.language',
80
+ ));
81
+ } else if (!ValidationUtils.isValidLanguageCode(metadata.language)) {
82
+ warnings.push(ValidationUtils.warning(
83
+ 'INVALID_LANGUAGE_CODE',
84
+ `Language "${metadata.language}" is not a valid ISO 639-1 code`,
85
+ 'metadata.language',
86
+ 'Use a 2-letter language code like "en", "es", "fr"',
87
+ ));
88
+ }
89
+ }
90
+
91
+ // Validate toc if specified
92
+ if (metadata.toc) {
93
+ if (!Array.isArray(metadata.toc)) {
94
+ errors.push(ValidationUtils.error(
95
+ 'INVALID_TOC',
96
+ 'Table of contents must be an array',
97
+ 'metadata.toc',
98
+ ));
99
+ } else {
100
+ for (let i = 0; i < metadata.toc.length; i++) {
101
+ const entry = metadata.toc[i];
102
+ const entryPath = `metadata.toc[${i}]`;
103
+
104
+ if (!entry || typeof entry !== 'object') {
105
+ errors.push(ValidationUtils.error(
106
+ 'INVALID_TOC_ENTRY',
107
+ `TOC entry at index ${i} must be an object`,
108
+ entryPath,
109
+ ));
110
+ continue;
111
+ }
112
+
113
+ if (!entry.title || typeof entry.title !== 'string') {
114
+ errors.push(ValidationUtils.error(
115
+ 'MISSING_TOC_TITLE',
116
+ `TOC entry at index ${i} must have a title`,
117
+ `${entryPath}.title`,
118
+ ));
119
+ }
120
+
121
+ if (typeof entry.level !== 'number' || entry.level < 1 || !Number.isInteger(entry.level)) {
122
+ errors.push(ValidationUtils.error(
123
+ 'INVALID_TOC_LEVEL',
124
+ `TOC entry at index ${i} must have a valid level (positive integer)`,
125
+ `${entryPath}.level`,
126
+ ));
127
+ }
128
+ }
129
+ }
130
+ }
131
+
132
+ // Validate pageCount if specified
133
+ if (metadata.pageCount !== undefined) {
134
+ if (typeof metadata.pageCount !== 'number' || metadata.pageCount <= 0 || !Number.isInteger(metadata.pageCount)) {
135
+ errors.push(ValidationUtils.error(
136
+ 'INVALID_PAGE_COUNT',
137
+ 'Page count must be a positive integer',
138
+ 'metadata.pageCount',
139
+ ));
140
+ }
141
+ }
142
+
143
+ // Validate wordCount if specified
144
+ if (metadata.wordCount !== undefined) {
145
+ if (typeof metadata.wordCount !== 'number' || metadata.wordCount < 0 || !Number.isInteger(metadata.wordCount)) {
146
+ errors.push(ValidationUtils.error(
147
+ 'INVALID_WORD_COUNT',
148
+ 'Word count must be a non-negative integer',
149
+ 'metadata.wordCount',
150
+ ));
151
+ }
152
+ }
153
+
154
+ // Validate readingTime if specified
155
+ if (metadata.readingTime !== undefined) {
156
+ if (typeof metadata.readingTime !== 'number' || metadata.readingTime <= 0) {
157
+ errors.push(ValidationUtils.error(
158
+ 'INVALID_READING_TIME',
159
+ 'Reading time must be a positive number (minutes)',
160
+ 'metadata.readingTime',
161
+ ));
162
+ }
163
+ }
164
+
165
+ // Validate keywords if specified
166
+ if (metadata.keywords) {
167
+ if (!Array.isArray(metadata.keywords)) {
168
+ errors.push(ValidationUtils.error(
169
+ 'INVALID_KEYWORDS',
170
+ 'Keywords must be an array of strings',
171
+ 'metadata.keywords',
172
+ ));
173
+ } else {
174
+ for (let i = 0; i < metadata.keywords.length; i++) {
175
+ if (typeof metadata.keywords[i] !== 'string') {
176
+ errors.push(ValidationUtils.error(
177
+ 'INVALID_KEYWORD',
178
+ `Keyword at index ${i} must be a string`,
179
+ `metadata.keywords[${i}]`,
180
+ ));
181
+ }
182
+ }
183
+ }
184
+ }
185
+
186
+ // Validate references if specified
187
+ if (metadata.references) {
188
+ if (!Array.isArray(metadata.references)) {
189
+ errors.push(ValidationUtils.error(
190
+ 'INVALID_REFERENCES',
191
+ 'References must be an array',
192
+ 'metadata.references',
193
+ ));
194
+ } else {
195
+ const refIds = new Set<string>();
196
+
197
+ for (let i = 0; i < metadata.references.length; i++) {
198
+ const ref = metadata.references[i];
199
+ const refPath = `metadata.references[${i}]`;
200
+
201
+ if (!ref || typeof ref !== 'object') {
202
+ errors.push(ValidationUtils.error(
203
+ 'INVALID_REFERENCE',
204
+ `Reference at index ${i} must be an object`,
205
+ refPath,
206
+ ));
207
+ continue;
208
+ }
209
+
210
+ if (!ref.id || typeof ref.id !== 'string') {
211
+ errors.push(ValidationUtils.error(
212
+ 'MISSING_REFERENCE_ID',
213
+ `Reference at index ${i} must have an id`,
214
+ `${refPath}.id`,
215
+ ));
216
+ } else {
217
+ if (refIds.has(ref.id)) {
218
+ errors.push(ValidationUtils.error(
219
+ 'DUPLICATE_REFERENCE_ID',
220
+ `Duplicate reference id: "${ref.id}"`,
221
+ `${refPath}.id`,
222
+ ));
223
+ }
224
+ refIds.add(ref.id);
225
+ }
226
+
227
+ if (!ref.title || typeof ref.title !== 'string') {
228
+ errors.push(ValidationUtils.error(
229
+ 'MISSING_REFERENCE_TITLE',
230
+ `Reference at index ${i} must have a title`,
231
+ `${refPath}.title`,
232
+ ));
233
+ }
234
+
235
+ // Validate URL if present
236
+ if (ref.url && !ValidationUtils.isValidURL(ref.url)) {
237
+ warnings.push(ValidationUtils.warning(
238
+ 'INVALID_REFERENCE_URL',
239
+ `Reference "${ref.id}" has an invalid URL`,
240
+ `${refPath}.url`,
241
+ ));
242
+ }
243
+ }
244
+ }
245
+ }
246
+
247
+ // Validate status if specified
248
+ if (metadata.status) {
249
+ if (!VALID_STATUSES.includes(metadata.status)) {
250
+ errors.push(ValidationUtils.error(
251
+ 'INVALID_STATUS',
252
+ `Invalid document status: "${metadata.status}". Must be one of: ${VALID_STATUSES.join(', ')}`,
253
+ 'metadata.status',
254
+ metadata.status,
255
+ ));
256
+ }
257
+ }
258
+
259
+ // Validate revision if specified
260
+ if (metadata.revision !== undefined) {
261
+ if (typeof metadata.revision !== 'number' || metadata.revision < 1 || !Number.isInteger(metadata.revision)) {
262
+ errors.push(ValidationUtils.error(
263
+ 'INVALID_REVISION',
264
+ 'Revision must be a positive integer',
265
+ 'metadata.revision',
266
+ ));
267
+ }
268
+ }
269
+
270
+ // Suggest adding language
271
+ if (!metadata.language) {
272
+ warnings.push(ValidationUtils.warning(
273
+ 'MISSING_LANGUAGE',
274
+ 'Consider specifying the document language',
275
+ 'metadata.language',
276
+ 'Add a language code like "en" for English',
277
+ ));
278
+ }
279
+
280
+ // Suggest adding abstract
281
+ if (!metadata.abstract) {
282
+ warnings.push(ValidationUtils.warning(
283
+ 'MISSING_ABSTRACT',
284
+ 'Consider adding an abstract or summary',
285
+ 'metadata.abstract',
286
+ ));
287
+ }
288
+
289
+ // Check that at least one document resource exists
290
+ const docResources = manifest.resources.filter(r =>
291
+ r.type === 'document' ||
292
+ r.type === 'text' ||
293
+ r.contentType.includes('text/') ||
294
+ r.contentType.includes('markdown') ||
295
+ r.contentType.includes('html') ||
296
+ r.contentType.includes('pdf')
297
+ );
298
+ if (docResources.length === 0) {
299
+ warnings.push(ValidationUtils.warning(
300
+ 'NO_DOCUMENT_RESOURCES',
301
+ 'No document resources found. Ensure resources have appropriate types',
302
+ 'resources',
303
+ ));
304
+ }
305
+
306
+ return errors.length > 0
307
+ ? ValidationUtils.failure(errors, warnings)
308
+ : ValidationUtils.success(warnings);
309
+ }
310
+ }
311
+
@@ -0,0 +1,269 @@
1
+ /**
2
+ * Media Kind Validator
3
+ *
4
+ * Validates manifests for media content (image, audio, video) with format metadata.
5
+ */
6
+
7
+ import { OriginalKind, type OriginalManifest, type ValidationResult, type MediaMetadata } from '../types';
8
+ import { BaseKindValidator, ValidationUtils } from './base';
9
+
10
+ /**
11
+ * Valid media types
12
+ */
13
+ const VALID_MEDIA_TYPES = ['image', 'audio', 'video', '3d', 'animation'];
14
+
15
+ /**
16
+ * Common MIME types by media type
17
+ */
18
+ const COMMON_MIME_TYPES: Record<string, string[]> = {
19
+ image: [
20
+ 'image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/svg+xml',
21
+ 'image/avif', 'image/bmp', 'image/tiff', 'image/heic', 'image/heif',
22
+ ],
23
+ audio: [
24
+ 'audio/mpeg', 'audio/mp3', 'audio/wav', 'audio/ogg', 'audio/flac',
25
+ 'audio/aac', 'audio/webm', 'audio/midi', 'audio/x-wav',
26
+ ],
27
+ video: [
28
+ 'video/mp4', 'video/webm', 'video/ogg', 'video/quicktime', 'video/x-msvideo',
29
+ 'video/x-matroska', 'video/mpeg',
30
+ ],
31
+ '3d': [
32
+ 'model/gltf+json', 'model/gltf-binary', 'model/obj', 'model/stl',
33
+ 'application/octet-stream',
34
+ ],
35
+ animation: [
36
+ 'image/gif', 'video/mp4', 'video/webm', 'application/json', // Lottie
37
+ ],
38
+ };
39
+
40
+ /**
41
+ * Validator for Media Originals
42
+ */
43
+ export class MediaValidator extends BaseKindValidator<OriginalKind.Media> {
44
+ readonly kind = OriginalKind.Media;
45
+
46
+ protected validateKind(manifest: OriginalManifest<OriginalKind.Media>): ValidationResult {
47
+ const errors: ValidationResult['errors'] = [];
48
+ const warnings: ValidationResult['warnings'] = [];
49
+ const metadata = manifest.metadata as MediaMetadata;
50
+
51
+ // Validate metadata exists
52
+ if (!metadata || typeof metadata !== 'object') {
53
+ return ValidationUtils.failure([
54
+ ValidationUtils.error('MISSING_METADATA', 'Media manifest must have metadata', 'metadata'),
55
+ ]);
56
+ }
57
+
58
+ // Validate mediaType (required)
59
+ if (!metadata.mediaType) {
60
+ errors.push(ValidationUtils.error(
61
+ 'MISSING_MEDIA_TYPE',
62
+ 'Media must specify a mediaType',
63
+ 'metadata.mediaType',
64
+ ));
65
+ } else if (!VALID_MEDIA_TYPES.includes(metadata.mediaType)) {
66
+ errors.push(ValidationUtils.error(
67
+ 'INVALID_MEDIA_TYPE',
68
+ `Invalid mediaType: "${metadata.mediaType}". Must be one of: ${VALID_MEDIA_TYPES.join(', ')}`,
69
+ 'metadata.mediaType',
70
+ metadata.mediaType,
71
+ ));
72
+ }
73
+
74
+ // Validate mimeType (required)
75
+ if (!metadata.mimeType || typeof metadata.mimeType !== 'string') {
76
+ errors.push(ValidationUtils.error(
77
+ 'MISSING_MIME_TYPE',
78
+ 'Media must specify a mimeType',
79
+ 'metadata.mimeType',
80
+ ));
81
+ } else if (!ValidationUtils.isValidMimeType(metadata.mimeType)) {
82
+ errors.push(ValidationUtils.error(
83
+ 'INVALID_MIME_TYPE',
84
+ `Invalid mimeType format: "${metadata.mimeType}"`,
85
+ 'metadata.mimeType',
86
+ metadata.mimeType,
87
+ ));
88
+ } else if (metadata.mediaType) {
89
+ // Check if MIME type matches the declared media type
90
+ const expectedMimeTypes = COMMON_MIME_TYPES[metadata.mediaType];
91
+ if (expectedMimeTypes && !expectedMimeTypes.includes(metadata.mimeType)) {
92
+ warnings.push(ValidationUtils.warning(
93
+ 'MIME_TYPE_MISMATCH',
94
+ `mimeType "${metadata.mimeType}" is not typically associated with mediaType "${metadata.mediaType}"`,
95
+ 'metadata.mimeType',
96
+ ));
97
+ }
98
+ }
99
+
100
+ // Validate dimensions for images and video
101
+ if (metadata.mediaType === 'image' || metadata.mediaType === 'video') {
102
+ if (metadata.dimensions) {
103
+ if (typeof metadata.dimensions !== 'object') {
104
+ errors.push(ValidationUtils.error(
105
+ 'INVALID_DIMENSIONS',
106
+ 'Dimensions must be an object',
107
+ 'metadata.dimensions',
108
+ ));
109
+ } else {
110
+ if (typeof metadata.dimensions.width !== 'number' || metadata.dimensions.width <= 0) {
111
+ errors.push(ValidationUtils.error(
112
+ 'INVALID_WIDTH',
113
+ 'Width must be a positive number',
114
+ 'metadata.dimensions.width',
115
+ ));
116
+ }
117
+ if (typeof metadata.dimensions.height !== 'number' || metadata.dimensions.height <= 0) {
118
+ errors.push(ValidationUtils.error(
119
+ 'INVALID_HEIGHT',
120
+ 'Height must be a positive number',
121
+ 'metadata.dimensions.height',
122
+ ));
123
+ }
124
+ }
125
+ } else {
126
+ warnings.push(ValidationUtils.warning(
127
+ 'MISSING_DIMENSIONS',
128
+ `Consider specifying dimensions for ${metadata.mediaType} content`,
129
+ 'metadata.dimensions',
130
+ ));
131
+ }
132
+ }
133
+
134
+ // Validate duration for audio and video
135
+ if (metadata.mediaType === 'audio' || metadata.mediaType === 'video') {
136
+ if (metadata.duration !== undefined) {
137
+ if (typeof metadata.duration !== 'number' || metadata.duration < 0) {
138
+ errors.push(ValidationUtils.error(
139
+ 'INVALID_DURATION',
140
+ 'Duration must be a non-negative number (seconds)',
141
+ 'metadata.duration',
142
+ ));
143
+ }
144
+ } else {
145
+ warnings.push(ValidationUtils.warning(
146
+ 'MISSING_DURATION',
147
+ `Consider specifying duration for ${metadata.mediaType} content`,
148
+ 'metadata.duration',
149
+ ));
150
+ }
151
+ }
152
+
153
+ // Validate frameRate for video
154
+ if (metadata.mediaType === 'video' || metadata.mediaType === 'animation') {
155
+ if (metadata.frameRate !== undefined) {
156
+ if (typeof metadata.frameRate !== 'number' || metadata.frameRate <= 0) {
157
+ errors.push(ValidationUtils.error(
158
+ 'INVALID_FRAME_RATE',
159
+ 'Frame rate must be a positive number',
160
+ 'metadata.frameRate',
161
+ ));
162
+ }
163
+ }
164
+ }
165
+
166
+ // Validate audio-specific fields
167
+ if (metadata.mediaType === 'audio' || metadata.mediaType === 'video') {
168
+ if (metadata.audioChannels !== undefined) {
169
+ if (typeof metadata.audioChannels !== 'number' ||
170
+ metadata.audioChannels <= 0 ||
171
+ !Number.isInteger(metadata.audioChannels)) {
172
+ errors.push(ValidationUtils.error(
173
+ 'INVALID_AUDIO_CHANNELS',
174
+ 'Audio channels must be a positive integer',
175
+ 'metadata.audioChannels',
176
+ ));
177
+ }
178
+ }
179
+
180
+ if (metadata.sampleRate !== undefined) {
181
+ if (typeof metadata.sampleRate !== 'number' || metadata.sampleRate <= 0) {
182
+ errors.push(ValidationUtils.error(
183
+ 'INVALID_SAMPLE_RATE',
184
+ 'Sample rate must be a positive number',
185
+ 'metadata.sampleRate',
186
+ ));
187
+ }
188
+ }
189
+ }
190
+
191
+ // Validate bitrate if specified
192
+ if (metadata.bitrate !== undefined) {
193
+ if (typeof metadata.bitrate !== 'number' || metadata.bitrate <= 0) {
194
+ errors.push(ValidationUtils.error(
195
+ 'INVALID_BITRATE',
196
+ 'Bitrate must be a positive number (kbps)',
197
+ 'metadata.bitrate',
198
+ ));
199
+ }
200
+ }
201
+
202
+ // Validate thumbnail if specified
203
+ if (metadata.thumbnail) {
204
+ if (typeof metadata.thumbnail !== 'string') {
205
+ errors.push(ValidationUtils.error(
206
+ 'INVALID_THUMBNAIL',
207
+ 'Thumbnail must be a string (resource ID)',
208
+ 'metadata.thumbnail',
209
+ ));
210
+ } else if (!ValidationUtils.resourceExists(metadata.thumbnail, manifest.resources)) {
211
+ warnings.push(ValidationUtils.warning(
212
+ 'THUMBNAIL_NOT_RESOURCE',
213
+ `Thumbnail "${metadata.thumbnail}" does not match a resource ID`,
214
+ 'metadata.thumbnail',
215
+ ));
216
+ }
217
+ }
218
+
219
+ // Validate preview if specified
220
+ if (metadata.preview) {
221
+ if (typeof metadata.preview !== 'string') {
222
+ errors.push(ValidationUtils.error(
223
+ 'INVALID_PREVIEW',
224
+ 'Preview must be a string (resource ID)',
225
+ 'metadata.preview',
226
+ ));
227
+ } else if (!ValidationUtils.resourceExists(metadata.preview, manifest.resources)) {
228
+ warnings.push(ValidationUtils.warning(
229
+ 'PREVIEW_NOT_RESOURCE',
230
+ `Preview "${metadata.preview}" does not match a resource ID`,
231
+ 'metadata.preview',
232
+ ));
233
+ }
234
+ }
235
+
236
+ // Suggest adding alt text for accessibility
237
+ if (!metadata.altText && metadata.mediaType === 'image') {
238
+ warnings.push(ValidationUtils.warning(
239
+ 'MISSING_ALT_TEXT',
240
+ 'Consider adding altText for accessibility',
241
+ 'metadata.altText',
242
+ ));
243
+ }
244
+
245
+ // Check that at least one media resource exists
246
+ const mediaResources = manifest.resources.filter(r =>
247
+ r.type === 'media' ||
248
+ r.type === 'image' ||
249
+ r.type === 'audio' ||
250
+ r.type === 'video' ||
251
+ r.contentType.startsWith('image/') ||
252
+ r.contentType.startsWith('audio/') ||
253
+ r.contentType.startsWith('video/') ||
254
+ r.contentType.startsWith('model/')
255
+ );
256
+ if (mediaResources.length === 0) {
257
+ warnings.push(ValidationUtils.warning(
258
+ 'NO_MEDIA_RESOURCES',
259
+ 'No media resources found. Ensure resources have appropriate types',
260
+ 'resources',
261
+ ));
262
+ }
263
+
264
+ return errors.length > 0
265
+ ? ValidationUtils.failure(errors, warnings)
266
+ : ValidationUtils.success(warnings);
267
+ }
268
+ }
269
+