Skip to content

Commit

Permalink
fix: remove redundant check
Browse files Browse the repository at this point in the history
  • Loading branch information
mdovhopo committed Feb 7, 2022
1 parent 9bbf64f commit c75ea48
Showing 1 changed file with 4 additions and 15 deletions.
19 changes: 4 additions & 15 deletions src/decorators/is-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import { compose, noop, PropertyOptions } from '../core';

function validateObjectSize(
obj: Record<string, unknown> | undefined | null,
check: (len: number) => boolean,
optional = true
check: (len: number) => boolean
): boolean {
if (!obj) {
return optional;
return true;
}

return check(Object.keys(obj).length);
Expand All @@ -35,12 +34,7 @@ export const IsObject = <T extends Record<string, unknown>>({
? ValidateBy({
name: 'minProperties',
validator: {
validate: (v) =>
validateObjectSize(
v,
(length) => length >= minProperties,
base.optional || base.nullable
),
validate: (v) => validateObjectSize(v, (length) => length >= minProperties),
defaultMessage: (args) =>
`${args?.property} must have at least ${minProperties} properties`,
},
Expand All @@ -50,12 +44,7 @@ export const IsObject = <T extends Record<string, unknown>>({
? ValidateBy({
name: 'maxProperties',
validator: {
validate: (v) =>
validateObjectSize(
v,
(length) => length <= maxProperties,
base.optional || base.nullable
),
validate: (v) => validateObjectSize(v, (length) => length <= maxProperties),
defaultMessage: (args) =>
`${args?.property} must have at most ${maxProperties} properties`,
},
Expand Down

0 comments on commit c75ea48

Please sign in to comment.