public IDtoValidatorResult Validate(object dto)
        {
            var dtoValidator = _dtoValidatorFactory.TryCreate(dto.GetType());

            if (dtoValidator == null)
            {
                return(new DtoValidatorResult <object>());
            }

            var dependentDtosValidatorResult = new DtoValidatorResult <object>();

            foreach (var propertyInfo in dto.GetType().GetProperties())
            {
                if (!(propertyInfo.PropertyType.IsPrimitive ||
                      propertyInfo.PropertyType == typeof(String) ||
                      propertyInfo.PropertyType == typeof(Decimal) ||
                      propertyInfo.PropertyType == typeof(Decimal?) ||
                      propertyInfo.PropertyType == typeof(DateTime) ||
                      propertyInfo.PropertyType == typeof(DateTime?)))
                {
                    var val          = propertyInfo.GetValue(dto);
                    var propertyName = propertyInfo.Name;

                    if (val != null)
                    {
                        if (val.GetType().GetInterface("IEnumerable") != null)
                        {
                            VisitEnumerable(propertyName, val, dependentDtosValidatorResult);
                        }
                        else
                        {
                            VisitSingle(propertyName, val, dependentDtosValidatorResult);
                        }
                    }
                }
            }

            var dtoValidatorResult = dtoValidator.Validate(dto);

            dtoValidatorResult.AddArgumentErrors(dependentDtosValidatorResult.ArgumentErrors);


            return(dtoValidatorResult);
        }
Пример #2
0
        private void ValidateRequest(TRequest request, ValidateRequestResult <TRequest> validateRequestResult)
        {
            var dtoValidator = _dtoValidatorFactory.TryCreate <TRequest>();

            if (dtoValidator != null)
            {
                var dtoValidatorResult = dtoValidator.Validate(request);
                if (!dtoValidatorResult.IsValid)
                {
                    validateRequestResult.ArgumentErrors.AddRange(dtoValidatorResult.ArgumentErrors);
                }
            }

            ValidateRequestInternal(request, validateRequestResult);
        }