Skip to content

Instantly share code, notes, and snippets.

@rswilley
Created November 12, 2023 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rswilley/549431f815e81ed6b671c94d46e00c46 to your computer and use it in GitHub Desktop.
Save rswilley/549431f815e81ed6b671c94d46e00c46 to your computer and use it in GitHub Desktop.
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