undeclared type 'valid', no such module 'Validation'

Viewed 188

I was following along a screen cast from Ray Wenderlich (https://videos.raywenderlich.com/screencasts/545-server-side-swift-with-vapor-basic-validation) (which doesn't have a compilable project) when I tried to type

let input: Valid<OnlyAlphanumeric> = try request.data["input"].validated()

And the compiler told me Valid doesn't exist. I tried looking for this and found that Vapor doesn't do the validation like this anymore and that it uses different lines found here: https://stackoverflow.com/a/45363444/2305517 which are:

guard let input = req.data["input"]?.string else { throw SomeError }
try input.validated(by: OnlyAlphanumeric())

However strings don't have a function called validated(by:)

I tried importing Validation but the module doesn't exist.

Is there a way to complete the tutorial now? Validation doesn't appear to be in the Vapor code at all.

1 Answers

You need to add the validation-provider package to your project. You can do this by adding a package to the dependency array in your Package.swift file:

For Swift 3, you would add this:

.Package(url: "https://github.com/vapor/validation-provider.git", majorVersion: 1)

In Swift 4, it would look like this:

.package(url: "https://github.com/vapor/validation-provider.git", from: "1.0.0"),

Then run vapor update, and if you are using Xcode, vapor xcode.

You can then import ValidationProvider to the files you need it in.

Make sure you add the provider to your config:

try config.addProvider(ValidationProvider.Provider.self)
Related