Skip to content

Commit

Permalink
feat(IsObject): maxProperties option (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdovhopo committed Feb 7, 2022
1 parent 30c0f94 commit c2ded78
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 2 deletions.
97 changes: 97 additions & 0 deletions src/decorators/is-object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,103 @@ describe('IsObject', () => {
expect(await input(Test, { objectField: false })).toStrictEqual(
Result.err('objectField must be an object')
);
expect(await input(Test, { objectField: undefined })).toStrictEqual(
Result.err('objectField must be an object')
);
expect(await input(Test, { objectField: null })).toStrictEqual(
Result.err('objectField must be an object')
);
});

it('checks maxProperties', async () => {
class Test {
@IsObject({ maxProperties: 1 })
objectField!: Record<string, unknown>;
}

expect(await input(Test, { objectField: { a: 1 } })).toStrictEqual(
Result.ok(make(Test, { objectField: { a: 1 } }))
);
expect(await input(Test, { objectField: { a: 1, b: 2 } })).toStrictEqual(
Result.err('objectField must have at most 1 properties')
);
expect(await input(Test, { objectField: false })).toStrictEqual(
Result.err('objectField must be an object')
);
expect(await input(Test, { objectField: undefined })).toStrictEqual(
Result.err('objectField must be an object')
);
expect(await input(Test, { objectField: null })).toStrictEqual(
Result.err('objectField must be an object')
);
});

it('works with minProperties && nullable', async () => {
class Test {
@IsObject({ minProperties: 1, nullable: true })
objectField!: Record<string, unknown> | null;
}

expect(await input(Test, { objectField: { a: 1 } })).toStrictEqual(
Result.ok(make(Test, { objectField: { a: 1 } }))
);
expect(await input(Test, { objectField: undefined })).toStrictEqual(
Result.err('objectField must be an object')
);
expect(await input(Test, { objectField: null })).toStrictEqual(
Result.ok(make(Test, { objectField: null }))
);
});

it('works with minProperties && optional', async () => {
class Test {
@IsObject({ minProperties: 1, optional: true })
objectField?: Record<string, unknown>;
}

expect(await input(Test, { objectField: { a: 1 } })).toStrictEqual(
Result.ok(make(Test, { objectField: { a: 1 } }))
);
expect(await input(Test, { objectField: undefined })).toStrictEqual(
Result.ok(make(Test, {}))
);
expect(await input(Test, { objectField: null })).toStrictEqual(
Result.err('objectField must be an object')
);
});

it('works with maxProperties && nullable', async () => {
class Test {
@IsObject({ maxProperties: 1, nullable: true })
objectField!: Record<string, unknown> | null;
}

expect(await input(Test, { objectField: { a: 1 } })).toStrictEqual(
Result.ok(make(Test, { objectField: { a: 1 } }))
);
expect(await input(Test, { objectField: undefined })).toStrictEqual(
Result.err('objectField must be an object')
);
expect(await input(Test, { objectField: null })).toStrictEqual(
Result.ok(make(Test, { objectField: null }))
);
});

it('works with maxProperties && optional', async () => {
class Test {
@IsObject({ maxProperties: 1, optional: true })
objectField?: Record<string, unknown>;
}

expect(await input(Test, { objectField: { a: 1 } })).toStrictEqual(
Result.ok(make(Test, { objectField: { a: 1 } }))
);
expect(await input(Test, { objectField: undefined })).toStrictEqual(
Result.ok(make(Test, {}))
);
expect(await input(Test, { objectField: null })).toStrictEqual(
Result.err('objectField must be an object')
);
});
});

Expand Down
27 changes: 25 additions & 2 deletions src/decorators/is-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,52 @@ import { IsObject as IsObjectCV, ValidateBy } from 'class-validator';

import { compose, noop, PropertyOptions } from '../core';

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

return check(Object.keys(obj).length);
}

export const IsObject = <T extends Record<string, unknown>>({
message,
minProperties,
maxProperties,
...base
}: PropertyOptions<
T,
{
message?: string;
minProperties?: number;
maxProperties?: number;
}
> = {}): PropertyDecorator =>
compose(
{ type: 'object', minProperties },
{ type: 'object', minProperties, maxProperties },
base,
IsObjectCV({ each: !!base.isArray, message }),
minProperties
? ValidateBy({
name: 'minProperties',
validator: {
validate: (v) => Object.keys(v).length >= minProperties,
validate: (v) => validateObjectSize(v, (length) => length >= minProperties),
defaultMessage: (args) =>
`${args?.property} must have at least ${minProperties} properties`,
},
})
: noop,
maxProperties
? ValidateBy({
name: 'maxProperties',
validator: {
validate: (v) => validateObjectSize(v, (length) => length <= maxProperties),
defaultMessage: (args) =>
`${args?.property} must have at most ${maxProperties} properties`,
},
})
: noop
);

0 comments on commit c2ded78

Please sign in to comment.