@maioradv/nestjs-core 2.3.1 → 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,3 +1,4 @@
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;
3
4
  declare const METADATA_OPERATORS: readonly ["equals", "not", "gt", "gte", "lt", "lte", "string_contains", "string_starts_with", "string_ends_with", "array_contains"];
@@ -8,4 +9,8 @@ export type MetadataFilter = {
8
9
  value: null | string | boolean | number;
9
10
  };
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
+ }
11
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.IsMetadataClause = 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");
@@ -61,6 +67,21 @@ const METADATA_OPERATORS = [
61
67
  'array_contains'
62
68
  ];
63
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
+ };
64
85
  const IsMetadataClause = () => {
65
86
  return (0, common_1.applyDecorators)((0, swagger_1.ApiPropertyOptional)({
66
87
  type: String,
@@ -73,23 +94,33 @@ const IsMetadataClause = () => {
73
94
  .map(c => c.trim())
74
95
  .filter(Boolean)
75
96
  .map(clause => {
76
- const [rawPath, rawOperator, ...rawValue] = clause.split(':');
97
+ const [rawPath, rawOperator, ...rawValue] = clause.trim().split(':');
77
98
  const path = rawPath?.trim();
78
99
  const operator = rawOperator?.trim();
79
100
  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
101
  const isNull = ((operator === 'equals' || operator === 'not') && value === 'null') ? true : false;
86
- const result = {
102
+ return {
87
103
  path: path.split('.'),
88
104
  operator: operator,
89
105
  value: (isNull ? null : parseValue(value))
90
106
  };
91
- return result;
92
107
  });
93
- }), (0, class_validator_1.IsOptional)(), (0, class_validator_1.IsArray)());
108
+ }), (0, class_validator_1.IsOptional)(), (0, class_validator_1.IsArray)(), (0, class_validator_1.Validate)(IsMetadata));
94
109
  };
95
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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maioradv/nestjs-core",
3
- "version": "2.3.1",
3
+ "version": "2.3.2",
4
4
  "description": "NestJS helpers by MaiorADV",
5
5
  "repository": "https://github.com/maioradv/nestjs-core.git",
6
6
  "author": "Maior ADV Srl",