Skip to content

Commit

Permalink
fix: allow canBeEmpty: false, use @IsNotEmpty() in that case
Browse files Browse the repository at this point in the history
  • Loading branch information
glebbash committed Feb 15, 2024
1 parent b94b593 commit e0a1be6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/decorators/is-object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('IsObject', () => {
});

it('transforms objects', async () => {
expect(output(make(Test, { objectField: { a: 1 }, b: 2 }))).toStrictEqual({
expect(output(make(Test, { objectField: { a: 1 }, b: 2 } as never))).toStrictEqual({
objectField: { a: 1 },
});
});
Expand Down
17 changes: 13 additions & 4 deletions src/decorators/is-string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,24 @@ describe('IsString', () => {
);
});

it('allows empty strings if canBeEmpty is specified', async () => {
it('allows empty strings if canBeEmpty is set to true', async () => {
class TestCanBeEmpty {
@IsString({ canBeEmpty: true, maxLength: 10 })
stringField!: string;
}

expect(await input(TestCanBeEmpty, { stringField: '' })).toStrictEqual(
Result.ok(make(TestCanBeEmpty, { stringField: '' })),
);
const res = await input(TestCanBeEmpty, { stringField: '' });
expect(res).toStrictEqual(Result.ok(make(TestCanBeEmpty, { stringField: '' })));
});

it('does not allows empty strings if canBeEmpty is set to false', async () => {
class TestCanNotBeEmpty {
@IsString({ canBeEmpty: false, maxLength: 10 })
stringField!: string;
}

const res = await input(TestCanNotBeEmpty, { stringField: '' });
expect(res).toStrictEqual(Result.err('stringField should not be empty'));
});
});

Expand Down
6 changes: 4 additions & 2 deletions src/decorators/is-string.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
IsEmail,
isISO8601,
IsNotEmpty,
IsString as IsStringCV,
Length,
Matches,
Expand Down Expand Up @@ -37,7 +38,7 @@ export const IsString = ({
}: PropertyOptions<
string,
{
canBeEmpty?: true;
canBeEmpty?: boolean;
maxLength?: number;
minLength?: number;
pattern?: { regex: RegExp; message?: ValidationOptions['message'] };
Expand All @@ -57,7 +58,8 @@ export const IsString = ({
},
base,
IsStringCV({ each: !!base.isArray }),
canBeEmpty || minLength !== undefined || maxLength !== undefined
canBeEmpty === false ? IsNotEmpty({ each: !!base.isArray }) : noop,
minLength !== undefined || maxLength !== undefined
? Length(minLength ?? 0, maxLength, { each: !!base.isArray })
: noop,
isEmail ? IsEmail(undefined, { each: !!base.isArray }) : noop,
Expand Down
5 changes: 4 additions & 1 deletion tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { ClassConstructor, instanceToPlain, plainToClass } from 'class-transform
import { validate, ValidationError } from 'class-validator';
import { Result } from 'true-myth';

export function make<T, D extends T>(cls: ClassConstructor<T>, object: D): D {
export function make<C extends ClassConstructor<unknown>>(
cls: C,
object: InstanceType<C>,
): InstanceType<C> {
return Object.assign(new cls() as never, object);
}

Expand Down

0 comments on commit e0a1be6

Please sign in to comment.