Created
November 12, 2023 23:37
-
-
Save rswilley/549431f815e81ed6b671c94d46e00c46 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.ComponentModel.DataAnnotations; | |
using ValTool = System.ComponentModel.DataAnnotations; | |
namespace Validation | |
{ | |
public class Validator | |
{ | |
public List<ValidationResult> ValidationErrors { get; private set; } | |
public Validator(object dataObject) | |
{ | |
_dataObject = dataObject; | |
} | |
public bool IsValid() | |
{ | |
ValidationErrors = new List<ValidationResult>(); | |
if (_dataObject == null) | |
return false; | |
var context = new ValidationContext(_dataObject, null, null); | |
return ValTool.Validator.TryValidateObject(_dataObject, context, ValidationErrors, true); | |
} | |
private readonly object _dataObject; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment