Skip to content

Commit 06a973b

Browse files
authored
fix(ios): validate artboard and state machine names before creating RiveViewModel (#78)
1 parent 49041db commit 06a973b

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

ios/RiveReactNativeView.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
5858
func configure(_ config: ViewConfiguration, dataBindingChanged: Bool = false, reload: Bool = false, initialUpdate: Bool = false) throws {
5959
if reload {
6060
cleanup()
61+
try validateConfiguration(config)
62+
6163
let model = RiveModel(riveFile: config.riveFile)
6264
baseViewModel = RiveViewModel(model, stateMachineName: config.stateMachineName, autoPlay: config.autoPlay, artboardName: config.artboardName)
6365
createViewFromViewModel()
@@ -322,4 +324,38 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
322324

323325
return try onSuccess(input)
324326
}
327+
328+
private func validateConfiguration(_ config: ViewConfiguration) throws {
329+
try validateArtboardName(config.artboardName, in: config.riveFile)
330+
try validateStateMachineName(config.stateMachineName, artboardName: config.artboardName, in: config.riveFile)
331+
}
332+
333+
private func validateArtboardName(_ artboardName: String?, in riveFile: RiveFile) throws {
334+
guard let artboardName = artboardName else { return }
335+
let artboardNames = riveFile.artboardNames()
336+
if !artboardNames.contains(artboardName) {
337+
throw NSError(
338+
domain: RiveErrorDomain,
339+
code: Int(RiveErrorCode.noArtboardFound.rawValue),
340+
userInfo: [NSLocalizedDescriptionKey: "Artboard '\(artboardName)' not found. Available: \(artboardNames.joined(separator: ", "))"]
341+
)
342+
}
343+
}
344+
345+
private func validateStateMachineName(_ stateMachineName: String?, artboardName: String?, in riveFile: RiveFile) throws {
346+
guard let stateMachineName = stateMachineName else { return }
347+
let artboard = artboardName != nil
348+
? try? riveFile.artboard(fromName: artboardName!)
349+
: try? riveFile.artboard()
350+
351+
guard let artboard = artboard else { return }
352+
let stateMachineNames = artboard.stateMachineNames()
353+
if !stateMachineNames.contains(stateMachineName) {
354+
throw NSError(
355+
domain: RiveErrorDomain,
356+
code: Int(RiveErrorCode.noStateMachineFound.rawValue),
357+
userInfo: [NSLocalizedDescriptionKey: "State machine '\(stateMachineName)' not found. Available: \(stateMachineNames.joined(separator: ", "))"]
358+
)
359+
}
360+
}
325361
}

0 commit comments

Comments
 (0)