Tips on designing unique field validation decorators in NestJS using GraphQL

I'm currently developing a NestJS API using apollo-server-express and have created an InputType for appointments as shown below:

@InputType()
export class AppointmentInput {
    @Field(of => String)
    @IsNotEmpty()
    name: string;

    @Field(of => String)
    @IsNotEmpty()
    @IsDateString()
    dateStart: string;

    @Field(of => String)
    @IsNotEmpty()
    @IsDateString()
    dateEnd: string;

    @Field(of => Boolean)
    @IsBoolean()
    paid: boolean;

    @Field(of => Int)
    idDoctor: number;
    
    @Field(of => Int)
    idPatient: number;
    @Field(of => Int)
    idService: number;
}

If I want to validate if the name contains the letter 'a' using a Pipe, I can create a custom pipe like this:

import { PipeTransform, Injectable, ArgumentMetadata, HttpException, HttpStatus } from '@nestjs/common';

@Injectable()
export class NamePipe implements PipeTransform<string, string>{
  transform(name: string, metadata: ArgumentMetadata) {
    if (name.includes('a')) {
      throw new HttpException('The name contains the letter "a"', HttpStatus.BAD_REQUEST);
    }
    return name;
  }
}

However, when I try to use this pipe as a decorator along with validation decorators, I encounter the following error:

The return type of a property decorator function must be either 'void' or 'any'. Unable to resolve signature of property decorator when called as an expression. Type 'TypedPropertyDescriptor' is not assignable to type 'void'.

To overcome this issue and successfully implement custom decorators for field validation, I need to find a way to make my decorators capable of calling services. This will allow me to perform more complex validations in the future, such as checking database records for specific data before proceeding.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a custom Higher Order Component to seamlessly connect react-relay and react-router using TypeScript

Hey there! So, my Frankenstein monster project has decided to go rogue and I'm running out of hair to pull out. Any help would be greatly appreciated. I've been working on setting up a simple app with React, React-Router, React-Relay, and Typesc ...

What is the process for implementing dynamic routing in Next.js using slugs pulled from a GraphQL API?

Struggling with understanding dynamic routing based on variables? Having difficulty retrieving individual items and their fields for a single page with Next.js? You're not alone. Background Operating a KeystoneJS headless CMS with a GraphQL API, the ...

Error in Angular 5: Google Maps not defined

Having trouble implementing Google Maps on my Angular 5 app. Upon loading the view, I am encountering this error in the JavaScript console: LoginComponent_Host.ngfactory.js? [sm]:1 ERROR ReferenceError: google is not defined at LoginComponent.ngAfterVie ...

Choose a date range from the date picker

I am currently attempting to combine two dates using a rangepicker. Below is the command used to select the date: Cypress.Commands.add('setDatePickerDate', (selector, date) => { const monthsShort = [ 'janv.', 'févr.& ...

Transform the date format in react.js using information provided by an API endpoint

I'm currently working on a project using React and TypeScript where I need to format the date retrieved from an API. I am able to map over the data and display it, but I'm struggling to convert it into a format like '22 June 2021'. The ...

What is the best way to invoke a function in a React component from another component?

I am working with a component structure that includes the Input component. In this component, there is a function called validate that is triggered by the onChange event. Here is a snippet of the code: https://i.sstatic.net/WjCLy.png import React, {FC, us ...

Tips for modifying the text color of a div that is being iterated through using ngFor

I am facing an issue with a div that is being looped using ngFor. What I want to achieve is when a user clicks on one of the divs in the loop, only that particular div should change its text color. If another div is clicked, it should change color while th ...

How to format a Date object as yyyy-MM-dd when serializing to JSON in Angular?

I have a situation where I need to display an object's 'birthDate' property in the format DD/MM/YYYY on screen. The data is stored in the database as YYYY-MM-DD, which is a string type. I am currently using bootstrap bsDatePicker for this ta ...

Struggling with consolidating values in an array of objects - seeking assistance with Javascript

Currently, I am handling a project where I receive data in the form of an Object Array. My task is to merge values with the same key into one key and convert the values into an array of strings. Below is the sample data I am working with: inputArray = [ ...

Typescript: The original type cannot be indexed with a type-mapped type

I'm currently working on a class where I need to define a method that returns an object with keys based on the generic type inferred by the compiler. However, I've encountered an issue with the code snippet below. The compiler is indicating that ...

Tips on Calculating the Number of Object Properties and Presenting Each Value Individually on a Dynamic Button with Click Event Attached

When it comes to displaying dynamic data with markers on a map, everything works fine up until this point. However, I've encountered some issues that I'm currently stuck on and not sure how to proceed. Dynamic data: { "page": 2, "data" ...

Learn how to implement AuthContext and createDataContext in React Native Expo development using TypeScript

AuthContext.tsx import createDataContext from './createDataContext'; import serverApi from '../api/server'; const authReducer = ({state, action}: any) => { switch(action.type){ default: return state; } } ...

Specialized Character Formats in TypeScript

In my quest to enhance the clarity in distinguishing different types of strings within my program - such as absolute paths and relative paths, I am seeking a solution that ensures functions can only take or return specific types without errors. Consider t ...

Create Angular file structures effortlessly using a tool similar to Rails scaffold

Is there a code generator in Angular similar to RoR's rails scaffold? I am looking to run a specific command and receive the following files, such as: *.component.html *.component.sass *.component.ts *.module.ts. ...

What is the best way to separate the user interface module from the logic in Angular2?

I am diving into Angular2 for the first time and have been tasked with creating a module using UI components like @angular/material. The goal is to shield my team members from the complexities of the UI framework I choose, allowing them to focus solely on ...

Angular does not seem to support drop and drag events in fullCalendar

I am looking to enhance my fullCalendar by adding a drag and drop feature for the events. This feature will allow users to easily move events within the calendar to different days and times. Below is the HTML code I currently have: <p-fullCalendar deep ...

Is there a way to halt the current traversal of visitEachChild in TypeScript Transformer API?

While navigating through each child node of a parent node using visitEachChild, is there a way to stop the process when I no longer wish to visit the subsequent child nodes? For example: Parent node Node 1 Node 2 <-- My target point. Node 3 Node 4 Nod ...

Utilizing Radio buttons to establish default values - a step-by-step guide

I am utilizing a Map to store the current state of my component. This component consists of three groups, each containing radio buttons. To initialize default values, I have created an array: const defaultOptions = [ { label: "Mark", value: & ...

Utilize TypeScript in creating a Chrome extension to fetch elements by their ID

I'm currently working on a Chrome extension using Angular and Typescript, and I have encountered an issue with accessing the document element by its id from the active tab. While this works perfectly fine in JavaScript, I am facing difficulties in ach ...

Utilize TypeScript to match patterns against either a string or Blob (type union = string | Blob)

I have created a union type called DataType, type TextData = string type BinaryData = Blob type DataType = TextData | BinaryData Now, I want to implement it in a function function processData(data: DataType): void { if (data instanceof TextData) ...