Skip to content

Commit

Permalink
refactor: improve coercion
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelvesavuori committed Mar 3, 2024
1 parent e3d08b7 commit 1f42c40
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
17 changes: 9 additions & 8 deletions src/domain/MikroValid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ export class MikroValid {
isAdditionalsOk
);

if (this.isObject(inputKey))
this.handleNestedObject(inputKey as Record<string, any>, propertyKey, results, errors);
this.handleNestedObject(inputKey as Record<string, any>, propertyKey, results, errors);
}
}

Expand All @@ -135,7 +134,7 @@ export class MikroValid {
* @description Checks if a value is actually defined as a non-null value.
*/
private isDefined(value: unknown) {
if (value || typeof value === 'number') return true;
if (!!value) return true;
return false;
}

Expand Down Expand Up @@ -223,12 +222,14 @@ export class MikroValid {
results: Result[],
errors: ValidationError[]
) {
const nestedObjects = this.getNestedObjects(inputKey);
if (this.isObject(inputKey)) {
const nestedObjects = this.getNestedObjects(inputKey);

for (const nested of nestedObjects) {
const nextSchema = propertyKey[nested];
const nextInput = inputKey[nested];
if (nextSchema && nextInput) this.validate(nextSchema, nextInput, results, errors);
for (const nested of nestedObjects) {
const nextSchema = propertyKey[nested];
const nextInput = inputKey[nested];
if (nextSchema && nextInput) this.validate(nextSchema, nextInput, results, errors);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/MikroValid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ test('It should validate a nested object', (t) => {
});

test('It should invalidate multiple errors separately', (t) => {
const expected = 3;
const expected = 2;
const { errors } = match.test(
{
properties: {
Expand All @@ -297,7 +297,7 @@ test('It should invalidate multiple errors separately', (t) => {
type: 'object'
}
},
required: ['box', 'asdf']
required: ['box']
},
{ box: { first: '1', _second: '2' } }
);
Expand Down

0 comments on commit 1f42c40

Please sign in to comment.