本文介绍了Vapor 2 Validation Valid-Type 抛出“Use of undeclared type Valid"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 Vapor 2 Valid Type,但编译器只说:Use of undeclared type Valid".

I try to use the Vapor 2 Valid Type, but the compiler only say: "Use of undeclared type Valid".

我读过:https://docs.vapor.codes/2.0/validation/包/ + https://docs.vapor.codes/2.0/validation/概述/

在我的 Package.swift 中,我定义了

In my my Package.swift I have defined

  • /vapor/vapor.git
  • /vapor/validation-provider.git
  • /fluent-provider.git

作为依赖项.

如果我尝试使用 Valid,编译器会说:使用未声明的类型 Valid".

If I try to use Valid<SomeValidator> the compiler says: "Use of undeclared type Valid".

import Validationimport VaporValidation 的使用没有区别.

The use of import Validation vs import VaporValidation makes no difference.

有人知道如何让 Valid 工作吗?

Does any one have an idea how I can get Valid to work?

感谢您的帮助.

我创建了一个新的 Vapor 项目 vapor new validtest --api.

I created a new Vapor project vapor new validtest --api.

编辑 Package.swift:

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",
    ]
)

运行 vapor fetchvapor xcode.

在示例代码的 Routes.swift 中,我尝试了这个:

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".

推荐答案

代替

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

Vapor 2 的验证用途

Vapor 2's validation uses

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

如果输入的字符串无效,会抛出一个ValidationError.

If the input string is not valid, a ValidationError will be thrown.

这篇关于Vapor 2 Validation Valid-Type 抛出“Use of undeclared type Valid"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 05:11