-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
validation #175
validation #175
Changes from all commits
3d769fe
95d38a8
4cdfd45
39e927f
ee06934
a0f9a0d
43e6a41
3011a7a
d29e0fc
14be548
9c640ae
4d6612d
a613134
6f3872b
e68a3a9
1a35132
50dedcf
e2b13c3
486d420
ee31574
b630847
f822461
bbf9a1e
6bb59c1
6b33387
41c5803
e787819
45d318b
20eea09
e07ca36
cf8cc3e
c10c137
acf886c
b9e3787
724528d
2db3cb7
2b0e0a3
544357c
82fb0e2
6f41f36
0a39cc3
a323f19
58ededb
b8d5a8e
6866242
c2ba1a3
cb87b12
6b6a0c8
9ef2942
ef7db3b
1c9a7bc
4362d62
0f0803f
7148f13
630b8ac
7573e59
609e003
1a1bfce
e09ec64
a77c6a7
7231e81
0150c25
fdcd1a8
0f0a153
519f7fe
fe04fb3
45c520d
3e639bc
9de3bb5
ce0fdff
6a20073
92798e8
4a3d0f8
af784f0
a264a88
aadffdd
05a84d4
77c76d0
52d0f80
6741720
231af31
615a639
9ae78b9
67ab30a
dcd0125
4a6a7c4
5758552
b9c1cec
2d3f8b3
f8d75a3
3b8127e
7d1e684
7554111
e339f4c
9081fd0
8dbe8d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
disabled_rules: | ||
- line_length | ||
- valid_docs | ||
opt_in_rules: | ||
included: | ||
excluded: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
The MIT License (MIT) Copyright (c) 2016 Benjamin Encz | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this | ||
software and associated documentation files (the "Software"), to deal in the Software | ||
without restriction, including without limitation the rights to use, copy, modify, merge, | ||
publish, distribute, sublicense, and/or sell copies of the Software, and to permit | ||
persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or | ||
substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | ||
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE | ||
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
/** | ||
This struct is used to encompass multiple Validators into one entity. | ||
|
||
It is possible to access this struct directly using | ||
|
||
And(validatorOne, validatorTwo) | ||
|
||
But it is more common to create And objects using the `+` operator: | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace) |
||
validatorOne + validatorTwo | ||
*/ | ||
public struct And< | ||
V: Validator, | ||
U: Validator where V.InputType == U.InputType> { | ||
private typealias Validator = (input: V.InputType) throws -> Void | ||
private let validator: Validator | ||
|
||
/** | ||
Convenience only. | ||
|
||
Must stay private. | ||
*/ | ||
private init(_ lhs: Validator, _ rhs: Validator) { | ||
validator = { value in | ||
try lhs(input: value) | ||
try rhs(input: value) | ||
} | ||
} | ||
} | ||
|
||
extension And: Validator { | ||
/** | ||
Validator conformance that allows the 'And' struct | ||
to concatenate multiple Validator types. | ||
|
||
- parameter value: the value to validate | ||
|
||
- throws: an error on failed validation | ||
*/ | ||
public func validate(input value: V.InputType) throws { | ||
try validator(input: value) | ||
} | ||
} | ||
|
||
extension And { | ||
/** | ||
Used to combine two Validator types | ||
*/ | ||
public init(_ lhs: V, _ rhs: U) { | ||
self.init(lhs.validate, rhs.validate) | ||
} | ||
} | ||
|
||
extension And where V: ValidationSuite { | ||
/** | ||
Used to combine two Validator types where one is a ValidationSuite | ||
*/ | ||
public init(_ lhs: V.Type = V.self, _ rhs: U) { | ||
self.init(lhs.validate, rhs.validate) | ||
} | ||
} | ||
|
||
extension And where U: ValidationSuite { | ||
/** | ||
Used to combine two Validators where one is a ValidationSuite | ||
*/ | ||
public init(_ lhs: V, _ rhs: U.Type = U.self) { | ||
self.init(lhs.validate, rhs.validate) | ||
} | ||
} | ||
|
||
extension And where V: ValidationSuite, U: ValidationSuite { | ||
/** | ||
Used to combine two ValidationSuite types | ||
*/ | ||
public init(_ lhs: V.Type = V.self, _ rhs: U.Type = U.self) { | ||
self.init(lhs.validate, rhs.validate) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leading Whitespace Violation: File shouldn't start with whitespace: currently starts with 1 whitespace characters (leading_whitespace) |
||
/** | ||
A validator that can be used to check that a | ||
given string contains only alphanumeric characters | ||
*/ | ||
public struct OnlyAlphanumeric: ValidationSuite { | ||
private static let alphanumeric = "abcdefghijklmnopqrstuvwxyz0123456789" | ||
private static let validCharacters = alphanumeric.characters | ||
|
||
/** | ||
Validate whether or not an input string contains only | ||
alphanumeric characters. a...z0...9 | ||
|
||
- parameter value: input value to validate | ||
|
||
- throws: an error if validation fails | ||
*/ | ||
public static func validate(input value: String) throws { | ||
let passed = value | ||
.lowercased() | ||
.characters | ||
.filter(validCharacters.contains) | ||
.count | ||
|
||
if passed != value.characters.count { | ||
throw error(with: value) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
Validate a comparable | ||
|
||
|
||
- greaterThan: validate input is > associated value | ||
- greaterThanOrEqual: validate input is >= associated value | ||
- lessThan: validate input is < associated value | ||
- lessThanOrEqual: validate input is <= associated value | ||
- equals: validate input == associated value | ||
- containedIn: validate low <= input && input <= high | ||
*/ | ||
public enum Compare<ComparableType where ComparableType: Comparable, ComparableType: Validatable>: Validator { | ||
public typealias InputType = ComparableType | ||
case greaterThan(ComparableType) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type Name Violation: Type name should start with an uppercase character: 'greaterThan' (type_name) |
||
case greaterThanOrEqual(ComparableType) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type Name Violation: Type name should start with an uppercase character: 'greaterThanOrEqual' (type_name) |
||
case lessThan(ComparableType) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type Name Violation: Type name should start with an uppercase character: 'lessThan' (type_name) |
||
case lessThanOrEqual(ComparableType) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type Name Violation: Type name should start with an uppercase character: 'lessThanOrEqual' (type_name) |
||
case equals(ComparableType) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type Name Violation: Type name should start with an uppercase character: 'equals' (type_name) |
||
case containedIn(low: ComparableType, high: ComparableType) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type Name Violation: Type name should start with an uppercase character: 'containedIn' (type_name) |
||
|
||
/** | ||
Validate that a string passes associated compare evaluation | ||
|
||
- parameter value: input string to validate | ||
|
||
- throws: an error if validation fails | ||
*/ | ||
public func validate(input value: InputType) throws { | ||
switch self { | ||
case .greaterThan(let c) where value > c: | ||
break | ||
case .greaterThanOrEqual(let c) where value >= c: | ||
break | ||
case .lessThan(let c) where value < c: | ||
break | ||
case .lessThanOrEqual(let c) where value <= c: | ||
break | ||
case .equals(let e) where value == e: | ||
break | ||
case .containedIn(low: let l, high: let h) where l <= value && value <= h: | ||
break | ||
default: | ||
throw error(with: value) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
Validate that a sequence contains a given value | ||
*/ | ||
public struct Contains< | ||
T where | ||
T: Sequence, | ||
T: Validatable, | ||
T.Iterator.Element: Equatable>: Validator { | ||
|
||
/** | ||
The value expected to be in sequence | ||
*/ | ||
public let expecting: T.Iterator.Element | ||
|
||
/** | ||
Create a validator to check that a sequence contains the given value | ||
|
||
- parameter expecting: the value expected to be in sequence | ||
*/ | ||
public init(_ expecting: T.Iterator.Element) { | ||
self.expecting = expecting | ||
} | ||
|
||
/** | ||
validate | ||
*/ | ||
public func validate(input sequence: T) throws { | ||
for element in sequence where element == expecting { | ||
return | ||
} | ||
|
||
throw error(with: sequence) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)