forked from yiminghe/async-validator
-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,15 +16,7 @@ import type { | |
Values, | ||
} from './interface'; | ||
import { messages as defaultMessages, newMessages } from './messages'; | ||
import { | ||
asyncMap, | ||
complementError, | ||
convertFieldsError, | ||
deepMerge, | ||
format, | ||
isEmptyValue, | ||
warning, | ||
} from './util'; | ||
import { asyncMap, complementError, convertFieldsError, deepMerge, format, warning } from './util'; | ||
import validators from './validator/index'; | ||
|
||
export * from './interface'; | ||
|
@@ -154,9 +146,8 @@ class Schema { | |
source = { ...source }; | ||
} | ||
value = source[z] = rule.transform(value); | ||
const type = rule.type || ((Array.isArray(value) ? 'array' : typeof value) as RuleType); | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. 当 |
||
} | ||
} | ||
if (typeof rule === 'function') { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,6 +167,17 @@ describe('number', () => { | |
v: { required: true, transform: v => undefined }, | ||
}).validate(value, errors => { | ||
expect(errors).toBeTruthy(); | ||
expect(errors[0].message).toBe('v is required'); | ||
done(); | ||
}); | ||
}); | ||
it('empty array message is "v is required"', done => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 本测试用例用意为与没有设置 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();
});
}); |
||
const value = { v: [] }; | ||
new Schema({ | ||
v: { required: true, transform: () => [] }, | ||
}).validate(value, errors => { | ||
expect(errors).toBeTruthy(); | ||
expect(errors[0].message).toBe('v is required'); | ||
done(); | ||
}); | ||
}); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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
不一致