Skip to content

Commit

Permalink
fix: tarnsform type value (#2)
Browse files Browse the repository at this point in the history
* fix: tarnsform type value

* fix: all typeof

* feat: add string test

* feat: add type test

* feat: 删除无用代码

---------

Co-authored-by: afc163 <afc163@gmail.com>
  • Loading branch information
crazyair and afc163 authored Apr 16, 2024
1 parent 89f27d5 commit 70b4b02
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ coverage
.dumi/tmp
.dumi/tmp-production
dist
.docs-dist
.docs-dist
.vscode
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
InternalValidateMessages,
Rule,
RuleItem,
RuleType,
RuleValuePackage,
Rules,
SyncErrorType,
Expand Down Expand Up @@ -145,6 +146,7 @@ class Schema {
source = { ...source };
}
value = source[z] = rule.transform(value);
rule.type ??= (Array.isArray(value) ? 'array' : typeof value) as RuleType;
}
if (typeof rule === 'function') {
rule = {
Expand Down
2 changes: 1 addition & 1 deletion tests/deep.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ describe('deep', () => {
testArray: [],
};
const validator = new Schema(descriptor);
validator.validate(record, (errors, fields) => {
validator.validate(record, () => {
done();
});
});
Expand Down
52 changes: 52 additions & 0 deletions tests/number.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,56 @@ describe('number', () => {
done();
});
});
it('transform type', done => {
const value = { v: 'a' };
new Schema({
v: { required: true, type: 'number', transform: () => 0 },
})
.validate(value, errors => {
expect(errors).toBeFalsy();
})
.then(source => {
expect(source).toEqual({ v: 0 });
done();
});
});
it('transform string', done => {
const value = { v: 'a' };
new Schema({
v: { required: true, transform: v => v },
})
.validate(value, errors => {
expect(errors).toBeFalsy();
})
.then(source => {
expect(source).toEqual({ v: 'a' });
done();
});
});
it('transform number', done => {
const value = { v: 0 };
new Schema({
v: { required: true, transform: v => v },
})
.validate(value, errors => {
expect(errors).toBeFalsy();
})
.then(source => {
expect(source).toEqual({ v: 0 });
done();
});
});
it('transform array', done => {
const value = { v: [0, 1] };
new Schema({
v: { required: true, transform: v => v },
})
.validate(value, errors => {
expect(errors).toBeFalsy();
})
.then(source => {
expect(source).toEqual({ v: [0, 1] });
done();
});
});
});

0 comments on commit 70b4b02

Please sign in to comment.