Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

492
Views
javascript/mecanografiado | validador de clase | decoradores de validación personalizados | anular `validationOptions` dentro del método `validator`

Considere el ejemplo proporcionado en la sección Decoradores de validación personalizados :

 // Decorator definition import { registerDecorator, ValidationOptions, ValidationArguments } from 'class-validator'; export function IsLongerThan(property: string, validationOptions?: ValidationOptions) { return function (object: Object, propertyName: string) { registerDecorator({ name: 'isLongerThan', target: object.constructor, propertyName: propertyName, constraints: [property], options: validationOptions, validator: { validate(value: any, args: ValidationArguments) { const [relatedPropertyName] = args.constraints; const relatedValue = (args.object as any)[relatedPropertyName]; return typeof value === 'string' && typeof relatedValue === 'string' && value.length > relatedValue.length; // you can return a Promise<boolean> here as well, if you want to make async validation }, }, }); }; }
 // Decorator usage import { IsLongerThan } from './IsLongerThan'; export class Post { title: string; @IsLongerThan('title', { /* you can also use additional validation options, like "groups" in your custom validation decorators. "each" is not supported */ message: 'Text must be longer than the title', }) text: string; }

¿Cómo puedo establecer/anular la cadena de message de forma condicional, dentro del método de validator ? es posible?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Lo encontré:

 export function IsLongerThan(property: string, validationOptions?: ValidationOptions) { return function (object: Object, propertyName: string) { let message; const validate = function (value: any, args: ValidationArguments) { // Set message based on some validation logic // message = ... } const defaultMessage = function () { return message; } registerDecorator({ name: 'isLongerThan', target: object.constructor, propertyName: propertyName, constraints: [property], options: validationOptions, validator: { validate, defaultMessage }, }); }; }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!