Skip to content
GitLab
菜单
为什么选择 GitLab
定价
联系销售
探索
为什么选择 GitLab
定价
联系销售
探索
登录
获取免费试用
主导航
搜索或转到…
项目
GitLab
管理
动态
成员
标记
计划
议题
议题看板
里程碑
迭代
需求
代码
合并请求
仓库
分支
提交
标签
仓库图
比较修订版本
代码片段
锁定的文件
构建
流水线
作业
流水线计划
测试用例
产物
部署
发布
Package registry
Container registry
模型注册表
运维
环境
Terraform 模块
监控
事件
服务台
分析
价值流分析
贡献者分析
CI/CD 分析
仓库分析
代码评审分析
议题分析
洞察
模型实验
效能分析
帮助
帮助
支持
GitLab 文档
比较 GitLab 各版本
社区论坛
为极狐GitLab 提交贡献
提交反馈
隐私声明
快捷键
?
新增功能
4
代码片段
群组
项目
显示更多面包屑
gitlab-cn
GitLab
提交
0475c24e
提交
0475c24e
编辑于
1 year ago
作者:
Marc Saleiko
提交者:
Natalia Tepluhina
1 year ago
浏览文件
操作
下载
补丁
差异文件
Adds JS form validation util methods
上级
aa9e4466
No related branches found
分支 包含提交
No related tags found
标签 包含提交
无相关合并请求
变更
2
隐藏空白变更内容
行内
左右并排
显示
2 个更改的文件
app/assets/javascripts/lib/utils/forms.js
+54
-3
54 个添加, 3 个删除
app/assets/javascripts/lib/utils/forms.js
spec/frontend/lib/utils/forms_spec.js
+110
-1
110 个添加, 1 个删除
spec/frontend/lib/utils/forms_spec.js
有
164 个添加
和
4 个删除
app/assets/javascripts/lib/utils/forms.js
+
54
−
3
浏览文件 @
0475c24e
...
@@ -17,19 +17,70 @@ export const serializeForm = (form) => {
...
@@ -17,19 +17,70 @@ export const serializeForm = (form) => {
return
serializeFormEntries
(
entries
);
return
serializeFormEntries
(
entries
);
};
};
/**
* Like trim but without the error for non-string values.
*
* @param {String, Number, Array} - value
* @returns {String, Number, Array} - the trimmed string or the value if it isn't a string
*/
export
const
safeTrim
=
(
value
)
=>
(
typeof
value
===
'
string
'
?
value
.
trim
()
:
value
);
/**
/**
* Check if the value provided is empty or not
* Check if the value provided is empty or not
*
*
* It is being used to check if a form input
* It is being used to check if a form input
* value has been set or not
* value has been set or not
.
*
*
* @param {String, Number, Array} - Any form value
* @param {String, Number, Array} - Any form value
* @returns {Boolean} - returns false if a value is set
* @returns {Boolean} - returns false if a value is set
*
*
* @example
* @example
* returns true for '', [], null, undefined
* returns true for '', ' ', [], null, undefined
*/
export
const
isEmptyValue
=
(
value
)
=>
value
==
null
||
safeTrim
(
value
).
length
===
0
;
/**
* Check if the value has a minimum string length
*
* @param {String, Number, Array} - Any form value
* @param {Number} - minLength
* @returns {Boolean}
*/
export
const
hasMinimumLength
=
(
value
,
minLength
)
=>
!
isEmptyValue
(
value
)
&&
value
.
length
>=
minLength
;
/**
* Checks if the given value can be parsed as an integer as it is (without cutting off decimals etc.)
*
* @param {String, Number, Array} - Any form value
* @returns {Boolean}
*/
export
const
isParseableAsInteger
=
(
value
)
=>
!
isEmptyValue
(
value
)
&&
Number
.
isInteger
(
Number
(
safeTrim
(
value
)));
/**
* Checks if the parsed integer value from the given input is greater than a certain number
*
* @param {String, Number, Array} - Any form value
* @param {Number} - greaterThan
* @returns {Boolean}
*/
export
const
isIntegerGreaterThan
=
(
value
,
greaterThan
)
=>
isParseableAsInteger
(
value
)
&&
parseInt
(
value
,
10
)
>
greaterThan
;
/**
* Regexp that matches email structure.
* Taken from app/models/service_desk_setting.rb custom_email
*/
export
const
EMAIL_REGEXP
=
/^
[\w\-
._
]
+@
[\w\-
.
]
+
\.[
a-zA-Z
]{2,}
$/
;
/**
* Checks if the input is a valid email address
*
* @param {String} - value
* @returns {Boolean}
*/
*/
export
const
isEm
ptyValue
=
(
value
)
=>
value
==
null
||
value
.
length
===
0
;
export
const
isEm
ail
=
(
value
)
=>
EMAIL_REGEXP
.
test
(
value
)
;
/**
/**
* A form object serializer
* A form object serializer
...
...
此差异已折叠。
点击以展开。
spec/frontend/lib/utils/forms_spec.js
+
110
−
1
浏览文件 @
0475c24e
import
{
import
{
serializeForm
,
serializeForm
,
serializeFormObject
,
serializeFormObject
,
safeTrim
,
isEmptyValue
,
isEmptyValue
,
hasMinimumLength
,
isParseableAsInteger
,
isIntegerGreaterThan
,
isEmail
,
parseRailsFormFields
,
parseRailsFormFields
,
}
from
'
~/lib/utils/forms
'
;
}
from
'
~/lib/utils/forms
'
;
...
@@ -99,6 +104,22 @@ describe('lib/utils/forms', () => {
...
@@ -99,6 +104,22 @@ describe('lib/utils/forms', () => {
});
});
});
});
describe
(
'
safeTrim
'
,
()
=>
{
it
.
each
`
input | returnValue
${
''
}
|
${
''
}
${[]}
|
${[]}
${
null
}
|
${
null
}
${
undefined
}
|
${
undefined
}
${
'
'
}
|
${
''
}
${
'
hello
'
}
|
${
'
hello
'
}
${
'
hello
'
}
|
${
'
hello
'
}
${
0
}
|
${
0
}
`
(
'
returns $returnValue for value $input
'
,
({
input
,
returnValue
})
=>
{
expect
(
safeTrim
(
input
)).
toEqual
(
returnValue
);
});
});
describe
(
'
isEmptyValue
'
,
()
=>
{
describe
(
'
isEmptyValue
'
,
()
=>
{
it
.
each
`
it
.
each
`
input | returnValue
input | returnValue
...
@@ -106,14 +127,102 @@ describe('lib/utils/forms', () => {
...
@@ -106,14 +127,102 @@ describe('lib/utils/forms', () => {
${[]}
|
${
true
}
${[]}
|
${
true
}
${
null
}
|
${
true
}
${
null
}
|
${
true
}
${
undefined
}
|
${
true
}
${
undefined
}
|
${
true
}
${
'
'
}
|
${
true
}
${
'
hello
'
}
|
${
false
}
${
'
hello
'
}
|
${
false
}
${
'
'
}
|
${
false
}
${
0
}
|
${
false
}
${
0
}
|
${
false
}
`
(
'
returns $returnValue for value $input
'
,
({
input
,
returnValue
})
=>
{
`
(
'
returns $returnValue for value $input
'
,
({
input
,
returnValue
})
=>
{
expect
(
isEmptyValue
(
input
)).
toBe
(
returnValue
);
expect
(
isEmptyValue
(
input
)).
toBe
(
returnValue
);
});
});
});
});
describe
(
'
hasMinimumLength
'
,
()
=>
{
it
.
each
`
input | minLength | returnValue
${[
'
o
'
,
'
t
'
]}
|
${
1
}
|
${
true
}
${
'
hello
'
}
|
${
3
}
|
${
true
}
${
'
'
}
|
${
2
}
|
${
false
}
${
''
}
|
${
0
}
|
${
false
}
${
''
}
|
${
8
}
|
${
false
}
${[]}
|
${
0
}
|
${
false
}
${
null
}
|
${
8
}
|
${
false
}
${
undefined
}
|
${
8
}
|
${
false
}
${
'
hello
'
}
|
${
8
}
|
${
false
}
${
0
}
|
${
8
}
|
${
false
}
${
4
}
|
${
1
}
|
${
false
}
`
(
'
returns $returnValue for value $input and minLength $minLength
'
,
({
input
,
minLength
,
returnValue
})
=>
{
expect
(
hasMinimumLength
(
input
,
minLength
)).
toBe
(
returnValue
);
},
);
});
describe
(
'
isPareseableInteger
'
,
()
=>
{
it
.
each
`
input | returnValue
${
'
0
'
}
|
${
true
}
${
'
12
'
}
|
${
true
}
${
''
}
|
${
false
}
${[]}
|
${
false
}
${
null
}
|
${
false
}
${
undefined
}
|
${
false
}
${
'
hello
'
}
|
${
false
}
${
'
'
}
|
${
false
}
${
'
12.4
'
}
|
${
false
}
${
'
12ef
'
}
|
${
false
}
`
(
'
returns $returnValue for value $input
'
,
({
input
,
returnValue
})
=>
{
expect
(
isParseableAsInteger
(
input
)).
toBe
(
returnValue
);
});
});
describe
(
'
isIntegerGreaterThan
'
,
()
=>
{
it
.
each
`
input | greaterThan | returnValue
${
25
}
|
${
8
}
|
${
true
}
${
'
25
'
}
|
${
8
}
|
${
true
}
${
'
4
'
}
|
${
1
}
|
${
true
}
${
'
4
'
}
|
${
8
}
|
${
false
}
${
'
9.5
'
}
|
${
8
}
|
${
false
}
${
'
9.5e
'
}
|
${
8
}
|
${
false
}
${[
'
o
'
,
'
t
'
]}
|
${
0
}
|
${
false
}
${
'
hello
'
}
|
${
0
}
|
${
false
}
${
'
'
}
|
${
0
}
|
${
false
}
${
''
}
|
${
0
}
|
${
false
}
${
''
}
|
${
8
}
|
${
false
}
${[]}
|
${
0
}
|
${
false
}
${
null
}
|
${
0
}
|
${
false
}
${
undefined
}
|
${
0
}
|
${
false
}
${
'
hello
'
}
|
${
0
}
|
${
false
}
${
0
}
|
${
0
}
|
${
false
}
`
(
'
returns $returnValue for value $input and greaterThan $greaterThan
'
,
({
input
,
greaterThan
,
returnValue
})
=>
{
expect
(
isIntegerGreaterThan
(
input
,
greaterThan
)).
toBe
(
returnValue
);
},
);
});
describe
(
'
isEmail
'
,
()
=>
{
it
.
each
`
input | returnValue
${
'
user-with_special-chars@example.com
'
}
|
${
true
}
${
'
user@subdomain.example.com
'
}
|
${
true
}
${
'
user@example.com
'
}
|
${
true
}
${
'
user@example.co
'
}
|
${
true
}
${
'
user@example.c
'
}
|
${
false
}
${
'
user@example
'
}
|
${
false
}
${
''
}
|
${
false
}
${[]}
|
${
false
}
${
null
}
|
${
false
}
${
undefined
}
|
${
false
}
${
'
hello
'
}
|
${
false
}
${
'
'
}
|
${
false
}
${
'
12
'
}
|
${
false
}
`
(
'
returns $returnValue for value $input
'
,
({
input
,
returnValue
})
=>
{
expect
(
isEmail
(
input
)).
toBe
(
returnValue
);
});
});
describe
(
'
serializeFormObject
'
,
()
=>
{
describe
(
'
serializeFormObject
'
,
()
=>
{
it
(
'
returns an serialized object
'
,
()
=>
{
it
(
'
returns an serialized object
'
,
()
=>
{
const
form
=
{
const
form
=
{
...
...
此差异已折叠。
点击以展开。
预览
0%
加载中
请重试
或
添加新附件
.
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
保存评论
取消
想要评论请
注册
或
登录