Skip to content

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 6 commits into from
May 14, 2024
Merged

Conversation

crazyair
Copy link

No description provided.

const type = rule.type || ((Array.isArray(value) ? 'array' : typeof value) as RuleType);
if (!isEmptyValue(value, type)) {
rule.type = type;
if (value !== undefined && value !== null) {
Copy link
Author

@crazyair crazyair May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

之前加 isEmptyValue 是因为如果 valueundefined,则 typeof value 为字符串 undefined 导致程序报错(不支持字符串 undefined)。

但是加上 isEmptyValue 后发现会将 [] 也识别为 empty,导致如果 transform 返回值是 []rule 没有 type,最后导致校验的 messagev is not a string 而不是 v is required,这样就与单独写 rule=[{ required:true }]message 不一致

done();
});
});
it('empty array message is "v is required"', done => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

本测试用例用意为与没有设置 transformmesasge 一致

  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();
    });
  });

Copy link

codecov bot commented May 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.33%. Comparing base (17ad411) to head (4411bca).

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.
📢 Have feedback on the report? Share it here.

if (!isEmptyValue(value, type)) {
rule.type = type;
if (value !== undefined && value !== null) {
rule.type = rule.type || ((Array.isArray(value) ? 'array' : typeof value) as RuleType);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value[]type 应该设置为 arraymaster 是没有设置

@afc163 afc163 merged commit 4d05b26 into react-component:master May 14, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants