@lenne.tech/nest-server 11.4.1 → 11.4.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lenne.tech/nest-server",
3
- "version": "11.4.1",
3
+ "version": "11.4.3",
4
4
  "description": "Modern, fast, powerful Node.js web framework in TypeScript based on Nest with a GraphQL API and a connection to MongoDB (or other databases).",
5
5
  "keywords": [
6
6
  "node",
@@ -1,31 +1,87 @@
1
1
  import { ArgumentMetadata, BadRequestException, Injectable, PipeTransform } from '@nestjs/common';
2
2
  import { plainToInstance } from 'class-transformer';
3
3
  import { validate, ValidationError } from 'class-validator';
4
+ import { inspect } from 'util';
4
5
 
5
6
  import { isBasicType } from '../helpers/input.helper';
6
7
 
8
+ // Debug mode can be enabled via environment variable: DEBUG_VALIDATION=true
9
+ const DEBUG_VALIDATION = process.env.DEBUG_VALIDATION === 'true';
10
+
7
11
  @Injectable()
8
12
  export class MapAndValidatePipe implements PipeTransform {
9
13
  async transform(value: any, metadata: ArgumentMetadata) {
10
14
  const { metatype } = metadata;
11
15
 
16
+ if (DEBUG_VALIDATION) {
17
+ console.debug('\n=== MapAndValidatePipe Debug ===');
18
+ console.debug('Metadata:', {
19
+ data: metadata.data,
20
+ metatype: metatype?.name,
21
+ type: metadata.type,
22
+ });
23
+ console.debug('Input value type:', typeof value);
24
+ console.debug('Input value:', inspect(value, { colors: true, depth: 3 }));
25
+ }
26
+
12
27
  if (!value || typeof value !== 'object' || !metatype || isBasicType(metatype)) {
28
+ if (DEBUG_VALIDATION) {
29
+ console.debug('Skipping validation - basic type or no metatype');
30
+ console.debug('=== End Debug ===\n');
31
+ }
13
32
  return value;
14
33
  }
15
34
 
16
35
  // Convert to metatype
17
36
  if (!(value instanceof metatype)) {
18
37
  if ((metatype as any)?.map) {
38
+ if (DEBUG_VALIDATION) {
39
+ console.debug('Using custom map function');
40
+ }
19
41
  value = (metatype as any)?.map(value);
20
42
  } else {
43
+ if (DEBUG_VALIDATION) {
44
+ console.debug('Using plainToInstance to transform to:', metatype.name);
45
+ }
21
46
  value = plainToInstance(metatype, value);
47
+ if (DEBUG_VALIDATION) {
48
+ console.debug('Transformed value:', inspect(value, { colors: true, depth: 3 }));
49
+ console.debug('Transformed value instance of:', value?.constructor?.name);
50
+ }
22
51
  }
23
52
  }
24
53
 
25
54
  // Validate
55
+ if (DEBUG_VALIDATION) {
56
+ console.debug('Starting validation...');
57
+ }
26
58
  const errors = await validate(value, { forbidUnknownValues: false });
59
+
27
60
  if (errors.length > 0) {
61
+ if (DEBUG_VALIDATION) {
62
+ console.debug('Validation errors found:', errors.length);
63
+ console.debug('Raw validation errors:');
64
+ errors.forEach((err, index) => {
65
+ console.debug(`\nError ${index + 1}:`);
66
+ console.debug(' Property:', err.property);
67
+ console.debug(' Value:', err.value);
68
+ console.debug(' Constraints:', err.constraints);
69
+ console.debug(' Children:', err.children?.length || 0);
70
+ if (err.children && err.children.length > 0) {
71
+ console.debug(' Children details:');
72
+ err.children.forEach((child, childIndex) => {
73
+ console.debug(` Child ${childIndex + 1}:`);
74
+ console.debug(' Property:', child.property);
75
+ console.debug(' Value:', child.value);
76
+ console.debug(' Constraints:', child.constraints);
77
+ console.debug(' Has children:', (child.children?.length || 0) > 0);
78
+ });
79
+ }
80
+ });
81
+ }
82
+
28
83
  const result = {};
84
+ const errorSummary: string[] = [];
29
85
 
30
86
  const processErrors = (errorList: ValidationError[], parentKey = '') => {
31
87
  errorList.forEach((e) => {
@@ -35,12 +91,48 @@ export class MapAndValidatePipe implements PipeTransform {
35
91
  processErrors(e.children, key);
36
92
  } else {
37
93
  result[key] = e.constraints;
94
+ // Build error summary without exposing values
95
+ if (e.constraints) {
96
+ const constraintTypes = Object.keys(e.constraints).join(', ');
97
+ errorSummary.push(`${key} (${constraintTypes})`);
98
+ }
38
99
  }
39
100
  });
40
101
  };
41
102
 
42
103
  processErrors(errors);
43
- throw new BadRequestException(result);
104
+
105
+ if (DEBUG_VALIDATION) {
106
+ console.debug('\nProcessed validation result:');
107
+ console.debug(inspect(result, { colors: true, depth: 5 }));
108
+ console.debug('Result is empty:', Object.keys(result).length === 0);
109
+ console.debug('Error summary:', errorSummary);
110
+ console.debug('=== End Debug ===\n');
111
+ }
112
+
113
+ // Create meaningful error message without exposing sensitive values
114
+ let errorMessage = 'Validation failed';
115
+ if (errorSummary.length > 0) {
116
+ const fieldCount = errorSummary.length;
117
+ const fieldWord = fieldCount === 1 ? 'field' : 'fields';
118
+ errorMessage = `Validation failed for ${fieldCount} ${fieldWord}: ${errorSummary.join('; ')}`;
119
+ } else if (errors.length > 0) {
120
+ // Handle case where there are validation errors but no constraints (nested errors only)
121
+ const topLevelProperties = errors.map((e) => e.property).join(', ');
122
+ errorMessage = `Validation failed for properties: ${topLevelProperties} (nested validation errors)`;
123
+ }
124
+
125
+ // Throw with message and validation errors (backward compatible structure)
126
+ // Add message property to result object for better error messages
127
+ throw new BadRequestException({
128
+ message: errorMessage,
129
+ ...result,
130
+ });
131
+ }
132
+
133
+ if (DEBUG_VALIDATION) {
134
+ console.debug('Validation successful - no errors');
135
+ console.debug('=== End Debug ===\n');
44
136
  }
45
137
 
46
138
  return value;