The markdown parsing is broken/disabled for release notes. Sorry about that, I'm chasing the source of a crash that's been bringing this website down for the last couple of days.
This release release include the following PR: #13
## Custom validations with model
User can now define custom validations by taking the entire object as parameter instead of the field tested itself.
This allow to access other attributes from the object when validating a single field:
```swift
struct User: Decodable, Reflectable {
// A custom date formatter to validate the user birthday
let formatter = DateFormatter()
// Field set by the end user
let birthday: String
}
extension User: Validatable {
static func validations() throws -> Validations<User> {
var validations = Validations(User.self)
// We now can define a validation on the entire user,
// allowing us to access other field (here formatter)
try validations.add(\.message, withModel: { user in
guard user.formatter.date(from: user.birthday) == nil else { return }
throw BasicValidationError("is not a valid date.")
})
return validations
}
}
```
This release include the following PR: #11 #12
## Phone Validator
A mistake was made on the phone validator, forgetting to make validator public and accessible from outside the package. This release correct it.
## SwiftLint file
A `.swiftlint.yml` was supposed to be added in version 1.2.1 but was forgot. This release contains it with different custom rules:
* Commented code should be remove
* Assertions such as `if value == true` should be remplaced by `if value`
* Assertions such as `if value == false` should be remplaced by `if !value`
This release include the following PR: #7 #8 #9 #10
## Custom error message
`ValidationsKit` now support to define custom error message when declaring `validations`. This allow to pass some localized strings or simply custom message to display to end user. The closure generating the message also dispose of the value pass by the user.
```swift
// Example with localised string.
validations.add(\.username, at: ["username"], !.empty) { _ in
NSLocalizedString("Username can't be empty", comment: "Error message when user try to sign in with empty user")
}
```
```swift
// Example with use of the user value.
validations.add(\.number, at: ["number"], .range(0 ..< 10) { number in
"\(number) is not between 0 and 10, try again!"
}
```
## Multiple fields validation
The principe of single field validation as been upgrade in order to now take a series of key paths instead. There's no breaking change to single field validation, but you can now extends fields that needs to be validate.
```swift
try model.validate(at: \Model.field, \Model.someOtherField, \Model.etc)
```
## Reflection
`ValidationsKit` now include original vapor `Refletable` system, which make it closer to original repository of validation.
This fulfill a big miss of this library since it was needed to declare the path of each field being validated, without any automatic way to fill it:
```swift
validations.add(\.mail, ["mail"], .mail) // where ["mail"] is the readable name of the field to be shown in case of errors.
```
The import of the `Reflectable` system now make it easier and most of all less error prone to declare a validation since this path will change with the variable name:
```swift
validations.add(\.mail, .mail)
```
## SwiftLint
This project now use [SwiftLint](https://github.com/realm/SwiftLint) to make sure of the code quality. Warnings and errors detect at the first launch as be corrected with the integration of the tool.
No custom rules as been defines yet but probably will be in the future.
This release will add the possibility to validate only one field, specified at `keyPath`:
```swift
try model.validate(at: \Model.field)
```
It also include the `RangeValidator`, working with a `Comparable` type.
It's the first public public release of the `ValidationsKit`, inspired by [vapor/validation](
https://github.com/vapor/validation). This framework is develop with the Swift Package Manager and is accessible with the SPM and Cocoapods.
It include validation system for a model conforming to the `Validatable` protocol, with a defined set of `Validator`s or custom closure.
It also include the following `Validator`s:
* `CharacterSetValidator`: Check if all characters of a `String` is in the defined character set.
* `CountValidator`: Check if the count of a collection is in the defined range.
* `EmptyValidator`: Check if the count of a collection is empty.
* `InValidator`: Check if the item is in the defined array of items.
* `MailValidator`: Check if a `String` is a valid mail address.
* `URLValidator`: Check if a `String` is a valid url address.
* `PhoneValidator`: Check if a `String` is a valid phone number.
* `NilValidator`: Check if an optional is nil.
* `NilIgnoringValidator`: Allow assembling `Validator`s for an optional, ignoring other `Validator`s is the optional is nil.
* `NotValidator`: Inverse the result of a `Validator`.
* `AndValidator`: Allow assembling `Validator`s between them with a `and` condition.
* `OrValidator`: Allow assembling `Validator`s between them with a `or` condition.