Convert to barfing.

This commit is contained in:
Kyle Matthew Hedden 2022-08-17 10:07:49 -04:00
parent bac00ad0fa
commit 3e00e294a0
3 changed files with 9 additions and 14 deletions

View File

@ -40,11 +40,10 @@ class LabelAttributeImageModel: LabelAttributeModel {
// MARK: - Validations // 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 { 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
} }
//-------------------------------------------------- //--------------------------------------------------

View File

@ -48,16 +48,15 @@
// MARK: - Validations // MARK: - Validations
//-------------------------------------------------- //--------------------------------------------------
public func validateInRange(of text: String) -> MolecularError? { public func validateInRange(of text: String) throws {
// Prevent invalid starting locations. // Prevent invalid starting locations.
guard location >= 0 && location <= text.count else { 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. // Prevent lengths extending beyond the bounds of the string.
guard length + location <= text.count else { 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
} }
//-------------------------------------------------- //--------------------------------------------------

View File

@ -66,13 +66,10 @@
// MARK: - Validations // MARK: - Validations
//-------------------------------------------------- //--------------------------------------------------
public func validate(_ attributes: [LabelAttributeModel]) -> MolecularError? { public func validate(_ attributes: [LabelAttributeModel]) throws {
for attribute in attributes { for attribute in attributes {
if let molecularError = attribute.validateInRange(of: text) { try attribute.validateInRange(of: text)
return molecularError
}
} }
return nil
} }
//-------------------------------------------------- //--------------------------------------------------
@ -97,8 +94,8 @@
shouldMaskRecordedView = try typeContainer.decodeIfPresent(Bool.self, forKey: .shouldMaskRecordedView) ?? false shouldMaskRecordedView = try typeContainer.decodeIfPresent(Bool.self, forKey: .shouldMaskRecordedView) ?? false
// Later make protocol based validate outside of decoding? // Later make protocol based validate outside of decoding?
if let attributes = attributes, let error = validate(attributes) { if let attributes = attributes {
throw error try validate(attributes)
} }
} }