IはValidationResult
データの次のセットを有します。それはList<ValidationResult>
です。
FYI
これは、実際には二つのタイプが含まれています。私はカスタム検証結果を作成し、ValidationResult
を実装してモデルのすべてのエラーを収集しました。したがって、このデータセットには実際に2種類のデータがあります。 1つはValidationResult
であり、もう1つはCustomValidationResult
です。
[0] { "Name Field shouldn't be null" }
[1] { "Money Field should be in range 0 and 100" }
[2] { "Validation failed at CompanyList" } // This is CustomValidationResult
ㄴ [0] { "CompanyName Field shouldn't be null" } // ValidationResult inside of CustomValidationResult.
ㄴ [1] { "Validation failed at DepartmentList" } // Belows are same as above
ㄴ [0] { "DepartmentName Field shouldn't be null" }
ㄴ [1] { "Validation failed at EmployeeList" }
ㄴ [0] { "EmployeeName Field shouldn't be null" }
ㄴ [1] { "EmployeeEmail Field's format should be Email" }
これはValidator.TryValidateObject(model, context, true)
方法、System.ComponentModel.DataAnnotations.Validator
で組み込み、静的関数の結果です。
とにかく、個々のエラーに簡単にアクセスできるように、結果のセットをもっときれいにしたいと思います。私は "Key" "Value"構造を考えているので、検証プロセスでどのプロパティが失敗したのかを簡単に知ることができます。
// I want the result to look something like this below.
[0] { "Name", "Name Field Shouldn't be null" }
[1] { "Money", "Money Field should be in range 0 and 100" }
[2] { "CompanyList", "Validation failed at CompanyList" }
ㄴ [0] { "CompanyName", "CompanyName Field shouldn't be null" }
ㄴ [1] { "DepartmentList", "Validation failed at DepartmentList" }
ㄴ [0] { "DepartmentName", "DepartmentName Field shouldn't be null" }
ㄴ [1] { "EmployeeList", "Validation failed at EmployeeList" }
ㄴ [0] { "EmployeeName", "EmployeeName Field shouldn't be null" }
ㄴ [1] { "EmployeeEmail", "EmployeeEmail Field's format should be Email" }
PROBLEM
しかし、 "PropertyName意味"、 "にErrorMessage" キー値モデルは、このようなN-奥行き構造を形成することができません。私は何をすべきか迷っています。
QUESTION
- どのように私はこのためのデータ構造をモデル化する必要がありますか?
- ベストプラクティスは何ですか?
このようなオブジェクトを作成しました。 .NETで目標を達成するための素晴らしい方法があることを期待していました。多少私は実際にそれにネストされたオブジェクトを作ることを避けたいと思っていました。しかし、それを考えに来る、それは最初の場所では不可能ではなかった。 – hina10531