Skip to content

Commit

Permalink
feat: add isBooleanField decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiin committed Apr 3, 2024
1 parent fae6eac commit 49bd9c1
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 24 deletions.
47 changes: 23 additions & 24 deletions src/common/@types/interfaces/validator.interface.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
export interface BaseValidator {
required?: boolean
message?: string
required?: boolean;
message?: string;
}

interface BaseArrayValidator {
arrayMaxSize?: number
arrayMinSize?: number
each?: boolean
arrayMaxSize?: number;
arrayMinSize?: number;
each?: boolean;
}

export interface DateFieldOptions extends BaseValidator, BaseArrayValidator {
greaterThan?: boolean
lessThan?: boolean
date?: Date // Date object to compare against
greaterThan?: boolean;
lessThan?: boolean;
date?: Date; // Date object to compare against
};

export interface NumberFieldOptions extends BaseValidator, BaseArrayValidator {
min?: number
max?: number
int?: boolean
positive?: boolean
min?: number;
max?: number;
int?: boolean;
positive?: boolean;
}

export interface StringFieldOptions extends BaseValidator, BaseArrayValidator {
trim?: boolean
regex?: RegExp
minLength?: number
maxLength?: number
sanitize?: boolean
trim?: boolean;
regex?: RegExp;
minLength?: number;
maxLength?: number;
sanitize?: boolean;
}

export interface FileValidator {
fileType?: RegExp | string
fileSize?: number
required?: boolean
fileType?: RegExp | string;
fileSize?: number;
required?: boolean;
}

export type MinMaxLengthOptions = Pick<StringFieldOptions, "each" | "minLength" | "maxLength">;
Expand All @@ -42,8 +42,7 @@ export type EnumFieldOptions = BaseValidator & BaseArrayValidator;
export type EmailFieldOptions = EnumFieldOptions;
export type UUIDFieldOptions = EnumFieldOptions;

export interface IsNestedFieldOptions extends Omit<BaseValidator, "message"> ,BaseArrayValidator{
arrayMinSize: number
arrayMaxSize: number
export interface IsNestedFieldOptions extends Omit<BaseValidator, "message">, BaseArrayValidator {
arrayMinSize: number;
arrayMaxSize: number;
}

61 changes: 61 additions & 0 deletions src/common/decorators/validation/is-boolean-field.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { applyDecorators } from "@nestjs/common";
import {
ArrayNotEmpty,
IsArray,
IsBoolean,
IsNotEmpty,
IsOptional,
} from "class-validator";
import { i18nValidationMessage } from "nestjs-i18n";
import { ToBoolean } from "./transform.decorator";
import { BaseValidator } from "@common/@types";

type IsBooleanValidator = BaseValidator & { each?: boolean };

export function IsBooleanField(options_?: IsBooleanValidator) {
const options: IsBooleanValidator = {
each: false,
required: true,
...options_,
};
const decoratorsToApply = [
IsBoolean({
message: i18nValidationMessage("validation.isDataType", {
type: "boolean",
}),
each: options.each,
}),
ToBoolean(),
];

if (options.required) {
decoratorsToApply.push(
IsNotEmpty({
message: i18nValidationMessage("validation.isNotEmpty"),
each: options.each,
}),
);

if (options.each) {
decoratorsToApply.push(
ArrayNotEmpty({
message: i18nValidationMessage("validation.isNotEmpty"),
}),
);
}
} else {
decoratorsToApply.push(IsOptional());
}

if (options.each) {
decoratorsToApply.push(
IsArray({
message: i18nValidationMessage("validation.isDataType", {
type: "array",
}),
}),
);
}

return applyDecorators(...decoratorsToApply);
}

0 comments on commit 49bd9c1

Please sign in to comment.