From 94a34a04e24a0d11fe5ef6ee80bcb9289327285e Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 9 Sep 2026 12:35:41 +0000 Subject: [PATCH] Move IsDigitsOnly into its own file, trim comment Follows the IsUniqueCombination / IsUniqueDateRange / MaxCountByUserLimit pattern: the raw decorator lives in its own file using registerDecorator, class-validator-he.ts just wraps it with the Hebrew message. Dropped the verbose block comment and the redundant IsDigitsOnly tests in class-validator-he.spec.ts (IsUniqueCombination/IsUniqueDateRange aren't tested there either) in favor of a dedicated is-digits-only.spec.ts. No behavior change from PR #53. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_016mY7oyUH46UtvEKekiF7fT --- .../__tests__/class-validator-he.spec.ts | 53 ------------------- .../__tests__/is-digits-only.spec.ts | 49 +++++++++++++++++ utils/validation/class-validator-he.ts | 23 +------- utils/validation/is-digits-only.ts | 24 +++++++++ 4 files changed, 75 insertions(+), 74 deletions(-) create mode 100644 utils/validation/__tests__/is-digits-only.spec.ts create mode 100644 utils/validation/is-digits-only.ts diff --git a/utils/validation/__tests__/class-validator-he.spec.ts b/utils/validation/__tests__/class-validator-he.spec.ts index a58b99d..d570947 100644 --- a/utils/validation/__tests__/class-validator-he.spec.ts +++ b/utils/validation/__tests__/class-validator-he.spec.ts @@ -11,7 +11,6 @@ import { IsInt, MaxCountByUserLimit, getTranslatedConstraint, - IsDigitsOnly, } from '../class-validator-he'; describe('class-validator-he', () => { @@ -148,56 +147,4 @@ 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/__tests__/is-digits-only.spec.ts b/utils/validation/__tests__/is-digits-only.spec.ts new file mode 100644 index 0000000..6b7336e --- /dev/null +++ b/utils/validation/__tests__/is-digits-only.spec.ts @@ -0,0 +1,49 @@ +import { validate } from 'class-validator'; +import { IsDigitsOnly } from '../is-digits-only'; + +describe('IsDigitsOnly', () => { + it('should not return an error for undefined, null or an empty string', async () => { + class Test { + @IsDigitsOnly() key: string; + } + + const test = new Test(); + expect(await validate(test)).toEqual([]); + + test.key = ''; + expect(await validate(test)).toEqual([]); + }); + + it('should not return an error for a digits-only string, leading zeros included', async () => { + class Test { + @IsDigitsOnly() key: string; + } + + const test = new Test(); + test.key = '0123456789'; + expect(await validate(test)).toEqual([]); + }); + + it('should return an error if the value contains letters or symbols', async () => { + class Test { + @IsDigitsOnly() key: string; + } + + const test = new Test(); + test.key = '12a34'; + expect(await validate(test)).not.toEqual([]); + }); + + it('should enforce an optional max length', async () => { + class Test { + @IsDigitsOnly(5) key: string; + } + + const test = new Test(); + test.key = '123456'; + expect(await validate(test)).not.toEqual([]); + + test.key = '12345'; + expect(await validate(test)).toEqual([]); + }); +}); diff --git a/utils/validation/class-validator-he.ts b/utils/validation/class-validator-he.ts index 3cd0aa0..98bb0a4 100644 --- a/utils/validation/class-validator-he.ts +++ b/utils/validation/class-validator-he.ts @@ -11,11 +11,11 @@ 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"; import { IsUniqueDateRange as _IsUniqueDateRange } from "./is-unique-date-range"; +import { IsDigitsOnly as _IsDigitsOnly } from "./is-digits-only"; export const IsNotEmpty = (validationOptions?: ValidationOptions): PropertyDecorator => _IsNotEmpty({ ...validationOptions, message: getErrorMessageFunction('הערך של $property לא יכול להיות ריק') }); @@ -41,27 +41,8 @@ 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') }, - ); + _IsDigitsOnly(maxLength, { ...validationOptions, message: getErrorMessageFunction('$property חייב להכיל ספרות בלבד $value') }); function getErrorMessageFunction(message: string) { return function (validationArguments: ValidationArguments) { diff --git a/utils/validation/is-digits-only.ts b/utils/validation/is-digits-only.ts new file mode 100644 index 0000000..4ca6508 --- /dev/null +++ b/utils/validation/is-digits-only.ts @@ -0,0 +1,24 @@ +import { registerDecorator, ValidationOptions, ValidationArguments } from 'class-validator'; + +export function IsDigitsOnly(maxLength?: number, validationOptions?: ValidationOptions) { + return function (object: Object, propertyName: string) { + registerDecorator({ + name: 'IsDigitsOnly', + target: object.constructor, + propertyName: propertyName, + constraints: [maxLength], + options: { + message: '$property must contain digits only', + ...validationOptions + }, + validator: { + validate(value: any, 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; + }, + }, + }); + }; +}