Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

DecimalPropertyValidator.cs

using FluentValidation;
using FluentValidation.Validators;

namespace Nop.Web.Framework.Validators;

/// 
/// Decimal validator
/// 
public partial class DecimalPropertyValidator : PropertyValidator
{
    protected readonly decimal _maxValue;

    public override string Name => "DecimalPropertyValidator";

    /// 
    /// Ctor
    /// 
    /// Maximum value
    public DecimalPropertyValidator(decimal maxValue)
    {
        _maxValue = maxValue;
    }

    /// 
    /// Is valid?
    /// 
    /// Validation context
    /// Result
    public override bool IsValid(ValidationContext context, TProperty value)
    {
        if (decimal.TryParse(value.ToString(), out var propertyValue))
            return Math.Round(propertyValue, 3) < _maxValue;

        return false;
    }

    protected override string GetDefaultMessageTemplate(string errorCode) => "Decimal value is out of range";
}