MaxValueAttribute.cs 972 B

12345678910111213141516171819202122232425262728293031
  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  3. namespace Masuit.Tools.Core.Validator
  4. {
  5. public class MaxValueAttribute : ValidationAttribute
  6. {
  7. private double MaxValue { get; }
  8. public MaxValueAttribute(double value)
  9. {
  10. MaxValue = value;
  11. }
  12. public override bool IsValid(object value)
  13. {
  14. if (value is null)
  15. {
  16. return true;
  17. }
  18. var input = Convert.ToDouble(value);
  19. return input <= MaxValue;
  20. }
  21. /// <summary>Applies formatting to an error message, based on the data field where the error occurred.</summary>
  22. /// <param name="name">The name to include in the formatted message.</param>
  23. /// <returns>An instance of the formatted error message.</returns>
  24. public override string FormatErrorMessage(string name)
  25. {
  26. return base.FormatErrorMessage(name);
  27. }
  28. }
  29. }