使用 xVal 进行数据类型验证

发布于 2024-08-03 15:35:28 字数 512 浏览 8 评论 0原文

我正在尝试找出在 MVC C# 应用程序中验证数据的最佳方法,xVal 似乎是最合适的。但是我遇到了数据类型验证的问题。

起初,我在 DTO 中执行 UpdateModel,然后在 DTO 上运行验证。这对于必填字段等内容非常有效,但是如果您尝试将字符串(“asd”)映射到十进制字段,则 UpdateModel 会抛出异常。由于 UpdateModel 必须在有任何数据需要验证之前运行,我不确定如何解决这个问题。

我的解决方案是为每个表单创建一个 DTO,UpdateModel 将复制到其中,对其运行验证,然后将值复制到正确的 DTO 中。表单 DTO 上的所有属性都是字符串,因此 UpdateModel 永远不会崩溃,并且我将通过 xVal 强制执行数据验证。然而,虽然像 required 这样的规则正在生效,但我似乎无法让 DataType 规则生效(在本例中尝试 DataType.Currency)。

我也尝试过让客户端验证工作,但我希望有一种干净的方法来对数据类型进行服务器端验证。

其他人在服务器端验证数据类型方面做了什么?

I'm trying to figure out the best way to validate data within a MVC C# app and xVal seemed to be the best fit. However I'm running into a problem with data type validation.

At first I was doing an UpdateModel into the DTO and then running the validation on the DTO. This worked great for things like required fields, however UpdateModel would throw an exception if you tried, for example, to map a string ("asd") into a decimal field. Since UpdateModel had to be ran before there was any data to validate I wasn't sure how to get around that.

My solution was to create a DTO per form that UpdateModel would copy into, run validation on that, and then copy values out into the proper DTOs. All the attributes on the form DTO would be strings so UpdateModel never bombs out, and I'd enforce the data validation through xVal. However while rules like required are kicking in, I can't seem to get the DataType rule to kick in (in this case trying DataType.Currency).

I had also tried getting the client-side validation to work, but I was hoping there was a clean way to do server-side validation of data types.

What have others done with regards to validating data types on the server-side?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唯憾梦倾城 2024-08-10 15:35:28

我最终所做的是创建一些代表表单的 DTO。这些 DTO 会接受 Request.Form,并自动将所有表单值映射到内部属性(例如公共字符串电子邮件、公共字符串名字),基于它们与表单值的名称相同。

它们将具有所有字符串属性,并且我将 xVal 属性放在它们上。然后,我使用 xVal 和正则表达式来确保传入的数据有效(例如有效日期、电子邮件、数字等)。这样就永远不会抛出异常,因为它总是进入字符串,而不是 .Net 尝试将其解析为日期或其他内容。

这将确保数据始终到达 xVal,我可以在其中运行我想要的验证,然后一旦我知道我有有效数据,就将其转换为正确的类型,例如 DateTime。

What I ended up doing was creating some DTOs that represent the forms. These DTOs would take a Request.Form in and automatically map all the form values into the internal properties (ex public string email, public string firstname) based on them being the same name as the form values.

They would have all string properties and I'd put the xVal attributes on them. I'd then use xVal and regular expressions to make sure the data coming in was valid (ex a valid date, email, number, etc). This way there would never be an exception thrown because it was always going into a string, as opposed to .Net trying to parse it into a date or something.

This would make sure that the data always made it to xVal where I could run the validation I want, and then convert it to the proper type like DateTime once I know I have valid data.

梦幻的味道 2024-08-10 15:35:28

我使用从 ValidationAttribute 派生的自定义验证器来验证应在服务器端从字符串解析为其他数据类型的数据。例如:

public class DateAttribute : ValidationAttribute
    {

        public override bool IsValid(object value)
        {
            var date = (string)value;
            DateTime result;
            return DateTime.TryParse(date, out result);
        }
    }

我还找到了一种方法,可以将此类验证属性转换为客户端和服务器端验证属性,而无需编写任何自定义 JavaScript 代码。我只需从不同的验证属性基类派生即可。看看我的 有关客户端验证的博客文章(如果您想了解更多相关信息)。

I'm using custom validators derived from ValidationAttribute for validating data that should be parsed on the server-side from string to other data types. For example:

public class DateAttribute : ValidationAttribute
    {

        public override bool IsValid(object value)
        {
            var date = (string)value;
            DateTime result;
            return DateTime.TryParse(date, out result);
        }
    }

I've also found a way to turn such validation attributes into client-side AND server-side validation attributes without writing any custom javascript code. I just have to derive from a different validation attribute base class. Have a look at my blog article about client-side validation if you'd like to learn more about this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文