Skip to content

Commit

Permalink
fix: fix coerce issue in object schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ASafaeirad committed Jan 7, 2024
1 parent 9ff7127 commit bb70f4c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/Schema/ObjectSchema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ObjectSchema } from './ObjectSchema';
describe('Object Schema', () => {
it('should throw error when there is an error in object keys', () => {
const schema = new ObjectSchema({
foo: new NumberSchema(),
foo: new NumberSchema({ coerce: false }),
}).setValue({ foo: '3000' });
schema.key = 'key';

Expand All @@ -18,7 +18,7 @@ describe('Object Schema', () => {
it('should throw validate nested schema', () => {
const schema = new ObjectSchema({
foo: new ObjectSchema({
bar: new NumberSchema(),
bar: new NumberSchema({ coerce: false }),
}),
}).setValue({ foo: '3000' });
schema.key = 'key';
Expand All @@ -45,4 +45,13 @@ describe('Object Schema', () => {
),
);
});

it('should coerce nested object by default', () => {
const schema = new ObjectSchema({
foo: new NumberSchema(),
}).setValue({ foo: '3000' });
schema.key = 'key';

expect(() => schema.validate()).not.toThrow();
});
});
2 changes: 1 addition & 1 deletion src/Schema/ObjectSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ObjectGuard implements Guard<Record<string, Schema>> {

Object.entries(this.schema).forEach(([subKey, schema]) => {
schema.key = `${key}.${subKey}`;
schema.value = input[subKey];
schema.setValue(input[subKey]);

schema.validate();
});
Expand Down

0 comments on commit bb70f4c

Please sign in to comment.