lundi 15 mai 2023

Validation should stop with the first condition violation

I have specification and I want to stop validation with first false result.

at now check all validation and then return result.

ISpecification:

 public interface ISpecification<T>
    {
        SpecificationResult IsSatisfiedBy(T value);
    }

Specification:

public abstract class Specification<T> : ISpecification<T>
{
    public Specification<T> And(ISpecification<T> right)
    {
        return new AndSpecification<T>(this, right);
    }
    public Specification<T> Or(ISpecification<T> right)
    {
        return new OrSpecification<T>(this, right);
    }
    public Specification<T> Not()
    {
        return new NotSpecification<T>(this);
    }

    public abstract SpecificationResult IsSatisfiedBy(T entity);
}

AndSpecification

    public class AndSpecification<T> : Specification<T>
{
    private readonly ISpecification<T> _leftSpec;
    private readonly ISpecification<T> _rightSpec;
    public AndSpecification(ISpecification<T> leftSpec, ISpecification<T> rightSpec)
    {
        _leftSpec = leftSpec;
        _rightSpec = rightSpec;
    }
    public override SpecificationResult IsSatisfiedBy(T entity)
    {
        return new SpecificationResult()
        {
            IsSatisfied = _leftSpec.IsSatisfiedBy(entity).IsSatisfied && _rightSpec.IsSatisfiedBy(entity).IsSatisfied,
            Message = new StringBuilder().Append(_leftSpec.IsSatisfiedBy(entity).Message).Append(_rightSpec.IsSatisfiedBy(entity).Message).ToString(),
        };
    }
}

my custom validation :

    public class UserIsActive : Specification<User>
{
    public override SpecificationResult IsSatisfiedBy(User user)
    {
        if (user.Status != UserStatus.Active)
        {
            return new SpecificationResult
            {
                IsSatisfied = false,
                Message = "حساب کاربری  شما غیرفعال میباشد لطفا با پشتیبانی تماس بگیرید"
            };
        }
        return new SpecificationResult
        {
            IsSatisfied = true,
            Message = string.Empty
        };
    }
}

public class UserIsNotExpoire : Specification<User>
{
    public override SpecificationResult IsSatisfiedBy(User user)
    {
        if (user.IsExpired)
        {
            return new SpecificationResult
            {
                IsSatisfied = false,
                Message = "حساب کاربری  شما منقضی شده است لطفا با پشتیبانی تماس بگیرید"
            };
        }
        return new SpecificationResult
        {
            IsSatisfied = true,
            Message = string.Empty
        };
    }
}

public class UserIsNotNull : Specification<User>
{
    public override SpecificationResult IsSatisfiedBy(User user)
    {
        if (user==null)
        {
            return new SpecificationResult
            {
                IsSatisfied = false,
                Message = "نام کاربری یا کلمه عبور اشتباه است"
            };
        }
        return new SpecificationResult
        {
            IsSatisfied = true,
            Message = string.Empty
        };
    }
}

and this how I use specification.

var validation = new UserIsActive().And(new UserIsNotExpoire().And(new UserIsNotNull()));

           var result = validation.IsSatisfiedBy(user);
            if (!result.IsSatisfied)
                return Ok(new
                {
                    success = false,
                    errorMessage = result.Message,  
                });

Aucun commentaire:

Enregistrer un commentaire