本文介绍了使用 C# 进行热巧克力验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我最近一直在玩 Hotchocolate,我制作了一个课程,它给了我一份学生名单,但我想为它提供一些验证功能.我没有从 hotchocolate 官方网站上找到任何对我有帮助的内容.

So I've been playing around with Hotchocolate lately and I made a class which gives me back a list of students, but I want to have some validation functions for it. I didn't really find anything that helps me from the official hotchocolate website.

Student.cs

public class Student
{
    [GraphQLNonNullType]
    public string Name{ get; set; }
    [GraphQLNonNullType]
    public string LastName{ get; set; }
    [GraphQLNonNullType]
    public string Picture { get; set; }
}

这是我的查询,它目前返回列表中的所有学生.

This is my query, which currently gives me back all students from a list.

StudentQuery.cs

StudentQuery.cs

public class StudentQuery
{
    [UseFiltering]
    [UseSorting]
    public List<Student> GetStudents()
    {
        return MongoDBHelper.LoadRecords<Student>(EMongoCollection.Students);
    }

}

现在我的问题是,我如何为学生制定验证规则,例如说学生的名字必须至少有 3 个字符?有没有好心人给我举个例子?

Now my question is, how can I make a ValidationRule for a student, saying for example that a student has to at least have 3 characters for his name?Could someone be kind enough to provide me some example?

提前致谢.

推荐答案

HotChocolate 本身没有用于此类输入验证的集成.该框架仅进行 GraphQL 验证.所以检查无效的 GraphQL 查询.(例如错误类型)

HotChocolate itself does not have a integration for input validation of this kind.The framework only does GraphQL validation. So checks for invalid GraphQL queries. (e.g. Wrong Type)

如果您想使用验证,有几个社区库可供选择:

If you want to use validation there are a few community libraries to choose from:

  • AppAny.HotChocolate.FluentValidation - Input field HotChocolate + FluentValidation integration
  • DataAnnotatedModelValidations - Data Annotated Model Validation Middleware for HotChocolate
  • FairyBread - Input validation for HotChocolate (FluentValidation)
  • FluentChoco - FluentValidation middleware for HotChocolate
  • Graph.ArgumentValidator - Adds input argument validation to HotChocolate (DataAnnotations)

此处列出了社区图书馆:https://github.com/ChilliCream/hotchocolate/blob/main/COMMUNITY.md

Community libraries are listed here:https://github.com/ChilliCream/hotchocolate/blob/main/COMMUNITY.md

这篇关于使用 C# 进行热巧克力验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 23:39