-
-
Notifications
You must be signed in to change notification settings - Fork 7
fix undefined type #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
const type = rule.type || ((Array.isArray(value) ? 'array' : typeof value) as RuleType); | ||
if (!isEmptyValue(value, type)) { | ||
rule.type = type; | ||
if (value !== undefined && value !== null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
之前加 isEmptyValue
是因为如果 value
是 undefined
,则 typeof value
为字符串 undefined
导致程序报错(不支持字符串 undefined
)。
但是加上 isEmptyValue
后发现会将 []
也识别为 empty,导致如果 transform
返回值是 []
,rule
没有 type
,最后导致校验的 message
为 v is not a string
而不是 v is required
,这样就与单独写 rule=[{ required:true }]
的 message
不一致
done(); | ||
}); | ||
}); | ||
it('empty array message is "v is required"', done => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
本测试用例用意为与没有设置 transform
的 mesasge
一致
it('empty array message is "v is required"', done => {
const value = { v: [] };
new Schema({
v: { required: true },
}).validate(value, errors => {
expect(errors).toBeTruthy();
expect(errors[0].message).toBe('v is required');
done();
});
});
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #10 +/- ##
==========================================
- Coverage 82.36% 82.33% -0.03%
==========================================
Files 25 25
Lines 652 651 -1
Branches 236 232 -4
==========================================
- Hits 537 536 -1
Misses 114 114
Partials 1 1 ☔ View full report in Codecov by Sentry. |
if (!isEmptyValue(value, type)) { | ||
rule.type = type; | ||
if (value !== undefined && value !== null) { | ||
rule.type = rule.type || ((Array.isArray(value) ? 'array' : typeof value) as RuleType); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
当 value
为 []
,type
应该设置为 array
,master
是没有设置
No description provided.