Skip to content

Commit

Permalink
refactor(validator): rename ForbiddenWords to NotIn
Browse files Browse the repository at this point in the history
Replaced 'ForbiddenWords' custom validation with 'NotIn' for clarity and consistency. Updated all references, including test files, to align with the new naming convention.
  • Loading branch information
AMoreaux committed Dec 16, 2024
1 parent 0a7eef9 commit f7eab46
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Field, InputType } from '@nestjs/graphql';

import { IsBoolean, IsOptional, IsString, Matches } from 'class-validator';

import { ForbiddenWords } from 'src/engine/utils/custom-class-validator/ForbiddenWords';
import { NotIn } from 'src/engine/utils/custom-class-validator/NotIn';

@InputType()
export class UpdateWorkspaceInput {
Expand All @@ -15,7 +15,7 @@ export class UpdateWorkspaceInput {
@IsString()
@IsOptional()
@Matches(/^[a-z0-9][a-z0-9-]{1,28}[a-z0-9]$/)
@ForbiddenWords([
@NotIn([
'demo',
'api',
't',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { validate } from 'class-validator';

import { ForbiddenWords } from 'src/engine/utils/custom-class-validator/ForbiddenWords';
import { NotIn } from 'src/engine/utils/custom-class-validator/NotIn';

describe('ForbiddenWordsConstraint', () => {
describe('NotInConstraint', () => {
test('should throw error when word is forbidden', async () => {
class Test {
@ForbiddenWords(['forbidden', 'restricted'])
@NotIn(['forbidden', 'restricted'])
subdomain: string;
}

const instance = new Test();

instance.subdomain = 'forbidden';
instance.subdomain = 'Forbidden';

const errors = await validate(instance);

expect(errors.length).toBeGreaterThan(0);
expect(errors[0].constraints).toEqual({
ForbiddenWordsConstraint: 'forbidden, restricted are not allowed',
NotInConstraint: 'forbidden, restricted are not allowed',
});
});

test('should throw error when regex is forbidden', async () => {
class Test {
@ForbiddenWords(['forbidden', 'restricted', /api-(.*?)+/])
@NotIn(['forbidden', 'restricted', /api-(.*?)+/])
subdomain: string;
}

Expand All @@ -35,14 +35,13 @@ describe('ForbiddenWordsConstraint', () => {

expect(errors.length).toBeGreaterThan(0);
expect(errors[0].constraints).toEqual({
ForbiddenWordsConstraint:
'forbidden, restricted, /api-(.*?)+/ are not allowed',
NotInConstraint: 'forbidden, restricted, /api-(.*?)+/ are not allowed',
});
});

test('should pass validation word is not in the list', async () => {
class Test {
@ForbiddenWords(['forbidden', 'restricted'])
@NotIn(['forbidden', 'restricted'])
subdomain: string;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import {
} from 'class-validator';

@ValidatorConstraint({ async: false })
export class ForbiddenWordsConstraint implements ValidatorConstraintInterface {
private forbiddenWords: Array<string | RegExp>;
export class NotInConstraint implements ValidatorConstraintInterface {
private expressionsList: Array<string | RegExp>;

constructor() {}

validate(value: string, validationArguments: ValidationArguments) {
this.forbiddenWords = validationArguments.constraints[0];
this.expressionsList = validationArguments.constraints[0];

for (const elm of this.forbiddenWords) {
for (const elm of this.expressionsList) {
if (typeof elm === 'string') {
if (elm.toLowerCase() === value.toLowerCase()) {
return false;
Expand All @@ -33,21 +33,21 @@ export class ForbiddenWordsConstraint implements ValidatorConstraintInterface {
}

defaultMessage() {
return `${Array.from(this.forbiddenWords).join(', ')} are not allowed`;
return `${Array.from(this.expressionsList).join(', ')} are not allowed`;
}
}

export function ForbiddenWords(
forbiddenWords: Array<string | RegExp>,
export function NotIn(
expressionsList: Array<string | RegExp>,
validationOptions?: ValidationOptions,
) {
return function (object: object, propertyName: string) {
registerDecorator({
target: object.constructor,
propertyName: propertyName,
options: validationOptions,
constraints: [forbiddenWords],
validator: ForbiddenWordsConstraint,
constraints: [expressionsList],
validator: NotInConstraint,
});
};
}

0 comments on commit f7eab46

Please sign in to comment.