From 6f8ad1f2c4f65f163d65596093b8a79965e85feb Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 9 Sep 2026 12:16:06 +0000 Subject: [PATCH] Add IsDigitsOnly validator for varchar ID/number fields Some entity fields (a TZ/ID number, a business "number") must stay varchar rather than a numeric-typed column, to preserve leading zeros. That put them out of reach of the existing numeric validators here, so they had no format validation beyond length/presence. IsDigitsOnly rejects a non-digit string but passes through undefined/null/'' (presence stays @IsNotEmpty's job, as with every other validator in this file), with an optional max length for fields that don't already have one. Follows the file's conventions: Hebrew message via getErrorMessageFunction, wraps class-validator's ValidateBy the same way every other export here wraps its underlying decorator. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_016mY7oyUH46UtvEKekiF7fT --- .../__tests__/class-validator-he.spec.ts | 53 +++++++++++++++++++ utils/validation/class-validator-he.ts | 22 ++++++++ 2 files changed, 75 insertions(+) diff --git a/utils/validation/__tests__/class-validator-he.spec.ts b/utils/validation/__tests__/class-validator-he.spec.ts index d570947..a58b99d 100644 --- a/utils/validation/__tests__/class-validator-he.spec.ts +++ b/utils/validation/__tests__/class-validator-he.spec.ts @@ -11,6 +11,7 @@ import { IsInt, MaxCountByUserLimit, getTranslatedConstraint, + IsDigitsOnly, } from '../class-validator-he'; describe('class-validator-he', () => { @@ -147,4 +148,56 @@ describe('class-validator-he', () => { expect(getTranslatedConstraint('StudentByYear')).toBe('שיוך תלמידות'); }); }); + + describe('IsDigitsOnly', () => { + it('should return a function', () => { + expect(IsDigitsOnly()).toBeInstanceOf(Function); + }); + + it('should not return an error message for undefined, null or an empty string', async () => { + class Test { + @IsDigitsOnly() key: string; + } + + const test = new Test(); + const result = await validate(test); + expect(result).toEqual([]); + + test.key = ''; + expect(await validate(test)).toEqual([]); + }); + + it('should not return an error message for a digits-only string, leading zeros included', async () => { + class Test { + @IsDigitsOnly() key: string; + } + + const test = new Test(); + test.key = '0123456789'; + const result = await validate(test); + expect(result).toEqual([]); + }); + + it('should return an error message if the value contains letters or symbols', async () => { + class Test { + @IsDigitsOnly() key: string; + } + + const test = new Test(); + test.key = '12a34'; + const result = await validate(test); + expect(result).not.toEqual([]); + }); + + it('should enforce an optional max length', async () => { + class Test { + @IsDigitsOnly(5) key: string; + } + + const test = new Test(); + test.key = '123456'; + const result = await validate(test); + expect(result).not.toEqual([]); + }); + }); }); diff --git a/utils/validation/class-validator-he.ts b/utils/validation/class-validator-he.ts index f7c84f0..3cd0aa0 100644 --- a/utils/validation/class-validator-he.ts +++ b/utils/validation/class-validator-he.ts @@ -11,6 +11,7 @@ import { ValidationArguments, Min as _Min, Max as _Max, + ValidateBy as _ValidateBy, } from "class-validator"; import { IsUniqueCombination as _IsUniqueCombination } from "./is-unique-combination"; import { GetMaxLimitType, MaxCountByUserLimit as _MaxCountByUserLimit } from "./max-count-by-user-limit"; @@ -40,6 +41,27 @@ export const MaxCountByUserLimit = (entity: Function, getMaxLimit: GetMaxLimitTy _MaxCountByUserLimit(entity, getMaxLimit, entities, foreignKey, { ...validationOptions, message: getErrorMessageFunction('לא ניתן ליצור עוד רשומות - הגבלת כמות לטבלת $constraint1') }); export const IsUniqueDateRange = (startDateField: string = 'start_date', endDateField: string = 'end_date', entities: Function[] = [], validationOptions?: ValidationOptions) => _IsUniqueDateRange(startDateField, endDateField, entities, { ...validationOptions, message: getErrorMessageFunction('קיימת כבר רשומה עם תאריכים חופפים למשתמש בין $constraint1 ל$constraint2') }); +// Validates a varchar identifier (a TZ/ID number, a business "number") holds digits only. +// Kept as a string column rather than a numeric type so leading zeros survive. +// Blank (undefined/null/'') always passes - presence is @IsNotEmpty's job. An +// 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; + }, + }, + }, + { ...validationOptions, message: getErrorMessageFunction('$property חייב להכיל ספרות בלבד $value') }, + ); function getErrorMessageFunction(message: string) { return function (validationArguments: ValidationArguments) {