framework-do-dede 5.5.1 → 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):
|
|
@@ -1,8 +1,9 @@
|
|
|
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 ?? {})
|
|
@@ -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
|
+
}
|