Ejemplo n.º 1
0
 private void SetIfThereIsNoStatuscode(IValidatorResponse response)
 {
     if (response.IsValid)
     {
         Result = new OkObjectResult(response);
     }
     else
     {
         Result = new BadRequestObjectResult(response);
     }
 }
Ejemplo n.º 2
0
        public IActionResult ProcessResponse(IValidatorResponse response)
        {
            switch (response.StatusCode)
            {
            case 400:
            case 401:
                Result = new BadRequestObjectResult(response);
                break;

            case 404:
                Result = new NotFoundObjectResult(response);
                break;

            case 200:
                Result = new OkObjectResult(response);
                break;

            default:
                SetIfThereIsNoStatuscode(response);
                break;
            }
            return(Result);
        }
Ejemplo n.º 3
0
        public object Validate(object value, HttpContext httpContext)
        {
            var resolveLocalization = (ILocalizationInfo)httpContext.RequestServices.GetService(typeof(ILocalizationInfo));

            ResponseValidation = ValidatorResponse.Response != null ? (IValidatorResponse)httpContext.RequestServices.GetService(ValidatorResponse.Response) : null;
            var errors = new Dictionary <string, string>();

            if (value != null)
            {
                value.Sanitize(httpContext);
                var entityType = value.GetType();
                var properties = entityType.GetProperties();
                foreach (var property in properties)
                {
                    var attributes = property.GetCustomAttributes(true);
                    foreach (var attribute in attributes)
                    {
                        if (attribute is IValidator)
                        {
                            var errorMessage = ((IValidator)attribute).Validate(value, httpContext, property, resolveLocalization);
                            if (!string.IsNullOrEmpty(errorMessage))
                            {
                                errors.Add(property.Name.ToCamelCase(), errorMessage);
                                break;
                            }
                        }
                    }
                }
                if (errors.Count > 0)
                {
                    var modelValidation = entityType.GetCustomAttributes(typeof(ModelValidationAttribute), true).SingleOrDefault() as ModelValidationAttribute;
                    return(this.CreateInvalidResponse(errors, resolveLocalization.GetValidationMessage(modelValidation != null ? modelValidation.KeyName : "validationTitle")));
                }
            }
            return(null);
        }