From 89b01f52ea0423bc83b4d56f9700eabb9312d18f Mon Sep 17 00:00:00 2001 From: sanzaru Date: Fri, 25 Oct 2024 19:28:44 +0200 Subject: [PATCH] Optimized validation --- Sources/Formify/FormifyDefaultPattern.swift | 4 ++-- Sources/Formify/FormifyField.swift | 14 +++++++------- Tests/FormifyTests/FormifyTests.swift | 3 +++ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/Sources/Formify/FormifyDefaultPattern.swift b/Sources/Formify/FormifyDefaultPattern.swift index f4220c9..90ad57a 100644 --- a/Sources/Formify/FormifyDefaultPattern.swift +++ b/Sources/Formify/FormifyDefaultPattern.swift @@ -8,6 +8,6 @@ enum FormifyDefaultPattern: String { case email = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}" case phone = "\\+?([0-9]{1,3})?[-.\\s]?(\\(?[0-9]{1,4}\\)?)?[-.\\s]?[0-9]{1,4}[-.\\s]?[0-9]{1,4}[-.\\s]?[0-9]{0,9}" - case urlWithScheme = "[a-zA-Z]+:\\/\\/{1}[a-zA-Z0-9_\\-.?=&\\/]+" - case urlNoScheme = "[a-zA-Z0-9-_.?=&\\/]+" + case urlWithScheme = "[a-zA-Z0-9]+?:\\/\\/?([a-zA-Z0-9\\-_~+]+(\\.[a-zA-Z0-9\\-_~]+)?+){1}(:\\d+)?(\\/[a-zA-Z0-9\\-_~+]*)*(\\?[a-zA-Z0-9\\-_~=&+\\[\\]@$*,.]*)?(#[a-zA-Z0-9\\-_~+\\[\\]@$*.,]*)?" + case urlNoScheme = "[a-zA-Z0-9-_.?=&\\/+]+" } diff --git a/Sources/Formify/FormifyField.swift b/Sources/Formify/FormifyField.swift index baf9bdd..96359da 100644 --- a/Sources/Formify/FormifyField.swift +++ b/Sources/Formify/FormifyField.swift @@ -30,7 +30,7 @@ public struct FormifyField { public private(set) var isTouched = false public private(set) var minLength: Int? public private(set) var maxLength: Int? - public private(set) var pattern: Regex? + public private(set) var pattern: String? private var disableTrimming = false @@ -51,15 +51,15 @@ public struct FormifyField { case .maxLength(let length): maxLength = length case .pattern(let regex): - pattern = try? Regex(regex) + pattern = regex case .email: - pattern = try? Regex(FormifyDefaultPattern.email.rawValue) + pattern = FormifyDefaultPattern.email.rawValue case .phonenumber: - pattern = try? Regex(FormifyDefaultPattern.phone.rawValue) + pattern = FormifyDefaultPattern.phone.rawValue case .urlNoScheme: - pattern = try? Regex(FormifyDefaultPattern.urlNoScheme.rawValue) + pattern = FormifyDefaultPattern.urlNoScheme.rawValue case .urlWithScheme: - pattern = try? Regex(FormifyDefaultPattern.urlWithScheme.rawValue) + pattern = FormifyDefaultPattern.urlWithScheme.rawValue case .disableTrimming: disableTrimming = true } @@ -88,7 +88,7 @@ extension FormifyField { errors.append(.maxLength(value.count)) } - if let pattern, (try? pattern.wholeMatch(in: value)) == nil { + if let pattern, let regex = try? Regex(pattern), (try? regex.wholeMatch(in: value)) == nil { errors.append(.pattern) } } diff --git a/Tests/FormifyTests/FormifyTests.swift b/Tests/FormifyTests/FormifyTests.swift index 08115d7..dc022f9 100644 --- a/Tests/FormifyTests/FormifyTests.swift +++ b/Tests/FormifyTests/FormifyTests.swift @@ -92,6 +92,9 @@ import Testing field.value = "https://example.com/somepath/?foo=bar&foo2=bar2" #expect(field.isValid) + field.value = "ssh://localhost" + #expect(field.isValid) + field.value = "foobar" #expect(!field.isValid) }