3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ASP.NETのモデル検証

Last updated at Posted at 2022-02-26

はじめに

ASP.NET/ASP.NET Coreでは、.NETライブラリのSystem.ComponentModel.DataAnnotations名前空間を用いたのモデル検証機能を提供している。

本記事では、以下について取り扱います。

  • モデル検証とは
  • System.ComponentModel.DataAnnotationsに定義された検証属性の一部を紹介
  • モデル検証結果を確認する方法
  • モデル検証をカスタマイズする方法
  • モデル検証を単体テストする方法

モデル検証とは

モデルクラス内に検証ルールを指定することで、アプリケーションのあらゆる場所に検証ルールを適用することができる機能。

参考
CodeZine ASP.NET 4.5の「モデル検証」を活用する
Microsoft Doc:Adding Validation to the Model
CodeZineの解説はとてもわかりやすいです。

検証属性

System.ComponentModel.DataAnnotations名前空間に定義された検証属性を用いることで、モデル検証ができます。

using System.ComponentModel.DataAnnotations;

namespace WebApiValidation.Models
{
    public class Customer
    {
        [Required]
        [Range(1,100)]
        public int? Id { get; set; }
        [Required]
        public string Surname { get; set; }
        [Required]
        public string Forename { get; set; }
    }
}

Required属性をつけると、プロパティを空にできなくなります(nullを指定できない。文字列の場合は空白文字もNG)。
Range属性では、範囲に制限を設けることができます。上記例だと、1から100までの間に制限する。

この他にも、Email形式の検証属性、正規表現の検証属性、文字列長の検証属性などが存在する。
詳細は、System.ComponentModel.DataAnnotations名前空間を参照して下さい。

モデル検証結果の判定

モデル検証結果はModelState.IsValidで確認できます。
ASP.NET/ASP.NET Coreでも同様です(下記リンクを参照)。

Microsoft Doc: ASP.NET WebAPIでのモデル検証
Microsoft Doc: ASP.NET Core MVC および Razor Pages でのモデルの検証
C# Corner: Creating Your Own Validation Attribute In MVC And Web API 2.0

ここでは、ASP.NET Web APIの実装例を以下に示します。

using System.Web.Http;
using WebApiValidation.Models;

namespace WebApiValidation.Controllers
{
    [RoutePrefix("Customer")]
    public class CustomerController : ApiController
    {
        [Route("")]
        [HttpPost]
        public IHttpActionResult Post([FromBody]Customer customer)
        {
            if (ModelState.IsValid)
            {
                return Ok();
            }
            return BadRequest();
        }
    }
}

モデル検証のカスタマイズ

検証属性のカスタマイズ

System.ComponentModel.DataAnnotations名前空間に定義された検証属性で処理できない場合などに、カスタム検証属性を作成します。

実装のポイントは下記の通り。

  • ValidationAttributeを継承します。
  • bool IsValid(object value)をオーバーライドします。

ここではすでに紹介したRequired属性を自作したいと思います。

using System.ComponentModel.DataAnnotations;

namespace WebApiValidation.Models
{
    public class RequiredCustomAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            if (value == null)
                return false;
            return !(value is string str) ||  str.Trim().Length != 0;
        }
    }
}

使用方法は下記の通り。

[RequiredCustom]
public string Surname { get; set; }

参考
Microsoft Doc: How to Customize Data Field Validation in the Data Model Using Custom Attributes

IValidatableObjectを用いた検証

IValidatableObjectを用いた場合、クラスレベルで検証するができるようになります。標準搭載の検証属性だと難しい複数のプロパティの値を比較するなどの検証が可能になります。

実装のポイントは下記の通り。

  • IValidatableObjectを継承します。
  • IValidatableObject.Validate(ValidationContext)を実装します。

ここでは検証属性で示したサンプルを、IValidatableObjectで書き直してみます。

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace WebApiValidation.Models
{
    public class CustomerValidateObject: IValidatableObject
    {
        private const string ErrorPrefix = "Invalid";
        public int? Id { get; set; }
        public string Surname { get; set; }
        public string Forename { get; set; }
        
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (Id == null)
            {
                yield return new ValidationResult($"{ErrorPrefix} {nameof(Id)}");
            }
            if (1 > Id || Id  > 100)
            {
                yield return new ValidationResult($"{ErrorPrefix} {nameof(Id)}");
            }
            if (string.IsNullOrEmpty(Surname))
            {
                yield return new ValidationResult($"{ErrorPrefix} {nameof(Surname)}");
            }
            if (string.IsNullOrEmpty(Forename))
            {
                yield return new ValidationResult($"{ErrorPrefix} {nameof(Forename)}");
            }
        }
    }
}

参考
C# Corner: Web API Validation

モデル検証の単体テスト

Validatorクラスを使用します。
IValidatableObjectを用いた検証の単体テスト方法を示します。

using System.Collections.Generic;
using WebApiValidation.Models;
using System.ComponentModel.DataAnnotations;
using NUnit.Framework;

namespace UnitTestProject
{
    [TestFixture]
    public class CustomerValidateObjectTest
    {
        [TestCase(null, null,null)]
        [TestCase(-1, null,null)]
        [TestCase(0, null,null)]
        [TestCase(1, null,null)]
        [TestCase(1, "",null)]
        [TestCase(1, null,"")]
        [TestCase(1, "","")]
        [TestCase(101, "","")]
        public void TestCustomerValidateObject_Validate_Failed(int? id , string surName, string foreName)
        {
            var model = new CustomerValidateObject
            {
                Id = id,
                Surname = surName,
                Forename = foreName
            };
            var context = new ValidationContext(model);
            Assert.IsFalse(Validator.TryValidateObject(model, context, new List<ValidationResult>()));
        }

        [TestCase(1, "Yamamoto","Taro")]
        [TestCase(100, "Yamamoto","Taro")]
        public void TestCustomerValidateObject_Validate_Success(int id , string surName, string foreName)
        {
            var model = new CustomerValidateObject
            {
                Id = id,
                Surname = surName,
                Forename = foreName
            };
            var context = new ValidationContext(model);
            Assert.IsTrue(Validator.TryValidateObject(model, context, new List<ValidationResult>()));
        }
    }
}
テストの実行環境
.NET Framework4.6.2
NUnit 3.13.2
NUnit3 TestAdapter 3.17.0
System.ComponentModel.Annotations 5.0.0
Microsoft.AspNet.WebApi.Core 5.2.7

参考
C# Corner:Model Class Validation Testing Using Nunit
INTERTECH: How to Unit Test .NET Entity Validation
StackOverFlow: How to write Unit Test for IValidatableObject Model

まとめ

System.ComponentModel.DataAnnotationsを用いて、モデル検証を実施する方法(実装・単体テスト)を示した。
今回は触れませんでしたが、FluentValidationという検証ルールを構築する便利なOSSライブラリをASP.NET/ASP.NET COREに組み込む方法もあります。

参考
FluentValidation 公式ドキュメント
Qiita:.NETで自前で入力チェックを行う際に便利なFluentValidationの使い方

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?