Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
0e222f02
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
0e222f02
编写于
6月 17, 2016
作者:
J
James Lopez
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fixing URL validation for import_url on projects
上级
077e3274
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
55 addition
and
3 deletion
+55
-3
app/models/project.rb
app/models/project.rb
+1
-3
app/validators/addressable_url_validator.rb
app/validators/addressable_url_validator.rb
+49
-0
spec/models/project_spec.rb
spec/models/project_spec.rb
+5
-0
未找到文件。
app/models/project.rb
浏览文件 @
0e222f02
...
...
@@ -153,9 +153,7 @@ class Project < ActiveRecord::Base
validates
:namespace
,
presence:
true
validates_uniqueness_of
:name
,
scope: :namespace_id
validates_uniqueness_of
:path
,
scope: :namespace_id
validates
:import_url
,
url:
{
protocols:
%w(ssh git http https)
},
if: :external_import?
validates
:import_url
,
addressable_url:
true
,
if: :external_import?
validates
:star_count
,
numericality:
{
greater_than_or_equal_to:
0
}
validate
:check_limit
,
on: :create
validate
:avatar_type
,
...
...
app/validators/addressable_url_validator.rb
0 → 100644
浏览文件 @
0e222f02
# UrlValidator
#
# Custom validator for URLs.
#
# By default, only URLs for the HTTP(S) protocols will be considered valid.
# Provide a `:protocols` option to configure accepted protocols.
#
# Example:
#
# class User < ActiveRecord::Base
# validates :personal_url, url: true
#
# validates :ftp_url, url: { protocols: %w(ftp) }
#
# validates :git_url, url: { protocols: %w(http https ssh git) }
# end
#
class
AddressableUrlValidator
<
ActiveModel
::
EachValidator
def
validate_each
(
record
,
attribute
,
value
)
unless
valid_url?
(
value
)
record
.
errors
.
add
(
attribute
,
"must be a valid URL"
)
end
end
private
def
default_options
@default_options
||=
{
protocols:
%w(http https ssh git)
}
end
def
valid_url?
(
value
)
return
false
unless
value
value
.
strip!
valid_uri?
(
value
)
&&
valid_protocol?
(
value
)
rescue
Addressable
::
URI
::
InvalidURIError
false
end
def
valid_uri?
(
value
)
Addressable
::
URI
.
parse
(
strip
).
is_a?
(
Addressable
::
URI
)
end
def
valid_protocol?
(
value
)
options
=
default_options
.
merge
(
self
.
options
)
value
=~
/\A
#{
URI
.
regexp
(
options
[
:protocols
])
}
\z/
end
end
spec/models/project_spec.rb
浏览文件 @
0e222f02
...
...
@@ -63,6 +63,11 @@ describe Project, models: true do
expect
(
project2
).
not_to
be_valid
expect
(
project2
.
errors
[
:limit_reached
].
first
).
to
match
(
/Personal project creation is not allowed/
)
end
it
'should not allow an invalid URI as import_url'
do
project2
=
build
(
:project
)
expect
(
project2
).
to
be_valid
end
end
describe
'default_scope'
do
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录