feature: set default option for plainToClass and others
Description
Passing options such as excludeExtraneousValues to every plainToClass call is too tiring and leads to bug. I want to set default options.
import { setClassTransformOptions, plainToClass }
setClassTransformOptions({excludeExtraneousValues: true})
const obj = plainToClass(User, userJson) // behaves as plainToClass(User, userJson, {excludeExtraneousValues: true})
https://github.com/typestack/class-transformer/blob/develop/src/ClassTransformer.ts#L20 In other words, I want to change this empty object in some way.
Proposed solution
I have two idea to implement this.
- Like above, setClassTransformOptions saves default options. it is passed to ClassTransformer.
- create ClassTransformer field to store default options and uses it when creating ClassTransformer. Users need to create own TransformOperationExecutor to use default options.
I have no idea about behavior when both default options and options passed by method exist.
- default options are entirely ignored
- merge both options
The advice for this scenario to wrapp class-transformer in a custom function which does do that:
import { plainToClass as originalPlainToClass } from 'class-transformer';
import { defaultOptions } from './your-custom-options.constant'
function plainToClass(obj, option) {
return originalPlainToClass(User, userJson, { ...defaultOptions, ...options});
}
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.