> For the complete documentation index, see [llms.txt](https://kinesis-school-of-programming.gitbook.io/nestjs-unleashed/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kinesis-school-of-programming.gitbook.io/nestjs-unleashed/core-module-backend-development-with-nestjs/decorator-composition.md).

# Decorator composition

It would be nice to encapsulate many decorators into one when their combined use is somewhat frequent.

As you know, we have a **DTO** to validate an <mark style="color:blue;">`id`</mark>, the <mark style="color:blue;">`IdDto`</mark>. As a serial id is a positive integer, we use both the <mark style="color:blue;">`@IsInt()`</mark> and <mark style="color:blue;">`@IsPositive()`</mark> decorators. We also use this same validation in both fields of the <mark style="color:blue;">`PaginationDto`</mark>, and soon will have more DTOs with fields requiring it too. But is there a way to create a single decorator representing both these validations, so that we may avoid this repetition in the future?

Yes, with **decorator composition**. For this purpose, we'll create the <mark style="color:blue;">`@IsCardinal()`</mark> decorator. So, create the file <mark style="color:purple;">common</mark>/<mark style="color:purple;">decorators</mark>/<mark style="color:purple;">is-cardinal.decorator</mark> with following content:

```typescript
export const IsCardinal = () => applyDecorators(IsInt(), IsPositive());
```

Our composite decorator is already working, but let's make some improvements to it before moving forward.

First, a cardinal number may have two slightly different meanings, the difference being the **inclusion or not** of the **zero** value. In our case, we do <mark style="color:red;">**not**</mark> want to include it. To make this completely clear, we can document our decorator with a **JSDoc**, which is a special comment that adds a readable description to an element. The shortcut for creating one is writing **/\*\***. Add the following above the decorator, to document it. Hovering the cursor over it will now show the description.

```jsdoc
/** Checks if the value is a positive integer greater than zero. */
```

{% hint style="info" %}
We should always strive to write code that is **clear**, **concise**, and **self-documenting**, in order to avoid unnecessary documentation. However, sometimes documentation can help us and other developers to quickly and easily understand a code element.
{% endhint %}

If you hover the cursor over the decorator, you'll notice that its return type is something kind of hard to understand. To easily identify that it is a **property decorator**, its return type can be explicitly defined as <mark style="color:blue;">`PropertyDecorator`</mark>.

Finally, class-validator decorators accept <mark style="color:blue;">`ValidationOptions`</mark> as parameter, with some interesting options such as <mark style="color:blue;">`each`</mark>, which validates if the property is an array of the decorator's type. So, let's also have these options as parameter and pass them along to the encapsulated decorators.

We should have the following result:

```typescript
/** Checks if the value is a positive integer greater than zero. */
export const IsCardinal = (
  validationOptions?: ValidationOptions,
): PropertyDecorator =>
  applyDecorators(IsInt(validationOptions), IsPositive(validationOptions));
```

We can now use this decorator in the <mark style="color:blue;">`IdDto`</mark> such as follows, and after that also in the <mark style="color:blue;">`PaginationDto`</mark>. Hover the cursor over the decorator to see its improved tooltip.

```typescript
@IsCardinal()
id: number;
```

It may not look like much right now, but with bigger combinations of decorators, this pattern is really helpful.

<mark style="color:green;">**Commit**</mark> - Creating a composite decorator
