class-validator icon indicating copy to clipboard operation
class-validator copied to clipboard

question: How to use @Param('id') in DTO class

Open abdul15irsyad opened this issue 3 years ago • 1 comments

i want to make isUnique with custom validator, it already works but in update case it doesn't works because i need the id of the entity that i update to exclude it from checking the field, or there is another example to implement isUnique in NestJS with typeOrm? thanks

abdul15irsyad avatar Sep 01 '22 11:09 abdul15irsyad

Could you explain a little more clearly what you want to do, and perhaps write a little example?

Is it possible to implement what you need using ValidateIf?

import { customValidator } from './my-custom-validator.js';
import * as v from 'class-validator';

export class MyClass {
  
  @v.IsString()
  @v.IsOptional()
  id?: string;

  // the validators on this field will only be checked if id is not undefined 
  // that is, if ValidateIf's callback function returns true
  @v.ValidateIf((object: MyClass) => object.id !== undefined)
  @v.Validate(customValidator)
  myField: string
}

braaar avatar Sep 02 '22 09:09 braaar

Sorry, I cannot understand your problem. Closing this as cannot reproduce.

If the issue still persists, you may open a new Q&A in the discussions tab and someone from the community may be able to help.

NoNameProvided avatar Nov 13 '22 14:11 NoNameProvided

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Dec 14 '22 00:12 github-actions[bot]