Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Validation 2.0.0

Compare
Choose a tag to compare
@tanner0101 tanner0101 released this 11 Apr 19:16
· 17 commits to master since this release

Validation 2.0 is here! 🎉
✅ Extensible data validation library (name, email, etc)

Docs:
https://docs.vapor.codes/3.0/validation/getting-started/

API Docs:
https://api.vapor.codes/validation/latest/Validation

Milestone:
2.0.0


Changes since final release candidate:

Validation was in need of some love before the official release. The APIs have been streamlined quite a bit.

  • Validators are now accessed via leading-dot syntax instead of globally available types.
  • Validatable now requires a static function instead of a static property.
  • Validations are now generic.
  • ValidationData has been removed in favor of type-safe alternative.
  • Separate Range and Count validators.
  • New CharacterSet validator.

Here's an example of how the API looks now compared to previous release:

2.0.0

struct User: Validatable, Reflectable, Codable {
    var id: Int?
    var name: String
    var age: Int
    var email: String?

    static func validations() throws -> Validations<User> {
        var validations = Validations(User.self)
        try validations.add(\.name, .count(5...) && .alphanumeric)
        try validations.add(\.age, .range(18...))
        try validations.add(\.email, .email || .nil)
        return validations
    }
}

2.0.0-rc

struct User: Validatable, Reflectable, Codable {
    var id: Int?
    var name: String
    var age: Int
    var email: String?

    static var validations: Validations = [
        key(\.name): IsCount(5...) && IsAlphanumeric(),
        key(\.age): IsCount(18...),
        key(\.email): IsEmail() || IsNil()
    ]
}