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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
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 不一致

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 是没有设置

}
}
if (typeof rule === 'function') {
Expand Down
11 changes: 11 additions & 0 deletions tests/number.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
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();
    });
  });

const value = { v: [] };
new Schema({
v: { required: true, transform: () => [] },
}).validate(value, errors => {
expect(errors).toBeTruthy();
expect(errors[0].message).toBe('v is required');
done();
});
});
Expand Down