@maioradv/nestjs-core 2.3.1 → 2.3.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.
|
@@ -38,7 +38,7 @@ const IsTranslationClause = () => {
|
|
|
38
38
|
};
|
|
39
39
|
exports.IsTranslationClause = IsTranslationClause;
|
|
40
40
|
const parseValue = (raw) => {
|
|
41
|
-
const trimmed = raw
|
|
41
|
+
const trimmed = raw?.trim();
|
|
42
42
|
if (trimmed === 'true')
|
|
43
43
|
return true;
|
|
44
44
|
if (trimmed === 'false')
|
|
@@ -61,35 +61,48 @@ const METADATA_OPERATORS = [
|
|
|
61
61
|
'array_contains'
|
|
62
62
|
];
|
|
63
63
|
const allowedOperators = new Set(METADATA_OPERATORS);
|
|
64
|
+
const getMetadataClauseErrors = (item, index, property) => {
|
|
65
|
+
if (!item)
|
|
66
|
+
return [`${property}[${index}] is invalid`];
|
|
67
|
+
const errors = [];
|
|
68
|
+
const hasPath = Array.isArray(item.path) && item.path.some(part => part.trim().length > 0);
|
|
69
|
+
const hasValue = item.value !== undefined && item.value !== '';
|
|
70
|
+
if (!hasPath)
|
|
71
|
+
errors.push(`${property}[${index}].path must not be empty`);
|
|
72
|
+
if (!allowedOperators.has(item.operator)) {
|
|
73
|
+
errors.push(`${property}[${index}].operator must be one of: ${METADATA_OPERATORS.join(', ')}`);
|
|
74
|
+
}
|
|
75
|
+
if (!hasValue)
|
|
76
|
+
errors.push(`${property}[${index}].value must not be empty`);
|
|
77
|
+
return errors;
|
|
78
|
+
};
|
|
64
79
|
const IsMetadataClause = () => {
|
|
65
80
|
return (0, common_1.applyDecorators)((0, swagger_1.ApiPropertyOptional)({
|
|
66
81
|
type: String,
|
|
67
82
|
description: 'Metadata filter format: path:operator:value, multiple with ,. Example: user.id:equals:1,tags:array_contains:vip',
|
|
68
|
-
}), (0, class_transformer_1.Transform)(({ value }) => {
|
|
83
|
+
}), (0, class_transformer_1.Transform)(({ value, key }) => {
|
|
69
84
|
if (!value)
|
|
70
85
|
return [];
|
|
71
|
-
|
|
86
|
+
const clauses = value
|
|
72
87
|
.split(',')
|
|
73
88
|
.map(c => c.trim())
|
|
74
89
|
.filter(Boolean)
|
|
75
90
|
.map(clause => {
|
|
76
|
-
const [rawPath, rawOperator, ...rawValue] = clause.split(':');
|
|
91
|
+
const [rawPath, rawOperator, ...rawValue] = clause.trim().split(':');
|
|
77
92
|
const path = rawPath?.trim();
|
|
78
93
|
const operator = rawOperator?.trim();
|
|
79
94
|
const value = rawValue.join(':').trim();
|
|
80
|
-
if (!path)
|
|
81
|
-
throw new common_1.BadRequestException(`Path empty`);
|
|
82
|
-
if (!allowedOperators.has(operator)) {
|
|
83
|
-
throw new common_1.BadRequestException(`Invalid metadata operator: ${operator} must be one of ${METADATA_OPERATORS.join()}`);
|
|
84
|
-
}
|
|
85
95
|
const isNull = ((operator === 'equals' || operator === 'not') && value === 'null') ? true : false;
|
|
86
|
-
|
|
96
|
+
return {
|
|
87
97
|
path: path.split('.'),
|
|
88
98
|
operator: operator,
|
|
89
99
|
value: (isNull ? null : parseValue(value))
|
|
90
100
|
};
|
|
91
|
-
return result;
|
|
92
101
|
});
|
|
102
|
+
const errors = clauses.flatMap((item, index) => getMetadataClauseErrors(item, index, key));
|
|
103
|
+
if (errors.length)
|
|
104
|
+
throw new common_1.BadRequestException(errors);
|
|
105
|
+
return clauses;
|
|
93
106
|
}), (0, class_validator_1.IsOptional)(), (0, class_validator_1.IsArray)());
|
|
94
107
|
};
|
|
95
108
|
exports.IsMetadataClause = IsMetadataClause;
|