framework-do-dede 5.5.0 → 5.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -111,6 +111,11 @@ Opções de rota (comuns):
111
111
  - `responseType`: `"json" | "text" | "html"`
112
112
  - `validator`: pode ser uma classe com decorators do `class-validator` **ou** um objeto com `validate(data)` (sync/async)
113
113
 
114
+ Nota sobre `validator`:
115
+ - Apenas propriedades com decorators do `class-validator` são transformadas pelo `class-transformer`.
116
+ - Propriedades sem decorators têm o valor original preservado.
117
+ - Se você precisa transformar um campo opcional, adicione pelo menos um decorator do `class-validator` (ex.: `@IsOptional()`).
118
+
114
119
  ### Versionamento e Prefixo
115
120
 
116
121
  Você pode definir um prefixo e uma versão global ao criar a aplicação. A versão vira `v{numero}` logo após o prefixo (se existir):
@@ -44,7 +44,7 @@ export default class ControllerHandler {
44
44
  validatorLike = validator;
45
45
  }
46
46
  if (typeof validatorLike === 'function') {
47
- await validateWithClassValidator(validatorLike, request.data, options);
47
+ request.data = await validateWithClassValidator(validatorLike, request.data, options);
48
48
  }
49
49
  else if (typeof validatorLike.validate === 'function') {
50
50
  await validatorLike.validate(request.data);
@@ -5,4 +5,4 @@ export type ValidationErrorOptions = {
5
5
  errorName?: string;
6
6
  validatorOptions?: ValidatorOptions;
7
7
  };
8
- export declare function validateWithClassValidator<T extends object>(dtoClass: new () => T, input: T, options?: ValidationErrorOptions): Promise<void>;
8
+ export declare function validateWithClassValidator<T extends object>(dtoClass: new () => T, input: T, options?: ValidationErrorOptions): Promise<T>;
@@ -1,15 +1,16 @@
1
1
  import { CustomServerError } from '../../http/errors/server';
2
2
  import { plainToInstance } from 'class-transformer';
3
- import { validate } from 'class-validator';
3
+ import { getMetadataStorage, validate } from 'class-validator';
4
4
  export async function validateWithClassValidator(dtoClass, input, options = {}) {
5
5
  const instance = plainToInstance(dtoClass, input);
6
+ restoreExtraneousInput(instance, input, dtoClass);
6
7
  const validatorOptions = {
7
8
  forbidUnknownValues: false,
8
9
  ...(options.validatorOptions ?? {})
9
10
  };
10
11
  const errors = await validate(instance, validatorOptions);
11
12
  if (errors.length === 0)
12
- return;
13
+ return instance;
13
14
  const details = flattenErrors(errors);
14
15
  const statusCode = options.statusCode ?? 400;
15
16
  const errorName = options.errorName ?? 'BadRequest';
@@ -28,3 +29,16 @@ function flattenErrors(errors, parentPath = '') {
28
29
  }
29
30
  return output;
30
31
  }
32
+ function restoreExtraneousInput(instance, input, dtoClass) {
33
+ if (!input || typeof input !== 'object')
34
+ return;
35
+ const metadataStorage = getMetadataStorage();
36
+ const metadatas = metadataStorage.getTargetValidationMetadatas(dtoClass, '', false, false);
37
+ const validatedProps = new Set(metadatas.map(meta => meta.propertyName));
38
+ for (const key of Object.keys(input)) {
39
+ if (!validatedProps.has(key)) {
40
+ ;
41
+ instance[key] = input[key];
42
+ }
43
+ }
44
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "framework-do-dede",
3
- "version": "5.5.0",
3
+ "version": "5.5.2",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",