Skip to content

Instantly share code, notes, and snippets.

@xrisdoc
Created March 5, 2021 08:57
Show Gist options
  • Save xrisdoc/fcb79151c633e328b5bf46aa3f26998d to your computer and use it in GitHub Desktop.
Save xrisdoc/fcb79151c633e328b5bf46aa3f26998d to your computer and use it in GitHub Desktop.
using System;
using System.ComponentModel.DataAnnotations;
namespace ADL.TechPubs.WebFramework.Attributes
{
public class IsTrueAttribute : ValidationAttribute
{
/// <summary>
/// Determines whether the specified value of the object is valid.
/// </summary>
/// <returns>
/// true if the specified value is valid; otherwise, false.
/// </returns>
/// <param name="value">The value of the specified validation object on which the <see cref="T:System.ComponentModel.DataAnnotations.ValidationAttribute"/> is declared.</param>
public override bool IsValid(object value)
{
if (value == null) return false;
if (value.GetType() != typeof(bool)) throw new InvalidOperationException("can only be used on boolean properties.");
return (bool)value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment