Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions utils/validation/__tests__/class-validator-he.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
IsInt,
MaxCountByUserLimit,
getTranslatedConstraint,
IsDigitsOnly,
} from '../class-validator-he';

describe('class-validator-he', () => {
Expand Down Expand Up @@ -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([]);
});
Comment on lines +157 to +168

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([]);
});
});
});
22 changes: 22 additions & 0 deletions utils/validation/class-validator-he.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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') },
Comment on lines +59 to +63
);

function getErrorMessageFunction(message: string) {
return function (validationArguments: ValidationArguments) {
Expand Down