diff --git a/src/decorators/is-object.spec.ts b/src/decorators/is-object.spec.ts index 02cc83a..c7b0015 100644 --- a/src/decorators/is-object.spec.ts +++ b/src/decorators/is-object.spec.ts @@ -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 }, }); }); diff --git a/src/decorators/is-string.spec.ts b/src/decorators/is-string.spec.ts index c494a54..4f06abf 100644 --- a/src/decorators/is-string.spec.ts +++ b/src/decorators/is-string.spec.ts @@ -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')); }); }); diff --git a/src/decorators/is-string.ts b/src/decorators/is-string.ts index 8dbee15..657484e 100644 --- a/src/decorators/is-string.ts +++ b/src/decorators/is-string.ts @@ -1,6 +1,7 @@ import { IsEmail, isISO8601, + IsNotEmpty, IsString as IsStringCV, Length, Matches, @@ -37,7 +38,7 @@ export const IsString = ({ }: PropertyOptions< string, { - canBeEmpty?: true; + canBeEmpty?: boolean; maxLength?: number; minLength?: number; pattern?: { regex: RegExp; message?: ValidationOptions['message'] }; @@ -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, diff --git a/tests/helpers.ts b/tests/helpers.ts index 0e299e8..7cc9e4d 100644 --- a/tests/helpers.ts +++ b/tests/helpers.ts @@ -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(cls: ClassConstructor, object: D): D { +export function make>( + cls: C, + object: InstanceType, +): InstanceType { return Object.assign(new cls() as never, object); }