Skip to content
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

add Custom Validator section to Validation docs #935

Merged
merged 8 commits into from
Nov 9, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions docs/basics/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,68 @@ Validators can also be combined to build complex validations using operators.
|`!`|prefix|Inverts a validator, requiring the opposite.|
|`&&`|infix|Combines two validators, requires both.|
|`||`|infix|Combines two validators, requires one.|



## Custom Validators

Creating a custom validator for zip codes allows you to extend the functionality of your validation framework. In this section, we'll walk you through the steps to create a custom validator for validating zip codes.
hsharghi marked this conversation as resolved.
Show resolved Hide resolved
0xTim marked this conversation as resolved.
Show resolved Hide resolved

First step is to define the ZipCode Validator
hsharghi marked this conversation as resolved.
Show resolved Hide resolved

To begin, let's create a struct to represent the `ZipCode` validator. This struct will be responsible for checking whether a given string is a valid zip code.
hsharghi marked this conversation as resolved.
Show resolved Hide resolved

```swift
extension ValidatorResults {
/// Represents the result of a validator that checks if a string is a valid zip code.
public struct ZipCode {
/// Indicates whether the input is a valid zip code.
public let isValidZipCode: Bool
}
}
```

The `ZipCode` should conform to the `ValidatorResult` protocol, which defines the behavior expected from a custom validator.
hsharghi marked this conversation as resolved.
Show resolved Hide resolved

```swift
extension ValidatorResults.ZipCode: ValidatorResult {
public var isFailure: Bool {
!self.isValidZipCode
}

public var successDescription: String? {
"is a valid zip code"
}

public var failureDescription: String? {
"is not a valid zip code"
}
}
```

Now, let's implement the validation logic for zip codes. We'll use a regular expression to check whether the input string matches the format of a zip code.
hsharghi marked this conversation as resolved.
Show resolved Hide resolved

```swift
private let zipCodeRegex: String = "^\\d{5}(?:[-\\s]\\d{4})?$"

extension Validator where T == String {
/// Validates whether a `String` is a valid zip code.
public static var zipCode: Validator<T> {
.init { input in
guard let range = input.range(of: zipCodeRegex, options: [.regularExpression]),
range.lowerBound == input.startIndex && range.upperBound == input.endIndex
else {
return ValidatorResults.ZipCode(isValidZipCode: false)
}
return ValidatorResults.ZipCode(isValidZipCode: true)
}
}
}
```

Now that you've defined the custom `zipCode` validator, you can use it to validate zip codes in your application. Simply add the following line to your validation code:

```swift
validations.add("zipCode", as: String.self, is: .zipCode)
```