예제 #1
0
        public bool Validate(Entity entity)
        {
            var instance = entity.CreateIntance();
            var context  = new ValidationContext(instance);
            var isValid  = true;

            foreach (var property in entity.Properties)
            {
                if (property.TypeInfo.IsFile)
                {
                    var result = _fileValidator.Validate(property);
                    if (result == false)
                    {
                        isValid = false;
                    }
                }
                foreach (var validator in property.ValidationAttributes)
                {
                    try
                    {
                        validator.Validate(property.Value.Raw, context);
                    }
                    catch (ValidationException ex)
                    {
                        isValid = false;
                        _notificator.AddModelError(property.Name, ex.Message);
                    }
                    catch (Exception ex)
                    {
                        _log.Error(ex);
                    }
                }
            }
            return(isValid);
        }
예제 #2
0
        public bool Validate(PropertyValue propertyValue)
        {
            var file = (HttpPostedFileWrapper)propertyValue.Raw;

            if (file == null || file.ContentLength == 0)
            {
                if (propertyValue.Property.IsRequired)
                {
                    _notificator.AddModelError(propertyValue.Property.Name, "Missing file.");
                    return(false);
                }
                return(true);
            }

            var maxFileSize = propertyValue.Property.FileOptions.MaxFileSize.GetValueOrDefault(_configuration.MaxFileSize);

            if (file.ContentLength > maxFileSize)
            {
                _notificator.AddModelError(propertyValue.Property.Name, "File is too big.");
                return(false);
            }

            if (propertyValue.Property.TypeInfo.IsImage && IsImage(file) == false)
            {
                _notificator.AddModelError(propertyValue.Property.Name, "File is not a image.");
                return(false);
            }

            var allowedFileExtensions = propertyValue.Property.FileOptions.AllowedFileExtensions;

            if (allowedFileExtensions == null || allowedFileExtensions.Length == 0)
            {
                allowedFileExtensions = _configuration.AllowedFileExtensions;
            }
            var ext = Path.GetExtension(file.FileName).ToLower();

            if (allowedFileExtensions.Contains(ext) == false)
            {
                _notificator.AddModelError(propertyValue.Property.Name, "Wrong file extension.");
                return(false);
            }

            return(true);
        }
예제 #3
0
        public bool Validate(EntityRecord entityRecord)
        {
            var instance = entityRecord.CreateInstance();
            var context  = new ValidationContext(instance);
            var isValid  = true;

            foreach (var propertyValue in entityRecord.Values.WhereIsNotSkipped())
            {
                if (propertyValue.Property.TypeInfo.IsFile)
                {
                    var result = _fileValidator.Validate(propertyValue);
                    if (result == false)
                    {
                        isValid = false;
                    }
                }

                context.DisplayName = propertyValue.Property.Display;
                foreach (var validator in propertyValue.Property.Validators)
                {
                    try
                    {
                        validator.Validate(propertyValue.Raw, context);
                    }
                    catch (ValidationException ex)
                    {
                        isValid = false;
                        _notificator.AddModelError(propertyValue.Property.Name, ex.Message);
                    }
                    catch (Exception ex)
                    {
                        _log.Error(ex);
                    }
                }
            }
            return(isValid);
        }