Skip to content

Commit c953cb1

Browse files
author
Aaron McTavish
committed
Initial project with a few conditions
Still working out the bugs on the `Validator` protocol. There are some Swift compiler crashes right now.
1 parent 944c850 commit c953cb1

39 files changed

+1946
-0
lines changed

FormValidatorSwift.xcodeproj/project.pbxproj

Lines changed: 600 additions & 0 deletions
Large diffs are not rendered by default.

FormValidatorSwift.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "0720"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "0062E9CF1C45493E00021C0A"
18+
BuildableName = "FormValidatorSwift.framework"
19+
BlueprintName = "FormValidatorSwift"
20+
ReferencedContainer = "container:FormValidatorSwift.xcodeproj">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
24+
</BuildAction>
25+
<TestAction
26+
buildConfiguration = "Debug"
27+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
28+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
shouldUseLaunchSchemeArgsEnv = "YES"
30+
codeCoverageEnabled = "YES">
31+
<Testables>
32+
<TestableReference
33+
skipped = "NO">
34+
<BuildableReference
35+
BuildableIdentifier = "primary"
36+
BlueprintIdentifier = "0062E9D91C45493E00021C0A"
37+
BuildableName = "FormValidatorSwiftTests.xctest"
38+
BlueprintName = "FormValidatorSwiftTests"
39+
ReferencedContainer = "container:FormValidatorSwift.xcodeproj">
40+
</BuildableReference>
41+
</TestableReference>
42+
</Testables>
43+
<MacroExpansion>
44+
<BuildableReference
45+
BuildableIdentifier = "primary"
46+
BlueprintIdentifier = "0062E9CF1C45493E00021C0A"
47+
BuildableName = "FormValidatorSwift.framework"
48+
BlueprintName = "FormValidatorSwift"
49+
ReferencedContainer = "container:FormValidatorSwift.xcodeproj">
50+
</BuildableReference>
51+
</MacroExpansion>
52+
<AdditionalOptions>
53+
</AdditionalOptions>
54+
</TestAction>
55+
<LaunchAction
56+
buildConfiguration = "Debug"
57+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
58+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
59+
launchStyle = "0"
60+
useCustomWorkingDirectory = "NO"
61+
ignoresPersistentStateOnLaunch = "NO"
62+
debugDocumentVersioning = "YES"
63+
debugServiceExtension = "internal"
64+
allowLocationSimulation = "YES">
65+
<MacroExpansion>
66+
<BuildableReference
67+
BuildableIdentifier = "primary"
68+
BlueprintIdentifier = "0062E9CF1C45493E00021C0A"
69+
BuildableName = "FormValidatorSwift.framework"
70+
BlueprintName = "FormValidatorSwift"
71+
ReferencedContainer = "container:FormValidatorSwift.xcodeproj">
72+
</BuildableReference>
73+
</MacroExpansion>
74+
<AdditionalOptions>
75+
</AdditionalOptions>
76+
</LaunchAction>
77+
<ProfileAction
78+
buildConfiguration = "Release"
79+
shouldUseLaunchSchemeArgsEnv = "YES"
80+
savedToolIdentifier = ""
81+
useCustomWorkingDirectory = "NO"
82+
debugDocumentVersioning = "YES">
83+
<MacroExpansion>
84+
<BuildableReference
85+
BuildableIdentifier = "primary"
86+
BlueprintIdentifier = "0062E9CF1C45493E00021C0A"
87+
BuildableName = "FormValidatorSwift.framework"
88+
BlueprintName = "FormValidatorSwift"
89+
ReferencedContainer = "container:FormValidatorSwift.xcodeproj">
90+
</BuildableReference>
91+
</MacroExpansion>
92+
</ProfileAction>
93+
<AnalyzeAction
94+
buildConfiguration = "Debug">
95+
</AnalyzeAction>
96+
<ArchiveAction
97+
buildConfiguration = "Release"
98+
revealArchiveInOrganizer = "YES">
99+
</ArchiveAction>
100+
</Scheme>

FormValidatorSwift.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// AlphabeticCondition.swift
3+
// FormValidatorSwift
4+
//
5+
// Created by Aaron McTavish on 12/01/2016.
6+
// Copyright © 2016 ustwo. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
12+
/**
13+
* The `AlphabeticCondition` checks a string for occurrences of letters.
14+
*/
15+
public struct AlphabeticCondition: Condition {
16+
17+
18+
// MARK: - Properties
19+
20+
public static let localizedViolationString = StringLocalization.localizedString("US2KeyConditionViolationAlphabetic", comment: "")
21+
22+
public let regex: String
23+
24+
public var shouldAllowViolation = true
25+
26+
/// Whether or not to allow whitespace.
27+
public let allowsWhiteSpace: Bool
28+
29+
30+
// MARK: - Initializers
31+
32+
/**
33+
Initializes a `AlphabeticCondition`.
34+
- parameter allowsWhiteSpace: Whether or not to allow whitespace.
35+
*/
36+
public init(allowsWhiteSpace: Bool) {
37+
self.allowsWhiteSpace = allowsWhiteSpace
38+
39+
if allowsWhiteSpace {
40+
regex = "[a-zA-Z\\s]"
41+
} else {
42+
regex = "[a-zA-Z]"
43+
}
44+
}
45+
46+
47+
// MARK: - Check
48+
49+
public func check(text: String?) -> Bool {
50+
guard let sourceText = text
51+
where !sourceText.isEmpty,
52+
let regExpression = try? NSRegularExpression(pattern: regex, options: .CaseInsensitive) else {
53+
54+
return false
55+
}
56+
57+
return regExpression.numberOfMatchesInString(sourceText, options: [], range: NSRange(location: 0, length: sourceText.utf16.count)) == sourceText.utf16.count
58+
}
59+
60+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// AlphanumericCondition.swift
3+
// FormValidatorSwift
4+
//
5+
// Created by Aaron McTavish on 13/01/2016.
6+
// Copyright © 2016 ustwo. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
12+
/**
13+
* The `AlphanumericCondition` checks a string for occurrences of letters and/or numbers.
14+
*/
15+
public struct AlphanumericCondition: Condition {
16+
17+
18+
// MARK: - Properties
19+
20+
public static let localizedViolationString = StringLocalization.localizedString("US2KeyConditionViolationAlphanumeric", comment: "")
21+
22+
public let regex = "[a-zA-Z0-9]"
23+
24+
public var shouldAllowViolation = true
25+
26+
27+
// MARK: - Check
28+
29+
public func check(text: String?) -> Bool {
30+
guard let sourceText = text
31+
where !sourceText.isEmpty,
32+
let regExpression = try? NSRegularExpression(pattern: regex, options: .CaseInsensitive) else {
33+
34+
return false
35+
}
36+
37+
return regExpression.numberOfMatchesInString(sourceText, options: [], range: NSRange(location: 0, length: sourceText.utf16.count)) == sourceText.utf16.count
38+
}
39+
40+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// EmailCondition.swift
3+
// FormValidatorSwift
4+
//
5+
// Created by Aaron McTavish on 13/01/2016.
6+
// Copyright © 2016 ustwo. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
12+
/**
13+
* The `EmailCondition` checks a string for an email.
14+
*/
15+
public struct EmailCondition: Condition {
16+
17+
18+
// MARK: - Properties
19+
20+
public static let localizedViolationString = StringLocalization.localizedString("US2KeyConditionViolationEmail", comment: "")
21+
22+
public let regex = "^[+\\w\\.\\-']+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*(\\.[a-zA-Z]{2,})+$"
23+
24+
public var shouldAllowViolation = true
25+
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// NumericCondition.swift
3+
// FormValidatorSwift
4+
//
5+
// Created by Aaron McTavish on 13/01/2016.
6+
// Copyright © 2016 ustwo. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
12+
/**
13+
* The `NumericCondition` checks a string for numbers.
14+
*/
15+
public struct NumericCondition: Condition {
16+
17+
18+
// MARK: - Properties
19+
20+
public static let localizedViolationString = StringLocalization.localizedString("US2KeyConditionViolationNumeric", comment: "")
21+
22+
public let regex = "^\\d+$"
23+
24+
public var shouldAllowViolation = true
25+
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// PostcodeUKCondition.swift
3+
// FormValidatorSwift
4+
//
5+
// Created by Aaron McTavish on 13/01/2016.
6+
// Copyright © 2016 ustwo. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
12+
/**
13+
* The `PostcodeUKCondition` checks a string for UK post code.
14+
*/
15+
public struct PostcodeUKCondition: Condition {
16+
17+
18+
// MARK: - Properties
19+
20+
public static let localizedViolationString = StringLocalization.localizedString("US2KeyConditionViolationPostcodeUK", comment: "")
21+
22+
public let regex = "^([A-PR-UWYZa-pr-uwyz]([0-9]{1,2}|([A-HK-Ya-hk-y][0-9]|[A-HK-Ya-hk-y][0-9]([0-9]|[ABEHMNPRV-Yabehmnprv-y]))|[0-9][A-HJKS-UWa-hjks-uw])\\ {0,1}[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}|([Gg][Ii][Rr]\\ 0[Aa][Aa])|([Ss][Aa][Nn]\\ {0,1}[Tt][Aa]1)|([Bb][Ff][Pp][Oo]\\ {0,1}([Cc]\\/[Oo]\\ )?[0-9]{1,4})|(([Aa][Ss][Cc][Nn]|[Bb][Bb][Nn][Dd]|[BFSbfs][Ii][Qq][Qq]|[Pp][Cc][Rr][Nn]|[Ss][Tt][Hh][Ll]|[Tt][Dd][Cc][Uu]|[Tt][Kk][Cc][Aa])\\ {0,1}1[Zz][Zz]))$"
23+
24+
public var shouldAllowViolation = true
25+
26+
}

Sources/Conditions/URLCondition.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// URLCondition.swift
3+
// FormValidatorSwift
4+
//
5+
// Created by Aaron McTavish on 13/01/2016.
6+
// Copyright © 2016 ustwo. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
12+
/**
13+
* The `URLCondition` checks a string for a valid URL.
14+
* - note: The scheme (protocol) is needed for a valid URL. If you want a check for shorthand URLs see `URLShorthandCondition`. Only HTTP and HTTPS schemes are considered valid.
15+
*/
16+
public struct URLCondition: Condition {
17+
18+
19+
// MARK: - Properties
20+
21+
public static let localizedViolationString = StringLocalization.localizedString("US2KeyConditionViolationURL", comment: "")
22+
23+
public let regex = "^((https?)://)[a-z0-9-]+(\\.[a-z0-9-]+)+([/?].*)?$"
24+
25+
public var shouldAllowViolation = true
26+
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// URLShorthandCondition.swift
3+
// FormValidatorSwift
4+
//
5+
// Created by Aaron McTavish on 13/01/2016.
6+
// Copyright © 2016 ustwo. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
12+
/**
13+
* The `URLShorthandCondition` checks a string for a valid URL.
14+
* - note: No scheme (protocol) is needed for a valid URL. If you want a check for more strict URLs see `URLCondition`.
15+
*/
16+
public struct URLShorthandCondition: Condition {
17+
18+
19+
// MARK: - Properties
20+
21+
public static let localizedViolationString = StringLocalization.localizedString("US2KeyConditionViolationShorthandURL", comment: "")
22+
23+
public let regex = "^((https?)://)?[a-z0-9-]+(\\.[a-z0-9-]+)+([/?].*)?$"
24+
25+
public var shouldAllowViolation = true
26+
27+
}

Sources/FormValidatorSwift.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// FormValidatorSwift.h
3+
// FormValidatorSwift
4+
//
5+
// Created by Aaron McTavish on 12/01/2016.
6+
// Copyright © 2016 ustwo. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
//! Project version number for FormValidatorSwift.
12+
FOUNDATION_EXPORT double FormValidatorSwiftVersionNumber;
13+
14+
//! Project version string for FormValidatorSwift.
15+
FOUNDATION_EXPORT const unsigned char FormValidatorSwiftVersionString[];
16+
17+
// In this header, you should import all the public headers of your framework using statements like #import <FormValidatorSwift/PublicHeader.h>
18+
19+

0 commit comments

Comments
 (0)