updated documentation

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-03-05 14:44:08 -06:00
parent 805b0c716c
commit 8b591103c8

View File

@ -18,57 +18,85 @@ public protocol FormFieldable {
var value: ValueType? { get set }
}
public struct AnyRule<ValueType> {
/// Protocol for FormFieldable that require internal validation.
public protocol FormFieldValidatable: FormFieldable {
/// Is there an internalError
var hasInternalError: Bool { get }
/// Internal Error Message that will show.
var internalErrorText: String? { get }
/// Validator that will run the Field Validation
var validator: (any FormFieldValidatorable)? { get set }
}
/// Struct that will execute the validation.
public protocol FormFieldValidatorable {
associatedtype FieldType: FormFieldable
/// FormFieldable to be validated.
var field: FieldType { get set }
/// Rules that will be applied against the FormFieldable
var rules: [AnyRule<FieldType.ValueType>] { get set }
/// Error Message that will show.
var errorMessage: String? { get }
/// Is the FormField valid.
var isValid: Bool { get }
/// Run the rules against the FormFieldable.
func validate()
}
/// Rule that will be executed against a specific ValueType.
public protocol Rule<ValueType> {
associatedtype ValueType
/// Determines if this rule valid for the value passed.
func isValid(value: ValueType?) -> Bool
/// Error Message to be show if the value is invalid.
var errorMessage: String { get }
}
/// Type Erased Rule for a specific ValueType.
public struct AnyRule<ValueType>: Rule {
private let _isValid: (ValueType?) -> Bool
public let errorMessage: String
init<R: Rule>(_ rule: R) where R.ValueType == ValueType {
public init<R: Rule>(_ rule: R) where R.ValueType == ValueType {
self._isValid = rule.isValid
self.errorMessage = rule.errorMessage
}
public func isValid(value: Any?) -> Bool {
guard let typedValue = value as? ValueType? else {
return false
}
return _isValid(typedValue)
public func isValid(value: ValueType?) -> Bool {
return _isValid(value)
}
}
public protocol Rule<ValueType> {
associatedtype ValueType
func isValid(value: ValueType?) -> Bool
var errorMessage: String { get }
}
public class FieldValidator<Field:FormFieldable>{
/// Generic Validator for a specific FormFieldable.
public class FormFieldValidator<Field:FormFieldable>: FormFieldValidatorable{
public var field: Field
public var rules: [AnyRule<Field.ValueType>]?
public var rules: [AnyRule<Field.ValueType>]
public var errorMessages = [String]()
public init(field: Field, rules: [AnyRule<Field.ValueType>]? = nil) {
public var isValid: Bool = true
public init(field: Field, rules: [AnyRule<Field.ValueType>]) {
self.field = field
self.rules = rules
}
public var errorMessage: String? {
errorMessages.joined(separator: "\r")
guard errorMessages.count > 0 else { return nil }
return errorMessages.joined(separator: "\r")
}
public func validate() -> Bool{
public func validate() {
errorMessages.removeAll()
guard let rules else {
return true
}
for rule in rules {
if !rule.isValid(value: field.value) {
errorMessages.append(rule.errorMessage)
return false
isValid = false
return
}
}
return true
isValid = true
}
}