如何在 C# 中使用 Fluent Validation 和 Azure Function 来验证 JSON 对象列表?
我正在尝试使用 Fluent Validation 来验证 JSON 对象列表。我参考了官方文档 https://docs.fluidation.net/en/latest/collections.html 同样,但我的函数无法验证 json 对象列表
public class MyModel
{
public string _No { get; set; }
public string _date { get; set; }
public string expiry_date { get; set; }
public string I_Name { get; set; }
}
public class MyModels
{
public List<MyModel> mymodel { get; set; } = new List<MyModel>();
}
public class ModelValidator : AbstractValidator<MyModel>
{
public ModelValidator()
{
RuleFor(x => x._No).NotEmpty();
RuleFor(x => x._date).NotEmpty();
}
}
public class MyModelsValidator : AbstractValidator<MyModels>
{
public ModelsValidator()
{
RuleForEach(x => x.mymodel).SetValidator(new ModelValidator());
}
}
main.cs
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
ILogger log)
Validators.MyModelsValidator val = new MyModelsValidator();
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var p = JsonConvert.DeserializeObject<MyModels>(requestBody);
var results = await val.ValidateAsync(p);
结果是它没有验证,我返回的 p 值返回为 null。我不确定如何使用 Fluent Validation 处理 JSON 对象列表。
我的 JSON 字符串的格式为:
{
"Data":[
{
"_No":"12345678"
,"_date":"2022-02-08"
,"_expiry_date": "2023-02-08"
, "I_Name" : "Sam"
},
{
"_No":"5587421"
,"_date":"2022-02-10"
,"_expiry_date": "2024-02-08"
, "I_Name" : "Tam"
}
]
}
任何人都可以帮助我并建议我在这里做错了什么吗?
I am trying to validate a list of JSON objects using Fluent Validation. I referred the official documentation
https://docs.fluentvalidation.net/en/latest/collections.html
as well but my function is not able to validate list of json objects
public class MyModel
{
public string _No { get; set; }
public string _date { get; set; }
public string expiry_date { get; set; }
public string I_Name { get; set; }
}
public class MyModels
{
public List<MyModel> mymodel { get; set; } = new List<MyModel>();
}
public class ModelValidator : AbstractValidator<MyModel>
{
public ModelValidator()
{
RuleFor(x => x._No).NotEmpty();
RuleFor(x => x._date).NotEmpty();
}
}
public class MyModelsValidator : AbstractValidator<MyModels>
{
public ModelsValidator()
{
RuleForEach(x => x.mymodel).SetValidator(new ModelValidator());
}
}
main.cs
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
ILogger log)
Validators.MyModelsValidator val = new MyModelsValidator();
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var p = JsonConvert.DeserializeObject<MyModels>(requestBody);
var results = await val.ValidateAsync(p);
The result is that it is not validating and I returned the value of p is returned as null. I am not sure how to handle list of JSON objects with Fluent Validation.
My JSON string is of format:
{
"Data":[
{
"_No":"12345678"
,"_date":"2022-02-08"
,"_expiry_date": "2023-02-08"
, "I_Name" : "Sam"
},
{
"_No":"5587421"
,"_date":"2022-02-10"
,"_expiry_date": "2024-02-08"
, "I_Name" : "Tam"
}
]
}
Can anyone please help me and suggest what I am doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请按照以下解决方法来解决您的问题:
为了更好地理解,我稍微修改了您的代码。
MyModel.cs class
Function.cs
Post 请求主体数据
确保您在像上面那样发布请求时使用了有效的 JSON 数据。
我在发布请求时看到了您的 JSON 数据,它具有不同的格式,如下所示。 (避免错误的 json 格式)
结果:
< /a>
Please follow the below workaround to fix your issue:
I am slightly modified your code for my better understanding.
MyModel.cs class
Function.cs
Post Request Body Data
Make sure you have used valid JSON data while post Request like above.
I saw your JSON Data while post Request it has a different format like below. (Avoid the incorrect format of json)
Results: