Convert to barfing.
This commit is contained in:
parent
bac00ad0fa
commit
3e00e294a0
@ -40,11 +40,10 @@ class LabelAttributeImageModel: LabelAttributeModel {
|
||||
// MARK: - Validations
|
||||
//--------------------------------------------------
|
||||
|
||||
public override func validateInRange(of text: String) -> MolecularError? {
|
||||
public override func validateInRange(of text: String) throws {
|
||||
guard location >= 0 && location <= text.count else {
|
||||
return MolecularError.validationError("Attribute starting location \(location) is out of bounds for '\(text)'.")
|
||||
throw MolecularError.validationError("Attribute starting location \(location) is out of bounds for '\(text)'.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
|
||||
@ -48,16 +48,15 @@
|
||||
// MARK: - Validations
|
||||
//--------------------------------------------------
|
||||
|
||||
public func validateInRange(of text: String) -> MolecularError? {
|
||||
public func validateInRange(of text: String) throws {
|
||||
// Prevent invalid starting locations.
|
||||
guard location >= 0 && location <= text.count else {
|
||||
return MolecularError.validationError("Attribute starting location \(location) is out of bounds for '\(text)'.")
|
||||
throw MolecularError.validationError("Attribute starting location \(location) is out of bounds for '\(text)'.")
|
||||
}
|
||||
// Prevent lengths extending beyond the bounds of the string.
|
||||
guard length + location <= text.count else {
|
||||
return MolecularError.validationError("Attribute length \(length) starting at \(location) is out of bounds for '\(text)'.")
|
||||
throw MolecularError.validationError("Attribute length \(length) starting at \(location) is out of bounds for '\(text)'.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
|
||||
@ -66,13 +66,10 @@
|
||||
// MARK: - Validations
|
||||
//--------------------------------------------------
|
||||
|
||||
public func validate(_ attributes: [LabelAttributeModel]) -> MolecularError? {
|
||||
public func validate(_ attributes: [LabelAttributeModel]) throws {
|
||||
for attribute in attributes {
|
||||
if let molecularError = attribute.validateInRange(of: text) {
|
||||
return molecularError
|
||||
}
|
||||
try attribute.validateInRange(of: text)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
@ -97,8 +94,8 @@
|
||||
shouldMaskRecordedView = try typeContainer.decodeIfPresent(Bool.self, forKey: .shouldMaskRecordedView) ?? false
|
||||
|
||||
// Later make protocol based validate outside of decoding?
|
||||
if let attributes = attributes, let error = validate(attributes) {
|
||||
throw error
|
||||
if let attributes = attributes {
|
||||
try validate(attributes)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user