catalyst-core 0.0.3-canary.9 → 0.1.0-beta.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 (182) hide show
  1. package/README.md +4 -5
  2. package/bin/catalyst.js +28 -15
  3. package/changelog.md +139 -30
  4. package/dist/native/androidProject/app/build.gradle.kts +125 -15
  5. package/dist/native/androidProject/app/proguard-rules.pro +85 -14
  6. package/dist/native/androidProject/app/src/debug/res/xml/network_security_config.xml +8 -0
  7. package/dist/native/androidProject/app/src/main/AndroidManifest.xml +10 -3
  8. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/BridgeMessageValidator.kt +552 -0
  9. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/CatalystConstants.kt +109 -0
  10. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/CustomWebview.kt +605 -124
  11. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/MainActivity.kt +405 -47
  12. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/MetricsMonitor.kt +341 -0
  13. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/NativeBridge.kt +954 -71
  14. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/SplashActivity.kt +111 -0
  15. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/URLWhitelistManager.kt +231 -0
  16. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/Utils.kt +49 -55
  17. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/WebCacheManager.kt +81 -8
  18. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/design/DesignTokens.kt +307 -0
  19. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/security/EmulatorDetector.kt +203 -0
  20. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/security/FridaDetector.kt +187 -0
  21. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/security/SecurityAlertHandler.kt +170 -0
  22. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/security/SecurityAlertUI.kt +366 -0
  23. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/security/SecurityBottomSheet.kt +138 -0
  24. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/security/SecurityCheckManager.kt +511 -0
  25. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/security/SecurityCheckScheduler.kt +68 -0
  26. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/BridgeUtils.kt +47 -11
  27. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/DeviceInfoUtils.kt +38 -13
  28. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/DownloadUtils.kt +36 -14
  29. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/FileUtils.kt +3 -2
  30. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/FrameworkServerUtils.kt +57 -8
  31. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/IntentUtils.kt +31 -6
  32. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/KeyboardUtil.kt +82 -0
  33. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/NetworkUtils.kt +105 -0
  34. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/NotificationConstants.kt +28 -0
  35. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/NotificationUtils.kt +639 -0
  36. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/SafeAreaUtils.kt +62 -0
  37. package/dist/native/androidProject/app/src/main/res/drawable/splashscreen_fallback.png +0 -0
  38. package/dist/native/androidProject/app/src/main/res/layout/activity_main.xml +1 -0
  39. package/dist/native/androidProject/app/src/main/res/layout/activity_splash.xml +20 -0
  40. package/dist/native/androidProject/app/src/main/res/values/themes.xml +23 -0
  41. package/dist/native/androidProject/app/src/main/res/values-night/themes.xml +11 -0
  42. package/dist/native/androidProject/app/src/main/res/xml/network_security_config.xml +12 -0
  43. package/dist/native/androidProject/app/src/noFcm/java/io/yourname/androidproject/utils/NotificationManager.kt +166 -0
  44. package/dist/native/androidProject/app/src/noFcm/java/io/yourname/androidproject/utils/PushNotificationUtils.kt +94 -0
  45. package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/BridgeMessageValidatorTest.kt +267 -0
  46. package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/BridgeUtilsTest.kt +286 -0
  47. package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/CameraUtilsTest.kt +296 -0
  48. package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/ConfigMappingTest.kt +594 -0
  49. package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/CustomWebviewTest.kt +466 -0
  50. package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/DeviceInfoUtilsTest.kt +187 -0
  51. package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/FileUtilsTest.kt +304 -0
  52. package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/NativeBridgeTest.kt +424 -0
  53. package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/NotificationUtilsTest.kt +469 -0
  54. package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/SecurityBridgeTest.kt +199 -0
  55. package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/URLWhitelistManagerTest.kt +462 -0
  56. package/dist/native/androidProject/app/src/test/java/io/yourname/androidproject/WebCacheManagerTest.kt +391 -0
  57. package/dist/native/androidProject/app/src/withFcm/java/io/yourname/androidproject/utils/NotificationManager.kt +224 -0
  58. package/dist/native/androidProject/app/src/withFcm/java/io/yourname/androidproject/utils/PushNotificationUtils.kt +399 -0
  59. package/dist/native/androidProject/build.gradle.kts +6 -0
  60. package/dist/native/androidProject/gradle/libs.versions.toml +8 -2
  61. package/dist/native/androidSetup.js +4 -3
  62. package/dist/native/assets/catalyst.png +0 -0
  63. package/dist/native/bridge/WebBridge.js +16 -3
  64. package/dist/native/bridge/constants/NativeInterfaces.js +28 -10
  65. package/dist/native/bridge/errors.js +1 -1
  66. package/dist/native/bridge/hooks.js +76 -16
  67. package/dist/native/bridge/safeArea.js +5 -0
  68. package/dist/native/bridge/standardizedHooks.js +23 -5
  69. package/dist/native/bridge/useBaseHook.js +7 -9
  70. package/dist/native/bridge/utils/CameraUtils.js +58 -0
  71. package/dist/native/bridge/utils/FileObjectConverter.js +40 -0
  72. package/dist/native/bridge/utils/NativeBridge.js +57 -8
  73. package/dist/native/buildAppAndroid.js +115 -43
  74. package/dist/native/buildAppIos.js +291 -26
  75. package/dist/native/iosnativeWebView/Configurations/Debug.xcconfig +21 -0
  76. package/dist/native/iosnativeWebView/Configurations/Release.xcconfig +24 -0
  77. package/dist/native/iosnativeWebView/Configurations/Shared.xcconfig +50 -0
  78. package/dist/native/iosnativeWebView/Package.resolved +32 -0
  79. package/dist/native/iosnativeWebView/Package.swift +37 -0
  80. package/dist/native/iosnativeWebView/Sources/CatalystNotifications/LocalNotificationHandler.swift +293 -0
  81. package/dist/native/iosnativeWebView/Sources/CatalystNotifications/NotificationManager.swift +299 -0
  82. package/dist/native/iosnativeWebView/Sources/CatalystNotifications/PushNotificationHandler.swift +199 -0
  83. package/dist/native/iosnativeWebView/Sources/Core/Constants/CatalystConstants.swift +125 -0
  84. package/dist/native/iosnativeWebView/Sources/Core/Constants/ConfigConstants.swift +70 -0
  85. package/dist/native/iosnativeWebView/Sources/Core/Notifications/NotificationModels.swift +156 -0
  86. package/dist/native/iosnativeWebView/Sources/Core/Protocols/NotificationHandlerProtocol.swift +35 -0
  87. package/dist/native/iosnativeWebView/Sources/Core/Utils/BootTimingUtility.swift +26 -0
  88. package/dist/native/iosnativeWebView/{iosnativeWebView → Sources/Core/Utils}/CacheManager.swift +69 -36
  89. package/dist/native/iosnativeWebView/Sources/Core/Utils/CameraTransportUtils.swift +363 -0
  90. package/dist/native/iosnativeWebView/Sources/Core/Utils/DeviceInfoUtils.swift +67 -0
  91. package/dist/native/iosnativeWebView/Sources/Core/Utils/FilePickerHandler.swift +1053 -0
  92. package/dist/native/iosnativeWebView/Sources/Core/Utils/FrameworkServerUtils.swift +901 -0
  93. package/dist/native/iosnativeWebView/{iosnativeWebView → Sources/Core/Utils}/ImageHandler.swift +206 -20
  94. package/dist/native/iosnativeWebView/Sources/Core/Utils/NetworkMonitor.swift +112 -0
  95. package/dist/native/iosnativeWebView/Sources/Core/Utils/NotificationHandlerProvider.swift +20 -0
  96. package/dist/native/iosnativeWebView/Sources/Core/Utils/NullNotificationHandler.swift +85 -0
  97. package/dist/native/iosnativeWebView/{iosnativeWebView → Sources/Core/Utils}/ResourceURLProtocol.swift +60 -25
  98. package/dist/native/iosnativeWebView/Sources/Core/Utils/SafeAreaUtils.swift +132 -0
  99. package/dist/native/iosnativeWebView/Sources/Core/Utils/ScreenSecureManager.swift +131 -0
  100. package/dist/native/iosnativeWebView/Sources/Core/Utils/URLWhitelistManager.swift +224 -0
  101. package/dist/native/iosnativeWebView/Sources/Core/Utils/WebKitConfig.swift +43 -0
  102. package/dist/native/iosnativeWebView/Sources/Core/WebView/BridgeCommandHandler.swift +991 -0
  103. package/dist/native/iosnativeWebView/Sources/Core/WebView/BridgeDelegateHandler.swift +166 -0
  104. package/dist/native/iosnativeWebView/Sources/Core/WebView/BridgeFileHandler.swift +340 -0
  105. package/dist/native/iosnativeWebView/Sources/Core/WebView/BridgeJavaScriptInterface.swift +149 -0
  106. package/dist/native/iosnativeWebView/Sources/Core/WebView/BridgeMessageValidator.swift +611 -0
  107. package/dist/native/iosnativeWebView/Sources/Core/WebView/NativeBridge.swift +334 -0
  108. package/dist/native/iosnativeWebView/Sources/Core/WebView/SplashView.swift +95 -0
  109. package/dist/native/iosnativeWebView/Sources/Core/WebView/SplashViewModel.swift +86 -0
  110. package/dist/native/iosnativeWebView/Sources/Core/WebView/WebView.swift +181 -0
  111. package/dist/native/iosnativeWebView/Sources/Core/WebView/WebViewModel.swift +182 -0
  112. package/dist/native/iosnativeWebView/Sources/Core/WebView/WebViewNavigationDelegate.swift +395 -0
  113. package/dist/native/iosnativeWebView/iosnativeWebView/AppDelegate.swift +84 -0
  114. package/dist/native/iosnativeWebView/iosnativeWebView/Assets.xcassets/AccentColor.colorset/Contents.json +8 -8
  115. package/dist/native/iosnativeWebView/iosnativeWebView/Assets.xcassets/AppIcon.appiconset/Contents.json +10 -10
  116. package/dist/native/iosnativeWebView/iosnativeWebView/Assets.xcassets/Contents.json +4 -4
  117. package/dist/native/iosnativeWebView/iosnativeWebView/ContentView.swift +65 -10
  118. package/dist/native/iosnativeWebView/iosnativeWebView/Info-Release.plist +21 -0
  119. package/dist/native/iosnativeWebView/iosnativeWebView/Info.plist +13 -0
  120. package/dist/native/iosnativeWebView/iosnativeWebView/Preview Content/Preview Assets.xcassets/Contents.json +4 -4
  121. package/dist/native/iosnativeWebView/iosnativeWebView/iosnativeWebView.entitlements +12 -0
  122. package/dist/native/iosnativeWebView/iosnativeWebView/iosnativeWebViewApp.swift +80 -0
  123. package/dist/native/iosnativeWebView/iosnativeWebView/localhost.p12 +0 -0
  124. package/dist/native/iosnativeWebView/iosnativeWebView.xcodeproj/project.pbxproj +118 -100
  125. package/dist/native/iosnativeWebView/iosnativeWebView.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +69 -0
  126. package/dist/native/iosnativeWebView/iosnativeWebView.xcodeproj/xcshareddata/xcschemes/iosnativeWebView.xcscheme +9 -4
  127. package/dist/native/iosnativeWebView/iosnativeWebView.xctestplan +47 -0
  128. package/dist/native/iosnativeWebView/iosnativeWebViewTests/BootTimingUtilityTests.swift +236 -0
  129. package/dist/native/iosnativeWebView/iosnativeWebViewTests/BridgeCommandHandlerSecurityTests.swift +212 -0
  130. package/dist/native/iosnativeWebView/iosnativeWebViewTests/BridgeMessageValidatorTests.swift +418 -0
  131. package/dist/native/iosnativeWebView/iosnativeWebViewTests/CacheManagerTests.swift +390 -0
  132. package/dist/native/iosnativeWebView/iosnativeWebViewTests/ConfigMappingTests.swift +299 -0
  133. package/dist/native/iosnativeWebView/iosnativeWebViewTests/FilePickerHandlerTests.swift +383 -0
  134. package/dist/native/iosnativeWebView/iosnativeWebViewTests/FrameworkServerUtilsTests.swift +391 -0
  135. package/dist/native/iosnativeWebView/iosnativeWebViewTests/NativeBridgeTests.swift +581 -0
  136. package/dist/native/iosnativeWebView/iosnativeWebViewTests/NotificationHandlerTests.swift +430 -0
  137. package/dist/native/iosnativeWebView/iosnativeWebViewTests/ScreenSecureManagerTests.swift +121 -0
  138. package/dist/native/iosnativeWebView/iosnativeWebViewTests/URLWhitelistManagerTests.swift +367 -0
  139. package/dist/native/iosnativeWebView/iosnativeWebViewTests/WebViewTests.swift +440 -0
  140. package/dist/native/renameAndroidProject.js +80 -70
  141. package/dist/native/setupEmulatorIos.js +22 -5
  142. package/dist/native/setupServer.js +11 -10
  143. package/dist/native/terminalProgress.js +10 -10
  144. package/dist/native/utils.js +4 -5
  145. package/dist/otel.js +4 -0
  146. package/dist/scripts/build.js +2 -2
  147. package/dist/scripts/devBuild.js +2 -2
  148. package/dist/scripts/loadEnvironmentVariables.js +3 -1
  149. package/dist/scripts/scriptUtils.js +2 -2
  150. package/dist/sentry.js +4 -1
  151. package/dist/server/renderer/document/Body.js +4 -4
  152. package/dist/server/renderer/handler.js +7 -5
  153. package/dist/webpack/development.client.babel.js +1 -1
  154. package/mcp_v2/conversion-tasks.json +326 -0
  155. package/mcp_v2/knowledge-base.json +1068 -0
  156. package/mcp_v2/lib/helpers.js +165 -0
  157. package/mcp_v2/mcp.js +563 -0
  158. package/mcp_v2/package.json +13 -0
  159. package/mcp_v2/schema.sql +88 -0
  160. package/mcp_v2/setup.js +278 -0
  161. package/mcp_v2/tools/build.js +686 -0
  162. package/mcp_v2/tools/config.js +453 -0
  163. package/mcp_v2/tools/conversion.js +799 -0
  164. package/mcp_v2/tools/debug.js +113 -0
  165. package/mcp_v2/tools/knowledge.js +219 -0
  166. package/mcp_v2/tools/sync.js +23 -0
  167. package/mcp_v2/tools/tasks.js +945 -0
  168. package/package.json +11 -15
  169. package/dist/native/androidProject/.idea/.name +0 -1
  170. package/dist/native/androidProject/.idea/AndroidProjectSystem.xml +0 -6
  171. package/dist/native/androidProject/.idea/compiler.xml +0 -6
  172. package/dist/native/androidProject/.idea/deploymentTargetSelector.xml +0 -10
  173. package/dist/native/androidProject/.idea/gradle.xml +0 -18
  174. package/dist/native/androidProject/.idea/migrations.xml +0 -10
  175. package/dist/native/androidProject/.idea/misc.xml +0 -10
  176. package/dist/native/androidProject/.idea/runConfigurations.xml +0 -17
  177. package/dist/native/androidProject/.idea/vcs.xml +0 -6
  178. package/dist/native/iosnativeWebView/iosnativeWebView/NativeBridge.swift +0 -217
  179. package/dist/native/iosnativeWebView/iosnativeWebView/WebView.swift +0 -96
  180. package/dist/native/iosnativeWebView/iosnativeWebView/WebViewModel.swift +0 -47
  181. package/dist/native/iosnativeWebView/iosnativeWebView/WebViewNavigationDelegate.swift +0 -140
  182. package/license +0 -10
@@ -0,0 +1,611 @@
1
+ //
2
+ // BridgeMessageValidator.swift
3
+ // iosnativeWebView
4
+ //
5
+ // Bridge message validation using JSONSchema library
6
+ // Extracted from NativeBridge.swift for better separation of concerns
7
+ //
8
+
9
+ import Foundation
10
+ import WebKit
11
+ import JSONSchema
12
+ import os
13
+
14
+ private let logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "com.app", category: CatalystConstants.Logging.Categories.messageValidator)
15
+
16
+ // MARK: - Validation Result
17
+
18
+ struct BridgeValidationResult {
19
+ let isValid: Bool
20
+ let command: String?
21
+ let params: Any?
22
+ let body: [String: Any]?
23
+ let error: BridgeValidationError?
24
+ }
25
+
26
+ struct BridgeValidationError {
27
+ let message: String
28
+ let code: String
29
+ let eventName: String
30
+ }
31
+
32
+ // MARK: - Bridge Message Validator
33
+
34
+ class BridgeMessageValidator {
35
+
36
+ // MARK: - Schema Definitions
37
+
38
+ private static var schemas: [String: [String: Any]] = [
39
+ "openCamera": [
40
+ "type": "object",
41
+ "properties": [
42
+ "quality": [
43
+ "type": "string",
44
+ "enum": ["high", "medium", "low"]
45
+ ],
46
+ "allowsEditing": ["type": "boolean"],
47
+ "preferredCameraType": [
48
+ "type": "string",
49
+ "enum": ["front", "back"]
50
+ ],
51
+ "flashMode": [
52
+ "type": "string",
53
+ "enum": ["auto", "on", "off"]
54
+ ],
55
+ "videoMaximumDuration": [
56
+ "type": "number",
57
+ "minimum": 0,
58
+ "maximum": 3600
59
+ ]
60
+ ],
61
+ "additionalProperties": false
62
+ ],
63
+ "requestCameraPermission": [
64
+ "type": "object",
65
+ "properties": [
66
+ "showRationale": ["type": "boolean"],
67
+ "fallbackToSettings": ["type": "boolean"],
68
+ "includeDetails": ["type": "boolean"]
69
+ ],
70
+ "additionalProperties": false
71
+ ],
72
+ "pickFile": [
73
+ "type": "object",
74
+ "properties": [
75
+ "mimeType": ["type": "string"],
76
+ "multiple": ["type": "boolean"],
77
+ "minFileSize": [
78
+ "type": "number",
79
+ "minimum": 0
80
+ ],
81
+ "maxFileSize": [
82
+ "type": "number",
83
+ "minimum": 0
84
+ ],
85
+ "minFiles": [
86
+ "type": "number",
87
+ "minimum": 1
88
+ ],
89
+ "maxFiles": [
90
+ "type": "number",
91
+ "minimum": 1
92
+ ]
93
+ ],
94
+ "additionalProperties": false
95
+ ],
96
+ "requestHapticFeedback": [
97
+ "type": "object",
98
+ "properties": [
99
+ "type": [
100
+ "type": "string",
101
+ "enum": ["light", "medium", "heavy", "selection", "impact", "notification", "VIRTUAL_KEY", "LONG_PRESS", "DEFAULT"]
102
+ ],
103
+ "intensity": [
104
+ "type": "number",
105
+ "minimum": 0,
106
+ "maximum": 1
107
+ ]
108
+ ],
109
+ "required": ["type"],
110
+ "additionalProperties": false
111
+ ],
112
+ "openFileWithIntent": [
113
+ "type": "object",
114
+ "properties": [
115
+ "url": ["type": "string"],
116
+ "filename": ["type": "string"],
117
+ "mimeType": ["type": "string"],
118
+ "data": ["type": "string"]
119
+ ],
120
+ "additionalProperties": false
121
+ ],
122
+ "getDeviceInfo": [
123
+ "type": "object",
124
+ "properties": [:],
125
+ "additionalProperties": false
126
+ ],
127
+ "getNetworkStatus": [
128
+ "type": "object",
129
+ "properties": [:],
130
+ "additionalProperties": false
131
+ ],
132
+ "logger": [
133
+ "type": "object",
134
+ "properties": [:],
135
+ "additionalProperties": false
136
+ ],
137
+ "googleSignIn": [
138
+ "type": "object",
139
+ "properties": [
140
+ "clientId": ["type": "string"],
141
+ "webClientId": ["type": "string"],
142
+ "nonce": ["type": "string"],
143
+ "hint": ["type": "string"],
144
+ "email": ["type": "string"],
145
+ "autoSelect": ["type": "boolean"],
146
+ "filterByAuthorizedAccounts": ["type": "boolean"],
147
+ "additionalScopes": [
148
+ "type": "array",
149
+ "items": ["type": "string"]
150
+ ]
151
+ ],
152
+ "additionalProperties": false
153
+ ],
154
+ // Notification commands
155
+ "requestNotificationPermission": [
156
+ "type": "object",
157
+ "properties": [:],
158
+ "additionalProperties": false
159
+ ],
160
+ "scheduleLocalNotification": [
161
+ "type": "object",
162
+ "properties": [
163
+ "title": ["type": "string"],
164
+ "body": ["type": "string"],
165
+ "badge": ["type": "number"],
166
+ "sound": ["type": "string"],
167
+ "data": ["type": "object"],
168
+ "triggerTime": ["type": "number"],
169
+ "notificationId": ["type": "string"]
170
+ ],
171
+ "additionalProperties": false
172
+ ],
173
+ "cancelLocalNotification": [
174
+ "type": "object",
175
+ "properties": [
176
+ "notificationId": ["type": "string"]
177
+ ],
178
+ "required": ["notificationId"],
179
+ "additionalProperties": false
180
+ ],
181
+ "registerForPushNotifications": [
182
+ "type": "object",
183
+ "properties": [:],
184
+ "additionalProperties": false
185
+ ],
186
+ "subscribeToTopic": [
187
+ "type": "object",
188
+ "properties": [
189
+ "topic": ["type": "string"]
190
+ ],
191
+ "required": ["topic"],
192
+ "additionalProperties": false
193
+ ],
194
+ "unsubscribeFromTopic": [
195
+ "type": "object",
196
+ "properties": [
197
+ "topic": ["type": "string"]
198
+ ],
199
+ "required": ["topic"],
200
+ "additionalProperties": false
201
+ ],
202
+ "getSubscribedTopics": [
203
+ "type": "object",
204
+ "properties": [:],
205
+ "additionalProperties": false
206
+ ],
207
+ "getSafeArea": [
208
+ "type": "object",
209
+ "properties": [:],
210
+ "additionalProperties": false
211
+ ]
212
+ ]
213
+
214
+
215
+ // MARK: - Public Validation Interface
216
+
217
+ static func validate(message: WKScriptMessage) -> BridgeValidationResult {
218
+ logger.debug("Starting validation for bridge message")
219
+
220
+ // Step 1: Validate message handler name
221
+ guard message.name == "NativeBridge" else {
222
+ logger.error("Invalid message handler name: \(message.name)")
223
+ return BridgeValidationResult(
224
+ isValid: false,
225
+ command: nil,
226
+ params: nil,
227
+ body: nil,
228
+ error: BridgeValidationError(
229
+ message: "Invalid message handler",
230
+ code: "INVALID_HANDLER",
231
+ eventName: "BRIDGE_ERROR"
232
+ )
233
+ )
234
+ }
235
+
236
+ // Step 2: Validate message size
237
+ if let messageData = try? JSONSerialization.data(withJSONObject: message.body, options: []),
238
+ messageData.count > CatalystConstants.Bridge.maxMessageSize {
239
+ logger.error("Message size exceeds limit: \(messageData.count) > \(CatalystConstants.Bridge.maxMessageSize)")
240
+ return BridgeValidationResult(
241
+ isValid: false,
242
+ command: nil,
243
+ params: nil,
244
+ body: nil,
245
+ error: BridgeValidationError(
246
+ message: "Message too large",
247
+ code: "MESSAGE_TOO_LARGE",
248
+ eventName: "BRIDGE_ERROR"
249
+ )
250
+ )
251
+ }
252
+
253
+ // Step 3: Validate message body structure
254
+ guard let body = message.body as? [String: Any] else {
255
+ logger.error("Invalid message format - body is not a dictionary")
256
+ return BridgeValidationResult(
257
+ isValid: false,
258
+ command: nil,
259
+ params: nil,
260
+ body: nil,
261
+ error: BridgeValidationError(
262
+ message: "Invalid message format",
263
+ code: "INVALID_FORMAT",
264
+ eventName: "BRIDGE_ERROR"
265
+ )
266
+ )
267
+ }
268
+
269
+ // Step 4: Validate root message structure
270
+ guard let rootValidationResult = validateRootMessageStructure(body) else {
271
+ return BridgeValidationResult(
272
+ isValid: false,
273
+ command: nil,
274
+ params: nil,
275
+ body: body,
276
+ error: BridgeValidationError(
277
+ message: "Invalid root message structure",
278
+ code: "INVALID_ROOT_STRUCTURE",
279
+ eventName: "BRIDGE_ERROR"
280
+ )
281
+ )
282
+ }
283
+
284
+ let command = rootValidationResult.command
285
+ let params = rootValidationResult.params
286
+
287
+ // Step 5: Validate command is supported
288
+ guard CatalystConstants.Bridge.validCommands.contains(command) else {
289
+ logger.error("Unsupported command: \(command)")
290
+ print("❌ DEBUG [BridgeMessageValidator]: Command '\(command)' is NOT in valid commands")
291
+ print("❌ DEBUG [BridgeMessageValidator]: Available commands: \(CatalystConstants.Bridge.validCommands.sorted().joined(separator: ", "))")
292
+ return BridgeValidationResult(
293
+ isValid: false,
294
+ command: command,
295
+ params: params,
296
+ body: body,
297
+ error: BridgeValidationError(
298
+ message: "Unsupported command: \(command)",
299
+ code: "UNSUPPORTED_COMMAND",
300
+ eventName: "BRIDGE_ERROR"
301
+ )
302
+ )
303
+ }
304
+
305
+ // Step 6: Validate command-specific parameters using JSONSchema
306
+ if let params = params {
307
+ if let validationError = validateCommandParameters(command: command, params: params) {
308
+ return BridgeValidationResult(
309
+ isValid: false,
310
+ command: command,
311
+ params: params,
312
+ body: body,
313
+ error: validationError
314
+ )
315
+ }
316
+ }
317
+
318
+ logger.debug("Message validation successful for command: \(command)")
319
+ return BridgeValidationResult(
320
+ isValid: true,
321
+ command: command,
322
+ params: params,
323
+ body: body,
324
+ error: nil
325
+ )
326
+ }
327
+
328
+ // MARK: - Private Validation Helpers
329
+
330
+ private static func validateRootMessageStructure(_ body: [String: Any]) -> (command: String, params: Any?)? {
331
+ // Validate required command field
332
+ guard let command = body["command"] as? String, !command.isEmpty else {
333
+ logger.error("Root message missing 'command' field")
334
+ return nil
335
+ }
336
+
337
+ // Optional: Validate timestamp if present
338
+ if let timestamp = body["timestamp"] as? String {
339
+ let formatter = ISO8601DateFormatter()
340
+ if formatter.date(from: timestamp) == nil {
341
+ logger.warning("Invalid timestamp format in message: \(timestamp)")
342
+ }
343
+ }
344
+
345
+ // Optional: Validate requestId if present (for tracking)
346
+ if let requestId = body["requestId"] as? String, requestId.isEmpty {
347
+ logger.warning("Empty requestId in message")
348
+ }
349
+
350
+ let params = body["data"]
351
+ return (command: command, params: params)
352
+ }
353
+
354
+ private static func validateCommandParameters(command: String, params: Any?) -> BridgeValidationError? {
355
+ // Commands that support flexible parameter formats (string or object)
356
+ let flexibleCommands = [
357
+ "openCamera",
358
+ "requestCameraPermission",
359
+ "pickFile",
360
+ "requestHapticFeedback",
361
+ "openFileWithIntent",
362
+ "getDeviceInfo",
363
+ "getNetworkStatus",
364
+ "logger",
365
+ "googleSignIn",
366
+ "getSubscribedTopics",
367
+ "requestNotificationPermission",
368
+ "registerForPushNotifications",
369
+ "scheduleLocalNotification",
370
+ "cancelLocalNotification",
371
+ "subscribeToTopic",
372
+ "unsubscribeFromTopic",
373
+ "getSafeArea"
374
+ ]
375
+
376
+ if flexibleCommands.contains(command) {
377
+ // For these commands, allow string, object, or nil parameters
378
+ if params is String || params == nil {
379
+ logger.debug("Command '\(command)' received string/nil parameters - allowing for legacy compatibility")
380
+ return nil
381
+ }
382
+
383
+ // If it's an object, validate against schema if available
384
+ if let paramsDict = params as? [String: Any] {
385
+ guard schemas[command] != nil else {
386
+ logger.debug("No schema defined for command: \(command), allowing object parameters")
387
+ return nil
388
+ }
389
+
390
+ return validateObjectAgainstJSONSchema(paramsDict, schema: nil, commandName: command)
391
+ }
392
+
393
+ if let params {
394
+ logger.warning("Command '\(command)' received unexpected parameter type: \(type(of: params))")
395
+ }
396
+ return nil // Allow for backward compatibility
397
+ }
398
+
399
+ // For strict commands, require object parameters and validate against schema
400
+ guard schemas[command] != nil else {
401
+ logger.debug("No schema defined for command: \(command), allowing all parameters")
402
+ return nil
403
+ }
404
+
405
+ guard let paramsDict = params as? [String: Any] else {
406
+ logger.error("Parameters for command '\(command)' must be an object")
407
+ return BridgeValidationError(
408
+ message: "Invalid parameters format for \(command)",
409
+ code: "INVALID_PARAMS",
410
+ eventName: "BRIDGE_ERROR"
411
+ )
412
+ }
413
+
414
+ return validateObjectAgainstJSONSchema(paramsDict, schema: nil, commandName: command)
415
+ }
416
+
417
+ private static func validateObjectAgainstJSONSchema(_ object: [String: Any], schema: Any?, commandName: String) -> BridgeValidationError? {
418
+ // Get the schema dictionary for the command
419
+ guard let schemaDict = schemas[commandName] else {
420
+ logger.warning("No schema dictionary found for command: \(commandName)")
421
+ return nil
422
+ }
423
+
424
+ do {
425
+ // Use JSONSchema.validate with the schema dictionary and object
426
+ let result = try JSONSchema.validate(object, schema: schemaDict)
427
+
428
+ if result.valid {
429
+ logger.debug("Parameter validation successful for command: \(commandName)")
430
+ if commandName == "pickFile" {
431
+ if let error = validatePickFileConstraints(object) {
432
+ return error
433
+ }
434
+ }
435
+ return nil
436
+ } else {
437
+ let errorMessages = result.errors?.map { $0.description }.joined(separator: "; ") ?? "Unknown validation error"
438
+ logger.error("Parameter validation failed for command '\(commandName)': \(errorMessages)")
439
+ return BridgeValidationError(
440
+ message: "Invalid parameters for \(commandName): \(errorMessages)",
441
+ code: "INVALID_PARAMS",
442
+ eventName: "BRIDGE_ERROR"
443
+ )
444
+ }
445
+ } catch {
446
+ logger.error("Failed to validate parameters: \(error.localizedDescription)")
447
+ return BridgeValidationError(
448
+ message: "Parameter validation failed for \(commandName)",
449
+ code: "PARAM_VALIDATION_ERROR",
450
+ eventName: "BRIDGE_ERROR"
451
+ )
452
+ }
453
+ }
454
+
455
+ private static func validateObjectAgainstSchema(_ object: [String: Any], schema: [String: Any], commandName: String) -> BridgeValidationError? {
456
+ // Validate object type
457
+ if let schemaType = schema["type"] as? String, schemaType != "object" {
458
+ logger.warning("Schema for \(commandName) is not of type 'object'")
459
+ }
460
+
461
+ let allowAdditionalProperties = schema["additionalProperties"] as? Bool ?? true
462
+ let schemaProperties = schema["properties"] as? [String: [String: Any]] ?? [:]
463
+ let requiredFields = schema["required"] as? [String] ?? []
464
+
465
+ // Validate required fields
466
+ for requiredField in requiredFields {
467
+ if object[requiredField] == nil {
468
+ logger.error("Required field '\(requiredField)' missing for command \(commandName)")
469
+ return BridgeValidationError(
470
+ message: "Missing required field: \(requiredField)",
471
+ code: "MISSING_REQUIRED_FIELD",
472
+ eventName: "BRIDGE_ERROR"
473
+ )
474
+ }
475
+ }
476
+
477
+ // Validate each property
478
+ for (key, value) in object {
479
+ if let propertySchema = schemaProperties[key] {
480
+ if let error = validatePropertyAgainstSchema(value, schema: propertySchema, propertyName: key, commandName: commandName) {
481
+ return error
482
+ }
483
+ } else if !allowAdditionalProperties {
484
+ logger.error("Additional property '\(key)' not allowed for command \(commandName)")
485
+ return BridgeValidationError(
486
+ message: "Additional property '\(key)' not allowed",
487
+ code: "ADDITIONAL_PROPERTY_NOT_ALLOWED",
488
+ eventName: "BRIDGE_ERROR"
489
+ )
490
+ }
491
+ }
492
+
493
+ return nil
494
+ }
495
+
496
+ private static func validatePropertyAgainstSchema(_ value: Any, schema: [String: Any], propertyName: String, commandName: String) -> BridgeValidationError? {
497
+ guard let expectedType = schema["type"] as? String else {
498
+ logger.error("No type specified in schema for property \(propertyName)")
499
+ return nil
500
+ }
501
+
502
+ switch expectedType {
503
+ case "string":
504
+ guard value is String else {
505
+ logger.error("Property '\(propertyName)' must be a string for command \(commandName)")
506
+ return BridgeValidationError(
507
+ message: "Property '\(propertyName)' must be a string",
508
+ code: "INVALID_TYPE",
509
+ eventName: "BRIDGE_ERROR"
510
+ )
511
+ }
512
+ if let enumValues = schema["enum"] as? [String], let stringValue = value as? String {
513
+ if !enumValues.contains(stringValue) {
514
+ logger.error("Property '\(propertyName)' value '\(stringValue)' not in allowed enum values")
515
+ return BridgeValidationError(
516
+ message: "Invalid value for \(propertyName): \(stringValue)",
517
+ code: "INVALID_ENUM_VALUE",
518
+ eventName: "BRIDGE_ERROR"
519
+ )
520
+ }
521
+ }
522
+
523
+ case "number":
524
+ guard let numberValue = value as? NSNumber else {
525
+ logger.error("Property '\(propertyName)' must be a number for command \(commandName)")
526
+ return BridgeValidationError(
527
+ message: "Property '\(propertyName)' must be a number",
528
+ code: "INVALID_TYPE",
529
+ eventName: "BRIDGE_ERROR"
530
+ )
531
+ }
532
+ if let minimum = schema["minimum"] as? NSNumber, numberValue.doubleValue < minimum.doubleValue {
533
+ logger.error("Property '\(propertyName)' value \(numberValue) is below minimum \(minimum)")
534
+ return BridgeValidationError(
535
+ message: "Value for \(propertyName) below minimum",
536
+ code: "VALUE_BELOW_MINIMUM",
537
+ eventName: "BRIDGE_ERROR"
538
+ )
539
+ }
540
+ if let maximum = schema["maximum"] as? NSNumber, numberValue.doubleValue > maximum.doubleValue {
541
+ logger.error("Property '\(propertyName)' value \(numberValue) is above maximum \(maximum)")
542
+ return BridgeValidationError(
543
+ message: "Value for \(propertyName) above maximum",
544
+ code: "VALUE_ABOVE_MAXIMUM",
545
+ eventName: "BRIDGE_ERROR"
546
+ )
547
+ }
548
+
549
+ case "boolean":
550
+ guard value is Bool else {
551
+ logger.error("Property '\(propertyName)' must be a boolean for command \(commandName)")
552
+ return BridgeValidationError(
553
+ message: "Property '\(propertyName)' must be a boolean",
554
+ code: "INVALID_TYPE",
555
+ eventName: "BRIDGE_ERROR"
556
+ )
557
+ }
558
+
559
+ default:
560
+ logger.warning("Unknown schema type '\(expectedType)' for property \(propertyName)")
561
+ }
562
+
563
+ return nil
564
+ }
565
+
566
+ private static func validatePickFileConstraints(_ object: [String: Any]) -> BridgeValidationError? {
567
+ if
568
+ let minFilesValue = object["minFiles"] as? NSNumber,
569
+ let maxFilesValue = object["maxFiles"] as? NSNumber,
570
+ minFilesValue.intValue > maxFilesValue.intValue
571
+ {
572
+ logger.error("minFiles cannot be greater than maxFiles for pickFile command")
573
+ return BridgeValidationError(
574
+ message: "minFiles cannot be greater than maxFiles",
575
+ code: "INVALID_FILE_PICKER_OPTIONS",
576
+ eventName: "ON_FILE_PICK_ERROR"
577
+ )
578
+ }
579
+
580
+ if
581
+ let minSizeValue = object["minFileSize"] as? NSNumber,
582
+ let maxSizeValue = object["maxFileSize"] as? NSNumber,
583
+ minSizeValue.int64Value > maxSizeValue.int64Value
584
+ {
585
+ logger.error("minFileSize cannot be greater than maxFileSize for pickFile command")
586
+ return BridgeValidationError(
587
+ message: "minFileSize cannot be greater than maxFileSize",
588
+ code: "INVALID_FILE_PICKER_OPTIONS",
589
+ eventName: "ON_FILE_PICK_ERROR"
590
+ )
591
+ }
592
+
593
+ return nil
594
+ }
595
+
596
+ // MARK: - Schema Management
597
+
598
+ static func addCustomSchema(for command: String, schema: [String: Any]) {
599
+ // Add schema dictionary directly to schemas
600
+ schemas[command] = schema
601
+ logger.info("Added custom schema for command: \(command)")
602
+ }
603
+
604
+ static func getAvailableCommands() -> Set<String> {
605
+ return Set(schemas.keys)
606
+ }
607
+
608
+ static func hasSchema(for command: String) -> Bool {
609
+ return schemas[command] != nil
610
+ }
611
+ }