Enforce validation rules on a nested object by referencing the properties of its parent class

How can a field in a child object be validated based on properties of the parent object's structure? For example, in the given structure, only the field first.name is not required when the fields id and dateOfBirth (dob) are defined.

@InputType()
export class GetEligibilityArgs {       
  @Field((type) => NameInput)
  @ValidateNested()
  @Type(() => NameInput)
  name: NameInput;
 
  @Field({ nullable: true })
  @ValidateIf((o: GetEligibilityArgs) => {
    return !(o.name?.first && o.name?.last && o.dateOfBirth);
  })
  @IsNotEmpty({ message: 'id is required' })
  id?: string;

  @Field({ nullable: true })
  @ValidateIf((o: GetEligibilityArgs) => {   
    return !(o.id && o.name?.first && o.name?.last);
  })
  @IsNotEmpty()
  dateOfBirth?: string;
}

Nested Object

@InputType()
export class NameInput {
  @Field({ nullable: true })
  @IsNotEmpty()
  first?: string;

  @Field({ nullable: true })
  @IsNotEmpty()
  last?: string;
}

Valid Inputs

  • id, name.first, name.last
  • id, name.first, dob
  • id, name.last, dob
  • name.first, name.last, dob

all other inputs are considered invalid

Answer №1

Here is my unique solution, please provide feedback for improvements.

Creating a Custom Validation Constraint

@ValidatorConstraint()
export class CustomValidationConstraint implements ValidatorConstraintInterface {
  validate(
    { id, name, dateOfBirth }: ValidationArgs,
    validationArguments: ValidationArguments,
  ) {
    if (id && name?.first && name?.last) {
      return true;
    }
    if (id && name?.first && dateOfBirth) {
      return true;
    }
    if (id && name?.last && dateOfBirth) {
      return true;
    }
    if (name?.first && name?.last && dateOfBirth) {
      return true;
    }
    return false;
  }

  defaultMessage(args: ValidationArguments) {
    return 'Eligibility requires 3 of the following 4 fields (id, name.first, name.last, dateOfBirth)';
  }
}

Wrapper Class for Eligibility Criteria Validation

@ArgsType()
export class EligibilityCriteria {
  @Field((type) => ValidationArgs)
  @Validate(CustomValidationConstraint)
  @Type(() => ValidationArgs)
  @ValidateNested()
  criteria: ValidationArgs;
}

Usage in Resolver Query

 @Query((returns) => Eligibility)
  eligibility(@Args() { criteria }: EligibilityCriteria) {
    // perform mapping and call service 
  }

Standard validator constraints are applied to the ValidationArgs class

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

Issue with Angular: boolean value remains unchanged

Currently, I'm encountering an issue with my application. My objective is to establish a list containing checkboxes that toggle their values between true and false when clicked. Sounds simple enough, right? Below is the HTML code snippet: <l ...

How can I bind the ID property of a child component from a parent component in Angular 2 using @Input?

I have a unique requirement in my parent component where I need to generate a child component with a distinct ID, and then pass this ID into the child component. The purpose of passing the unique ID is for the child component to use it within its template. ...

After the transition from Angular 8 to Angular 9, an issue arose with the node_modules/@zerohouse/router-tab/zerohouse-router-tab.d.ts file, as it was not declared

Error Image package.json { "name": "client", "version": "0.0.0", "license": "MIT", "scripts": { "ng": "ng", "serveapp": "ng serve ...

Searching and adding new elements to a sorted array of objects using binary insertion algorithm

I'm currently working on implementing a method to insert an object into a sorted array using binary search to determine the correct index for the new object. You can view the code on codesanbox The array I have is sorted using the following comparis ...

Switching a material-ui input control instead of a textfield for materials-ui-datepicker: the ultimate guide

Within my React application (v16.3), I am utilizing the DatePicker component from the material-ui-pickers library to render date-picker controls. This particular component renders a Material-UI TextField. However, I am interested in modifying it to only di ...

The Tanstack react-table feature is limited in its ability to output tsx from the cell

Currently conducting a test on Tanstack react-table library using React and TypeScript. It appears that I am encountering an issue with returning tsx/jsx from the cell function of ColumnDef: https://i.sstatic.net/d5X3y.png Is there something crucial that ...

The compatibility between cross-fetch and React Native is currently not supported

I have developed an API wrapper that utilizes fetch to carry out the API requests. To ensure compatibility with Browsers, Node, and React Native, I incorporate cross-fetch. When testing the wrapper in Node, everything works fine. However, when using it in ...

Performing simultaneous document queries within a single API in MongoDB

I am currently working with an API written in typescript and attempting to execute parallel queries for the same document by using Promise.allSettled. However, I have noticed that it is performing poorly and seems to be running sequentially instead of in p ...

The type 'Observable<boolean>' cannot be assigned to type 'Observable<UserRegistration>'

function completeRegistration(email: string, password: string, firstName: string, lastName: string, location: string): Observable<UserDetails> { let body = JSON.stringify({ email, password, firstName, lastName,location }); let headers = new H ...

Find out if OpenAI's chat completion feature will trigger a function call or generate a message

In my NestJS application, I have integrated a chat feature that utilizes the openai createChatCompletion API to produce responses based on user input and send them back to the client in real-time. Now, with the introduction of function calls in the openai ...

Angular 11 along with RxJS does not support the combineLatest method in the specified type

Hey there, I'm currently working on utilizing the combineLatest operator to merge two streams in Angular, but I keep encountering an error message stating that "combineLatest does not exist on type". I've attempted to move the code into a .pipe() ...

Error encountered: The term 'interface' is a restricted keyword

I am in the process of developing a NodeJS and MongoDB library for performing CRUD operations on APIs. My goal is to establish an interface with Typescript that includes the url and database name, structured as follows: However, I am encountering this par ...

Is it possible to conceal dom elements within an ng-template?

Utilizing ng-bootstrap, I am creating a Popover with HTML and bindings. However, the ng-template keeps getting recreated every time I click the button, causing a delay in the initialization of my component. Is there a way to hide the ng-template instead? ...

Checking if a route path is present in an array using Typescript and React

Here is a sample of my array data (I have simplified it here, but there are approximately 100 elements with about 20 values each): 0: odata.type: "SP.Data.ProductListItem" Title: "This is Product 1" Id: 1 1: odata.type: "SP.Data.ProductListItem" Title: ...

Angular - Acquire reference to the <audio> element

Is there a way to access the methods of an audio tag within my component in order to implement play and pause functions based on click events? The current method I tried does not allow me to access the play() function. How can I correctly approach this? ...

Is there a way to position the label to the left side of the gauge?

Is there a way to position the zero number outside the gauge? I'm having trouble figuring out how to do it because the x & y options won't work since the plotLine's position keeps changing. The zero needs to move along with the plotLine and ...

Issue with Angular Material Mat Horizontal Stepper: Incorrect step selection when progressing with next button

I have a horizontal mat stepper that needs to be controlled using Next and Back buttons. The steps are generated using ngFor. I have created a variable called "stepIndex" which is bound to the "selectedIndex" input. The Next button should increment this va ...

The sourcemap for a Vue file based on TypeScript is not available due to the presence of the lang="ts" attribute

I am facing an issue where I need to transition my vue files from JavaScript to TypeScript. Currently, they have a standard structure like this: <template> ... </template> <script> ... </script> To use them with TypeScript, I le ...

Determining the type of index to use for an interface

Imagine having an interface called Animal, with some general properties, and then either be a cat or a dog with corresponding properties. interface Dog { dog: { sound: string; } } interface Cat { cat: { lives: number; } } type CatOrDog = Cat | D ...

Displayed even when data is present, the PrimeNg empty message persists

I have set up a PrimeNg table to display data with an empty message template like this: <ng-template pTemplate="emptymessage"> <tr> <td> No records found </td> </tr> </ng-template> ...