From b35bfd1d646bd0f1f47b8d0cf0f40384c62ebd72 Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Wed, 16 Sep 2026 21:12:23 +0200 Subject: [PATCH] fix: coerce numeric values to strings in IsDigitsOnly Excel/CSV bulk uploads send numeric cells as JSON numbers, but fields decorated with @IsDigitsOnly expect a string. Every such entity failed validation on a bulk import. Add a @StringType-equivalent Transform to IsDigitsOnly so any field using it gets the numeric->string coercion for free, instead of repeating @StringType on each entity. Also add a spec proving plainToInstance turns 12345 into '12345' and validate passes. --- .../__tests__/class-validator-he.spec.ts | 12 ++++++ utils/validation/class-validator-he.ts | 37 +++++++++++++------ 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/utils/validation/__tests__/class-validator-he.spec.ts b/utils/validation/__tests__/class-validator-he.spec.ts index a58b99d..42edf8d 100644 --- a/utils/validation/__tests__/class-validator-he.spec.ts +++ b/utils/validation/__tests__/class-validator-he.spec.ts @@ -1,4 +1,5 @@ import { IsBoolean, validate } from 'class-validator'; +import { plainToInstance } from 'class-transformer'; jest.mock('../max-count-by-user-limit', () => ({ MaxCountByUserLimit: IsBoolean, @@ -199,5 +200,16 @@ describe('class-validator-he', () => { const result = await validate(test); expect(result).not.toEqual([]); }); + + it('should coerce a numeric value to a string before validating (Excel/CSV bulk upload)', async () => { + class Test { + @IsDigitsOnly(10) key: any; + } + + const test = plainToInstance(Test, { key: 12345 }); + expect(test.key).toBe('12345'); + const result = await validate(test); + expect(result).toEqual([]); + }); }); }); diff --git a/utils/validation/class-validator-he.ts b/utils/validation/class-validator-he.ts index 3cd0aa0..8dc775b 100644 --- a/utils/validation/class-validator-he.ts +++ b/utils/validation/class-validator-he.ts @@ -13,6 +13,8 @@ import { Max as _Max, ValidateBy as _ValidateBy, } from "class-validator"; +import { Transform, TransformFnParams } from "class-transformer"; +import { applyDecorators } from "@nestjs/common"; import { IsUniqueCombination as _IsUniqueCombination } from "./is-unique-combination"; import { GetMaxLimitType, MaxCountByUserLimit as _MaxCountByUserLimit } from "./max-count-by-user-limit"; import { IsUniqueDateRange as _IsUniqueDateRange } from "./is-unique-date-range"; @@ -47,20 +49,31 @@ export const IsUniqueDateRange = (startDateField: string = 'start_date', endDate // optional maxLength also covers fields whose @Column length has no matching // @MaxLength validator. export const IsDigitsOnly = (maxLength?: number, validationOptions?: ValidationOptions): PropertyDecorator => - _ValidateBy( - { - name: 'isDigitsOnly', - constraints: [maxLength], - validator: { - validate: (value: unknown, args: ValidationArguments) => { - if (value === undefined || value === null || value === '') return true; - if (typeof value !== 'string' || !/^\d+$/.test(value)) return false; - const max = args.constraints[0]; - return max === undefined || value.length <= max; + applyDecorators( + Transform((params: TransformFnParams) => { + // Coerce a numeric input (e.g. a cell parsed from an Excel/CSV + // upload) to its string form so it passes the digits-only check. + // Matches the explicit `@StringType` used on existing string-ID + // fields; keeping it here means every IsDigitsOnly field gets the + // transform for free instead of repeating the decorator per entity. + const value = params.value; + return typeof value === 'number' && Number.isFinite(value) ? String(value) : value; + }), + _ValidateBy( + { + name: 'isDigitsOnly', + constraints: [maxLength], + validator: { + validate: (value: unknown, args: ValidationArguments) => { + if (value === undefined || value === null || value === '') return true; + if (typeof value !== 'string' || !/^\d+$/.test(value)) return false; + const max = args.constraints[0]; + return max === undefined || value.length <= max; + }, }, }, - }, - { ...validationOptions, message: getErrorMessageFunction('$property חייב להכיל ספרות בלבד $value') }, + { ...validationOptions, message: getErrorMessageFunction('$property חייב להכיל ספרות בלבד $value') }, + ), ); function getErrorMessageFunction(message: string) {