@maioradv/nestjs-core 2.3.0 → 2.3.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.
|
@@ -1,2 +1,16 @@
|
|
|
1
|
+
import { ValidatorConstraintInterface, ValidationArguments } from "class-validator";
|
|
1
2
|
export declare const IsMetafieldClause: () => <TFunction extends Function, Y>(target: TFunction | object, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
|
|
2
3
|
export declare const IsTranslationClause: () => <TFunction extends Function, Y>(target: TFunction | object, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
|
|
4
|
+
declare const METADATA_OPERATORS: readonly ["equals", "not", "gt", "gte", "lt", "lte", "string_contains", "string_starts_with", "string_ends_with", "array_contains"];
|
|
5
|
+
export type MetadataOperator = typeof METADATA_OPERATORS[number];
|
|
6
|
+
export type MetadataFilter = {
|
|
7
|
+
path: string[];
|
|
8
|
+
operator: MetadataOperator;
|
|
9
|
+
value: null | string | boolean | number;
|
|
10
|
+
};
|
|
11
|
+
export declare const IsMetadataClause: () => <TFunction extends Function, Y>(target: TFunction | object, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
|
|
12
|
+
export declare class IsMetadata implements ValidatorConstraintInterface {
|
|
13
|
+
validate(value: MetadataFilter[]): boolean;
|
|
14
|
+
defaultMessage(args: ValidationArguments): string;
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
2
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.IsTranslationClause = exports.IsMetafieldClause = void 0;
|
|
9
|
+
exports.IsMetadata = exports.IsMetadataClause = exports.IsTranslationClause = exports.IsMetafieldClause = void 0;
|
|
4
10
|
const common_1 = require("@nestjs/common");
|
|
5
11
|
const swagger_1 = require("@nestjs/swagger");
|
|
6
12
|
const class_transformer_1 = require("class-transformer");
|
|
@@ -37,3 +43,84 @@ const IsTranslationClause = () => {
|
|
|
37
43
|
})), (0, class_validator_1.IsOptional)(), (0, class_validator_1.IsArray)());
|
|
38
44
|
};
|
|
39
45
|
exports.IsTranslationClause = IsTranslationClause;
|
|
46
|
+
const parseValue = (raw) => {
|
|
47
|
+
const trimmed = raw.trim();
|
|
48
|
+
if (trimmed === 'true')
|
|
49
|
+
return true;
|
|
50
|
+
if (trimmed === 'false')
|
|
51
|
+
return false;
|
|
52
|
+
const num = Number(trimmed);
|
|
53
|
+
if (!Number.isNaN(num) && trimmed !== '')
|
|
54
|
+
return num;
|
|
55
|
+
return trimmed;
|
|
56
|
+
};
|
|
57
|
+
const METADATA_OPERATORS = [
|
|
58
|
+
'equals',
|
|
59
|
+
'not',
|
|
60
|
+
'gt',
|
|
61
|
+
'gte',
|
|
62
|
+
'lt',
|
|
63
|
+
'lte',
|
|
64
|
+
'string_contains',
|
|
65
|
+
'string_starts_with',
|
|
66
|
+
'string_ends_with',
|
|
67
|
+
'array_contains'
|
|
68
|
+
];
|
|
69
|
+
const allowedOperators = new Set(METADATA_OPERATORS);
|
|
70
|
+
const getMetadataClauseErrors = (item, index, property) => {
|
|
71
|
+
if (!item)
|
|
72
|
+
return [`${property}[${index}] is invalid`];
|
|
73
|
+
const errors = [];
|
|
74
|
+
const hasPath = Array.isArray(item.path) && item.path.some(part => part.trim().length > 0);
|
|
75
|
+
const hasValue = item.value !== undefined && item.value !== '';
|
|
76
|
+
if (!hasPath)
|
|
77
|
+
errors.push(`${property}[${index}].path must not be empty`);
|
|
78
|
+
if (!allowedOperators.has(item.operator)) {
|
|
79
|
+
errors.push(`${property}[${index}].operator must be one of: ${METADATA_OPERATORS.join(', ')}`);
|
|
80
|
+
}
|
|
81
|
+
if (!hasValue)
|
|
82
|
+
errors.push(`${property}[${index}].value must not be empty`);
|
|
83
|
+
return errors;
|
|
84
|
+
};
|
|
85
|
+
const IsMetadataClause = () => {
|
|
86
|
+
return (0, common_1.applyDecorators)((0, swagger_1.ApiPropertyOptional)({
|
|
87
|
+
type: String,
|
|
88
|
+
description: 'Metadata filter format: path:operator:value, multiple with ,. Example: user.id:equals:1,tags:array_contains:vip',
|
|
89
|
+
}), (0, class_transformer_1.Transform)(({ value }) => {
|
|
90
|
+
if (!value)
|
|
91
|
+
return [];
|
|
92
|
+
return value
|
|
93
|
+
.split(',')
|
|
94
|
+
.map(c => c.trim())
|
|
95
|
+
.filter(Boolean)
|
|
96
|
+
.map(clause => {
|
|
97
|
+
const [rawPath, rawOperator, ...rawValue] = clause.trim().split(':');
|
|
98
|
+
const path = rawPath?.trim();
|
|
99
|
+
const operator = rawOperator?.trim();
|
|
100
|
+
const value = rawValue.join(':').trim();
|
|
101
|
+
const isNull = ((operator === 'equals' || operator === 'not') && value === 'null') ? true : false;
|
|
102
|
+
return {
|
|
103
|
+
path: path.split('.'),
|
|
104
|
+
operator: operator,
|
|
105
|
+
value: (isNull ? null : parseValue(value))
|
|
106
|
+
};
|
|
107
|
+
});
|
|
108
|
+
}), (0, class_validator_1.IsOptional)(), (0, class_validator_1.IsArray)(), (0, class_validator_1.Validate)(IsMetadata));
|
|
109
|
+
};
|
|
110
|
+
exports.IsMetadataClause = IsMetadataClause;
|
|
111
|
+
let IsMetadata = class IsMetadata {
|
|
112
|
+
validate(value) {
|
|
113
|
+
if (!value?.length)
|
|
114
|
+
return true;
|
|
115
|
+
return value.every((item, index) => getMetadataClauseErrors(item, index, 'metadata').length === 0);
|
|
116
|
+
}
|
|
117
|
+
defaultMessage(args) {
|
|
118
|
+
const value = args.value;
|
|
119
|
+
const errors = value?.flatMap((item, index) => getMetadataClauseErrors(item, index, args.property)) ?? [];
|
|
120
|
+
return errors.join(', ');
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
exports.IsMetadata = IsMetadata;
|
|
124
|
+
exports.IsMetadata = IsMetadata = __decorate([
|
|
125
|
+
(0, class_validator_1.ValidatorConstraint)({ name: 'IsMetadataConstraint', async: false })
|
|
126
|
+
], IsMetadata);
|