Class-validator default value

Class Validator – Default Value

The class-validator library is a popular validation library for JavaScript and TypeScript. When applying validation rules to an input field, it is often necessary to specify a default value for the field. In class-validator, this can be achieved in a few different ways depending on the specific use case.

1. Default Message

The simplest way to set a default value is by specifying a default error message for the validator. For example, if you have a required field and want to set a default value for it, you can do so by providing a default error message along with the validation decorator.


import { IsNotEmpty } from 'class-validator';
   
class ExampleClass {
   @IsNotEmpty({ message: 'Field is required.', each: true })
   field: string = 'Default value';
}
   

In the above example, the field will have a default value of ‘Default value’ if no value is provided by the user. The default error message will be used if the field is empty.

2. Default Value Decorator

Another way to set a default value is by using a custom decorator. This approach allows you to have more control over the default value and its logic. Here is an example of defining a custom decorator to set a default value:


import { ValidationOptions } from 'class-validator';
import { registerDecorator } from 'class-validator';
   
export function DefaultValue(defaultValue: any, validationOptions?: ValidationOptions) {
   return function (object: Object, propertyName: string) {
      registerDecorator({
         name: 'defaultValue',
         target: object.constructor,
         propertyName: propertyName,
         options: validationOptions,
         validator: {
            validate(value: any) {
               if (value === undefined || value === null) {
                  object[propertyName] = defaultValue;
                  return true;
               }
               return true;
            },
         },
      });
   };
}
   
class ExampleClass {
   @DefaultValue('Default value')
   field: string;
}
   

In this example, the `DefaultValue` decorator is created which can be used on any field to set a default value. If the field is undefined or null, the decorator sets the default value provided.

3. Default value in the constructor

If you are using TypeScript and have a constructor for your class, you can set the default value directly in the constructor. Here is an example:


import { IsNotEmpty } from 'class-validator';
   
class ExampleClass {
   @IsNotEmpty({ message: 'Field is required.', each: true })
   field: string;
   
   constructor() {
      this.field = 'Default value';
   }
}
   

This approach sets the default value in the class constructor, ensuring that it is always initialized with the specified value.

These are a few ways to set a default value in class-validator depending on your specific requirements. Choose the approach that best suits your use case and implement it accordingly.

Similar post

Leave a comment