问题
I try to use the Vapor 2 Valid Type, but the compiler only say: "Use of undeclared type Valid".
I had read: https://docs.vapor.codes/2.0/validation/package/ + https://docs.vapor.codes/2.0/validation/overview/
In my my Package.swift I have defined
- /vapor/vapor.git
- /vapor/validation-provider.git
- /fluent-provider.git
as dependencies.
If I try to use Valid<SomeValidator>
the compiler says: "Use of undeclared type Valid".
The use of import Validation
vs import VaporValidation
makes no difference.
Does any one have an idea how I can get Valid to work?
THX for your help.
Edit:
I created a new Vapor project vapor new validtest --api
.
Edited the Package.swift:
import PackageDescription
let package = Package(
name: "validtest",
targets: [
Target(name: "App"),
Target(name: "Run", dependencies: ["App"]),
],
dependencies: [
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 2),
.Package(url: "https://github.com/vapor/validation.git", majorVersion: 1),
.Package(url: "https://github.com/vapor/validation-provider.git", majorVersion: 1),
.Package(url: "https://github.com/vapor/fluent-provider.git", majorVersion: 1)
],
exclude: [
"Config",
"Database",
"Localization",
"Public",
"Resources",
]
)
Ran vapor fetch
and vapor xcode
.
In Routes.swift from the example code I tryed this:
import Vapor
import FluentProvider
import Validation
// or and both imports are tested
import VaporValidation
extension Droplet {
func setupRoutes() throws {
get("info") { req in
let input: Valid<OnlyAlphanumeric> = try req.data["input"].validated()
No matter which import I used, the compiler always say: "Use of undeclared type Valid".
回答1:
Instead of
let input: Valid<OnlyAlphanumeric> = try req.data["input"].validated()
Vapor 2's validation uses
guard let input = req.data["input"]?.string else { throw SomeError }
try input.validated(by: OnlyAlphanumeric())
If the input string is not valid, a ValidationError
will be thrown.
回答2:
I had the same problem. This should work:
.Package(url: "https://github.com/vapor/validation.git", majorVersion: 1)
...
import Validation
...
try EmailValidator().validate(email)
来源:https://stackoverflow.com/questions/44762706/vapor-2-validation-valid-type-throws-use-of-undeclared-type-valid