@solidstarters/solid-core 1.2.132 → 1.2.134

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.
@@ -14,6 +14,7 @@ export declare class SelectionDynamicFieldCrudManager implements FieldCrudManage
14
14
  private readonly options;
15
15
  constructor(options: SelectionDynamicFieldOptions);
16
16
  validate(dto: any): Promise<ValidationError[]>;
17
+ private parseAndValidateArray;
17
18
  private applyValidations;
18
19
  private applyFormatValidations;
19
20
  transformForCreate(dto: any): any;
@@ -1 +1 @@
1
- {"version":3,"file":"SelectionDynamicFieldCrudManager.d.ts","sourceRoot":"","sources":["../../../src/helpers/field-crud-managers/SelectionDynamicFieldCrudManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAEhD,OAAO,EAAE,kBAAkB,EAAE,MAAM,oCAAoC,CAAC;AACxE,OAAO,EAAE,gBAAgB,EAAiD,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAElH,MAAM,WAAW,4BAA4B;IACzC,wBAAwB,EAAE,MAAM,CAAC;IACjC,kBAAkB,EAAE,kBAAkB,CAAC;IACvC,QAAQ,EAAE,OAAO,GAAG,SAAS,GAAG,IAAI,CAAC;IACrC,4BAA4B,EAAE,GAAG,CAAC;IAClC,SAAS,EAAE,MAAM,CAAC;IAClB,gBAAgB,EAAE,gBAAgB,CAAC;IACnC,aAAa,EAAE,OAAO,GAAG,SAAS,GAAG,IAAI,CAAC;CAC7C;AAED,qBAAa,gCAAiC,YAAW,gBAAgB;IAEzD,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,EAAE,4BAA4B;IAG5D,QAAQ,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;YA4BtC,gBAAgB;YAUhB,sBAAsB;IAQpC,kBAAkB,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG;IAKjC,OAAO,CAAC,yBAAyB;YAWnB,qBAAqB;IAOnC,OAAO,CAAC,gBAAgB;IAWxB,OAAO,CAAC,yBAAyB;CAGpC"}
1
+ {"version":3,"file":"SelectionDynamicFieldCrudManager.d.ts","sourceRoot":"","sources":["../../../src/helpers/field-crud-managers/SelectionDynamicFieldCrudManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAEhD,OAAO,EAAE,kBAAkB,EAAE,MAAM,oCAAoC,CAAC;AACxE,OAAO,EAAE,gBAAgB,EAAiD,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAElH,MAAM,WAAW,4BAA4B;IACzC,wBAAwB,EAAE,MAAM,CAAC;IACjC,kBAAkB,EAAE,kBAAkB,CAAC;IACvC,QAAQ,EAAE,OAAO,GAAG,SAAS,GAAG,IAAI,CAAC;IACrC,4BAA4B,EAAE,GAAG,CAAC;IAClC,SAAS,EAAE,MAAM,CAAC;IAClB,gBAAgB,EAAE,gBAAgB,CAAC;IACnC,aAAa,EAAE,OAAO,GAAG,SAAS,GAAG,IAAI,CAAC;CAC7C;AAED,qBAAa,gCAAiC,YAAW,gBAAgB;IAEzD,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,EAAE,4BAA4B;IAG5D,QAAQ,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;IAoCpD,OAAO,CAAC,qBAAqB;YAkBf,gBAAgB;YAUhB,sBAAsB;IAQpC,kBAAkB,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG;IAKjC,OAAO,CAAC,yBAAyB;YAWnB,qBAAqB;IAOnC,OAAO,CAAC,gBAAgB;IAWxB,OAAO,CAAC,yBAAyB;CAGpC"}
@@ -10,21 +10,46 @@ class SelectionDynamicFieldCrudManager {
10
10
  async validate(dto) {
11
11
  const fieldValue = dto[this.options.fieldName];
12
12
  const isMultiSelect = this.options?.isMultiSelect;
13
- if (isMultiSelect) {
14
- let values;
15
- try {
16
- values = JSON.parse(fieldValue);
13
+ if (isMultiSelect && fieldValue) {
14
+ const arrayCheck = this.parseAndValidateArray(fieldValue);
15
+ if (!arrayCheck.isValid) {
16
+ return [
17
+ {
18
+ field: this.options.fieldName,
19
+ error: `Field: ${this.options.fieldName} must be a valid array`,
20
+ },
21
+ ];
17
22
  }
18
- catch {
19
- values = [fieldValue];
23
+ const values = arrayCheck.values;
24
+ if (this.isApplyRequiredValidation() && values.length === 0) {
25
+ return [
26
+ {
27
+ field: this.options.fieldName,
28
+ error: `Field: ${this.options.fieldName} is required`,
29
+ },
30
+ ];
20
31
  }
21
- const allErrors = await Promise.all(values.map(value => this.applyValidations(value)));
32
+ const allErrors = await Promise.all(values.map((val) => this.applyValidations(val)));
22
33
  return allErrors.flat();
23
34
  }
24
35
  else {
25
36
  return this.applyValidations(fieldValue);
26
37
  }
27
38
  }
39
+ parseAndValidateArray(fieldValue) {
40
+ if (Array.isArray(fieldValue)) {
41
+ return { isValid: true, values: fieldValue };
42
+ }
43
+ try {
44
+ const parsed = typeof fieldValue === 'string' ? JSON.parse(fieldValue) : null;
45
+ if (Array.isArray(parsed)) {
46
+ return { isValid: true, values: parsed };
47
+ }
48
+ }
49
+ catch {
50
+ }
51
+ return { isValid: false, values: [] };
52
+ }
28
53
  async applyValidations(fieldValue) {
29
54
  const errors = [];
30
55
  this.isApplyRequiredValidation() && (0, class_validator_1.isEmpty)(fieldValue) ? errors.push({ field: this.options.fieldName, error: `Field: ${this.options.fieldName} is required` }) : "no errors";
@@ -1 +1 @@
1
- {"version":3,"file":"SelectionDynamicFieldCrudManager.js","sourceRoot":"","sources":["../../../src/helpers/field-crud-managers/SelectionDynamicFieldCrudManager.ts"],"names":[],"mappings":";;;AACA,qDAAuE;AACvE,oFAAwE;AAaxE,MAAa,gCAAgC;IAEzC,YAA6B,OAAqC;QAArC,YAAO,GAAP,OAAO,CAA8B;IAClE,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,GAAQ;QACnB,MAAM,UAAU,GAA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EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,4BAA4B,CAAC,CAAC,CAAC;QAExG,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;IACzD,CAAC;IAEO,gBAAgB,CAAsC,wBAAgC;QAC1F,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,gBAAgB;aACzC,YAAY,EAAE;aACd,MAAM,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,KAAK,wBAAwB,CAAC;aAChE,GAAG,EAAE,CAAC;QACX,IAAI,CAAC,QAAQ,EAAE,CAAC;YACZ,MAAM,IAAI,KAAK,CAAC,gBAAgB,wBAAwB,YAAY,CAAC,CAAC;QAC1E,CAAC;QACD,OAAO,QAAQ,CAAC,QAAiC,CAAC;IACtD,CAAC;IAEO,yBAAyB;QAC7B,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;IACjC,CAAC;CACJ;AAxFD,4EAwFC","sourcesContent":["import { DiscoveryService } from \"@nestjs/core\";\nimport { isEmpty, isInt, isNotEmpty, isString } from \"class-validator\";\nimport { SelectionValueType } from \"src/dtos/create-field-metadata.dto\";\nimport { FieldCrudManager, ISelectionProvider, ISelectionProviderContext, ValidationError } from \"src/interfaces\";\n\nexport interface SelectionDynamicFieldOptions {\n selectionDynamicProvider: string;\n selectionValueType: SelectionValueType;\n required: boolean | undefined | null;\n selectionDynamicProviderCtxt: any;\n fieldName: string;\n discoveryService: DiscoveryService;\n isMultiSelect: boolean | undefined | null;\n}\n\nexport class SelectionDynamicFieldCrudManager implements FieldCrudManager {\n\n constructor(private readonly options: SelectionDynamicFieldOptions) {\n }\n\n async validate(dto: any): Promise<ValidationError[]> {\n const fieldValue: any = dto[this.options.fieldName];\n const isMultiSelect = this.options?.isMultiSelect;\n\n // return this.applyValidations(fieldValue);\n // Handle multi-select scenario\n if (isMultiSelect) {\n let values: any[];\n\n try {\n // Try to parse the field value, which should be a JSON stringified array\n values = JSON.parse(fieldValue);\n } catch {\n // If parsing fails, fallback to a single value\n values = [fieldValue];\n }\n\n // Apply validations to each value asynchronously\n const allErrors = await Promise.all(values.map(value => this.applyValidations(value)));\n\n // Flatten the array of errors and return\n return allErrors.flat();\n } else {\n // For non-multi-select, apply validations to the single field value\n return this.applyValidations(fieldValue);\n }\n }\n\n private async applyValidations(fieldValue: any): Promise<ValidationError[]> {\n const errors: ValidationError[] = [];\n this.isApplyRequiredValidation() && isEmpty(fieldValue) ? errors.push({ field: this.options.fieldName, error: `Field: ${this.options.fieldName} is required` }) : \"no errors\";\n if (isNotEmpty(fieldValue)) {\n const formatErrors = await this.applyFormatValidations(fieldValue);\n errors.push(...formatErrors);\n }\n return errors;\n }\n\n private async applyFormatValidations(fieldValue: any): Promise<ValidationError[]> {\n const errors: ValidationError[] = [];\n !this.isValidSelectionValueType(fieldValue, this.options.selectionValueType) ? errors.push({ field: this.options.fieldName, error: 'Field value type is invalid' }) : \"no errors\";\n const _isValidSelectionValue = await this.isValidSelectionValue(fieldValue, this.options.selectionDynamicProvider)\n !_isValidSelectionValue ? errors.push({ field: this.options.fieldName, error: 'Field value is invalid' }) : \"no errors\";\n return errors;\n }\n\n transformForCreate(dto: any): any {\n return dto;\n }\n\n // Validation to be applied\n private isValidSelectionValueType(fieldValue: any, selectionValueType: SelectionValueType): boolean {\n switch (selectionValueType) {\n case SelectionValueType.string:\n return isString(fieldValue);\n case SelectionValueType.int:\n return isInt(fieldValue);\n default:\n throw new Error(`Validation for selection value type ${selectionValueType} is not implemented`);\n }\n }\n\n private async isValidSelectionValue(fieldValue: any, selectionDynamicProvider: string): Promise<boolean> {\n const providerInstance = this.providerInstance<any>(selectionDynamicProvider);\n const values = await providerInstance.values('', JSON.parse(this.options.selectionDynamicProviderCtxt));\n // return values.map(v => v.split(\":\")[0]).includes(fieldValue);\n return values.map(v => v.value).includes(fieldValue);\n }\n\n private providerInstance<T extends ISelectionProviderContext>(selectionDynamicProvider: string): ISelectionProvider<T> {\n const provider = this.options.discoveryService\n .getProviders()\n .filter((provider) => provider.name === selectionDynamicProvider)\n .pop();\n if (!provider) {\n throw new Error(`Provider for ${selectionDynamicProvider} not found`);\n }\n return provider.instance as ISelectionProvider<T>;\n }\n\n private isApplyRequiredValidation(): boolean {\n return this.options.required;\n }\n}"]}
1
+ {"version":3,"file":"SelectionDynamicFieldCrudManager.js","sourceRoot":"","sources":["../../../src/helpers/field-crud-managers/SelectionDynamicFieldCrudManager.ts"],"names":[],"mappings":";;;AACA,qDAAuE;AACvE,oFAAwE;AAaxE,MAAa,gCAAgC;IAEzC,YAA6B,OAAqC;QAArC,YAAO,GAAP,OAAO,CAA8B;IAClE,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,GAAQ;QACnB,MAAM,UAAU,GAAQ,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QAEpD,MAAM,aAAa,GAAG,IAAI,CAAC,OAAO,EAAE,aAAa,CAAC;QAClD,IAAI,aAAa,IAAI,UAAU,EAAE,CAAC;YAC9B,MAAM,UAAU,GAAG,IAAI,CAAC,qBAAqB,CAAC,UAAU,CAAC,CAAC;YAE1D,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;gBACtB,OAAO;oBACH;wBACI,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,SAAS;wBAC7B,KAAK,EAAE,UAAU,IAAI,CAAC,OAAO,CAAC,SAAS,wBAAwB;qBAClE;iBACJ,CAAC;YACN,CAAC;YAED,MAAM,MAAM,GAAG,UAAU,CAAC,MAAM,CAAC;YAEjC,IAAI,IAAI,CAAC,yBAAyB,EAAE,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBAC1D,OAAO;oBACH;wBACI,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,SAAS;wBAC7B,KAAK,EAA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BACvB,OAAO,IAAA,uBAAK,EAAC,UAAU,CAAC,CAAC;YAC7B;gBACI,MAAM,IAAI,KAAK,CAAC,uCAAuC,kBAAkB,qBAAqB,CAAC,CAAC;QACxG,CAAC;IACL,CAAC;IAEO,KAAK,CAAC,qBAAqB,CAAC,UAAe,EAAE,wBAAgC;QACjF,MAAM,gBAAgB,GAAG,IAAI,CAAC,gBAAgB,CAAM,wBAAwB,CAAC,CAAC;QAC9E,MAAM,MAAM,GAAG,MAAM,gBAAgB,CAAC,MAAM,CAAC,EAAE,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,4BAA4B,CAAC,CAAC,CAAC;QAExG,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;IACzD,CAAC;IAEO,gBAAgB,CAAsC,wBAAgC;QAC1F,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,gBAAgB;aACzC,YAAY,EAAE;aACd,MAAM,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,KAAK,wBAAwB,CAAC;aAChE,GAAG,EAAE,CAAC;QACX,IAAI,CAAC,QAAQ,EAAE,CAAC;YACZ,MAAM,IAAI,KAAK,CAAC,gBAAgB,wBAAwB,YAAY,CAAC,CAAC;QAC1E,CAAC;QACD,OAAO,QAAQ,CAAC,QAAiC,CAAC;IACtD,CAAC;IAEO,yBAAyB;QAC7B,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;IACjC,CAAC;CACJ;AAlHD,4EAkHC","sourcesContent":["import { DiscoveryService } from \"@nestjs/core\";\nimport { isEmpty, isInt, isNotEmpty, isString } from \"class-validator\";\nimport { SelectionValueType } from \"src/dtos/create-field-metadata.dto\";\nimport { FieldCrudManager, ISelectionProvider, ISelectionProviderContext, ValidationError } from \"src/interfaces\";\n\nexport interface SelectionDynamicFieldOptions {\n selectionDynamicProvider: string;\n selectionValueType: SelectionValueType;\n required: boolean | undefined | null;\n selectionDynamicProviderCtxt: any;\n fieldName: string;\n discoveryService: DiscoveryService;\n isMultiSelect: boolean | undefined | null;\n}\n\nexport class SelectionDynamicFieldCrudManager implements FieldCrudManager {\n\n constructor(private readonly options: SelectionDynamicFieldOptions) {\n }\n\n async validate(dto: any): Promise<ValidationError[]> {\n const fieldValue: any = dto[this.options.fieldName];\n // return this.applyValidations(fieldValue);\n const isMultiSelect = this.options?.isMultiSelect;\n if (isMultiSelect && fieldValue) {\n const arrayCheck = this.parseAndValidateArray(fieldValue);\n\n if (!arrayCheck.isValid) {\n return [\n {\n field: this.options.fieldName,\n error: `Field: ${this.options.fieldName} must be a valid array`,\n },\n ];\n }\n\n const values = arrayCheck.values;\n\n if (this.isApplyRequiredValidation() && values.length === 0) {\n return [\n {\n field: this.options.fieldName,\n error: `Field: ${this.options.fieldName} is required`,\n },\n ];\n }\n\n // Apply validations to each value\n const allErrors = await Promise.all(values.map((val) => this.applyValidations(val)));\n return allErrors.flat();\n } else {\n // For non-multi-select, apply validations to the single field value\n return this.applyValidations(fieldValue);\n }\n }\n\n private parseAndValidateArray(fieldValue: any): { isValid: boolean; values: any[] } {\n if (Array.isArray(fieldValue)) {\n return { isValid: true, values: fieldValue };\n }\n \n try {\n const parsed = typeof fieldValue === 'string' ? JSON.parse(fieldValue) : null;\n if (Array.isArray(parsed)) {\n return { isValid: true, values: parsed };\n }\n } catch {\n // fall through\n }\n \n return { isValid: false, values: [] };\n }\n \n\n private async applyValidations(fieldValue: any): Promise<ValidationError[]> {\n const errors: ValidationError[] = [];\n this.isApplyRequiredValidation() && isEmpty(fieldValue) ? errors.push({ field: this.options.fieldName, error: `Field: ${this.options.fieldName} is required` }) : \"no errors\";\n if (isNotEmpty(fieldValue)) {\n const formatErrors = await this.applyFormatValidations(fieldValue);\n errors.push(...formatErrors);\n }\n return errors;\n }\n\n private async applyFormatValidations(fieldValue: any): Promise<ValidationError[]> {\n const errors: ValidationError[] = [];\n !this.isValidSelectionValueType(fieldValue, this.options.selectionValueType) ? errors.push({ field: this.options.fieldName, error: 'Field value type is invalid' }) : \"no errors\";\n const _isValidSelectionValue = await this.isValidSelectionValue(fieldValue, this.options.selectionDynamicProvider)\n !_isValidSelectionValue ? errors.push({ field: this.options.fieldName, error: 'Field value is invalid' }) : \"no errors\";\n return errors;\n }\n\n transformForCreate(dto: any): any {\n return dto;\n }\n\n // Validation to be applied\n private isValidSelectionValueType(fieldValue: any, selectionValueType: SelectionValueType): boolean {\n switch (selectionValueType) {\n case SelectionValueType.string:\n return isString(fieldValue);\n case SelectionValueType.int:\n return isInt(fieldValue);\n default:\n throw new Error(`Validation for selection value type ${selectionValueType} is not implemented`);\n }\n }\n\n private async isValidSelectionValue(fieldValue: any, selectionDynamicProvider: string): Promise<boolean> {\n const providerInstance = this.providerInstance<any>(selectionDynamicProvider);\n const values = await providerInstance.values('', JSON.parse(this.options.selectionDynamicProviderCtxt));\n // return values.map(v => v.split(\":\")[0]).includes(fieldValue);\n return values.map(v => v.value).includes(fieldValue);\n }\n\n private providerInstance<T extends ISelectionProviderContext>(selectionDynamicProvider: string): ISelectionProvider<T> {\n const provider = this.options.discoveryService\n .getProviders()\n .filter((provider) => provider.name === selectionDynamicProvider)\n .pop();\n if (!provider) {\n throw new Error(`Provider for ${selectionDynamicProvider} not found`);\n }\n return provider.instance as ISelectionProvider<T>;\n }\n\n private isApplyRequiredValidation(): boolean {\n return this.options.required;\n }\n}"]}
@@ -11,6 +11,7 @@ export declare class SelectionStaticFieldCrudManager implements FieldCrudManager
11
11
  private readonly options;
12
12
  constructor(options: SelectionStaticFieldOptions);
13
13
  validate(dto: any): Promise<ValidationError[]>;
14
+ private parseAndValidateArray;
14
15
  private applyValidations;
15
16
  private applyFormatValidations;
16
17
  transformForCreate(dto: any): any;
@@ -1 +1 @@
1
- {"version":3,"file":"SelectionStaticFieldCrudManager.d.ts","sourceRoot":"","sources":["../../../src/helpers/field-crud-managers/SelectionStaticFieldCrudManager.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,oCAAoC,CAAC;AACxE,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEnE,MAAM,WAAW,2BAA2B;IACxC,qBAAqB,EAAE,MAAM,EAAE,CAAC;IAChC,kBAAkB,EAAE,kBAAkB,CAAC;IACvC,QAAQ,EAAE,OAAO,GAAG,SAAS,GAAG,IAAI,CAAC;IACrC,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa,EAAE,OAAO,GAAG,SAAS,GAAG,IAAI,CAAC;CAC7C;AAED,qBAAa,+BAAgC,YAAW,gBAAgB;IAExD,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,EAAE,2BAA2B;IAG3D,QAAQ,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;IA0BpD,OAAO,CAAC,gBAAgB;IASxB,OAAO,CAAC,sBAAsB;IAO9B,kBAAkB,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG;IAKjC,OAAO,CAAC,yBAAyB;IAWjC,OAAO,CAAC,2BAA2B;IAWnC,OAAO,CAAC,yBAAyB;CAGpC"}
1
+ {"version":3,"file":"SelectionStaticFieldCrudManager.d.ts","sourceRoot":"","sources":["../../../src/helpers/field-crud-managers/SelectionStaticFieldCrudManager.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,oCAAoC,CAAC;AACxE,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEnE,MAAM,WAAW,2BAA2B;IACxC,qBAAqB,EAAE,MAAM,EAAE,CAAC;IAChC,kBAAkB,EAAE,kBAAkB,CAAC;IACvC,QAAQ,EAAE,OAAO,GAAG,SAAS,GAAG,IAAI,CAAC;IACrC,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa,EAAE,OAAO,GAAG,SAAS,GAAG,IAAI,CAAC;CAC7C;AAED,qBAAa,+BAAgC,YAAW,gBAAgB;IAExD,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,EAAE,2BAA2B;IAG3D,QAAQ,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;IAoCpD,OAAO,CAAC,qBAAqB;IAkB7B,OAAO,CAAC,gBAAgB;IASxB,OAAO,CAAC,sBAAsB;IAO9B,kBAAkB,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG;IAKjC,OAAO,CAAC,yBAAyB;IAWjC,OAAO,CAAC,2BAA2B;IAWnC,OAAO,CAAC,yBAAyB;CAGpC"}
@@ -10,21 +10,46 @@ class SelectionStaticFieldCrudManager {
10
10
  async validate(dto) {
11
11
  const fieldValue = dto[this.options.fieldName];
12
12
  const isMultiSelect = this.options?.isMultiSelect;
13
- if (isMultiSelect) {
14
- let values;
15
- try {
16
- values = JSON.parse(fieldValue);
13
+ if (isMultiSelect && fieldValue) {
14
+ const arrayCheck = this.parseAndValidateArray(fieldValue);
15
+ if (!arrayCheck.isValid) {
16
+ return [
17
+ {
18
+ field: this.options.fieldName,
19
+ error: `Field: ${this.options.fieldName} must be a valid array`,
20
+ },
21
+ ];
17
22
  }
18
- catch {
19
- values = [fieldValue];
23
+ const values = arrayCheck.values;
24
+ if (this.isApplyRequiredValidation() && values.length === 0) {
25
+ return [
26
+ {
27
+ field: this.options.fieldName,
28
+ error: `Field: ${this.options.fieldName} is required`,
29
+ },
30
+ ];
20
31
  }
21
- const allErrors = await Promise.all(values.map(value => this.applyValidations(value)));
32
+ const allErrors = await Promise.all(values.map((val) => this.applyValidations(val)));
22
33
  return allErrors.flat();
23
34
  }
24
35
  else {
25
36
  return this.applyValidations(fieldValue);
26
37
  }
27
38
  }
39
+ parseAndValidateArray(fieldValue) {
40
+ if (Array.isArray(fieldValue)) {
41
+ return { isValid: true, values: fieldValue };
42
+ }
43
+ try {
44
+ const parsed = typeof fieldValue === 'string' ? JSON.parse(fieldValue) : null;
45
+ if (Array.isArray(parsed)) {
46
+ return { isValid: true, values: parsed };
47
+ }
48
+ }
49
+ catch {
50
+ }
51
+ return { isValid: false, values: [] };
52
+ }
28
53
  applyValidations(fieldValue) {
29
54
  const errors = [];
30
55
  this.isApplyRequiredValidation() && (0, class_validator_1.isEmpty)(fieldValue) ? errors.push({ field: this.options.fieldName, error: `Field: ${this.options.fieldName} is required` }) : "no errors";
@@ -1 +1 @@
1
- {"version":3,"file":"SelectionStaticFieldCrudManager.js","sourceRoot":"","sources":["../../../src/helpers/field-crud-managers/SelectionStaticFieldCrudManager.ts"],"names":[],"mappings":";;;AAAA,qDAAuE;AACvE,oFAAwE;AAWxE,MAAa,+BAA+B;IAExC,YAA6B,OAAoC;QAApC,YAAO,GAAP,OAAO,CAA6B;IACjE,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,GAAQ;QAClB,MAAM,UAAU,GAAQ,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QAErD,MAAM,aAAa,GAAG,IAAI,CAAC,OAAO,EAAE,aAAa,CAAC;QAClD,IAAI,aAAa,EAAE,CAAC;YAChB,IAAI,MAAa,CAAC;YAElB,IAAI,CAAC;gBAED,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;YACpC,CAAC;YAAC,MAAM,CAAC;gBAEL,MAAM,GAAG,CAAC,UAAU,CAAC,CAAC;YAC1B,CAAC;YAGD,MAAM,SAAS,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAGvF,OAAO,SAAS,CAAC,IAAI,EAAE,CAAC;QAC5B,CAAC;aAAM,CAAC;YAEJ,OAAO,IAAI,CAAC,gBAAgB,CAAC,UAAU,CAAC,CAAC;QAC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AC;IAGO,yBAAyB,CAAC,UAAe,EAAE,kBAAsC;QACrF,QAAQ,kBAAkB,EAAE,CAAC;YACzB,KAAK,8CAAkB,CAAC,MAAM;gBAC1B,OAAO,IAAA,0BAAQ,EAAC,UAAU,CAAC,CAAC;YAChC,KAAK,8CAAkB,CAAC,GAAG;gBACvB,OAAO,IAAA,uBAAK,EAAC,UAAU,CAAC,CAAC;YAC7B;gBACI,MAAM,IAAI,KAAK,CAAC,uCAAuC,kBAAkB,qBAAqB,CAAC,CAAC;QACxG,CAAC;IACL,CAAC;IAEO,2BAA2B,CAAC,UAAe,EAAE,kBAAuB,EAAE,qBAA+B;QACzG,QAAQ,kBAAkB,EAAE,CAAC;YACzB,KAAK,8CAAkB,CAAC,MAAM;gBAC1B,OAAO,qBAAqB,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;YAChF,KAAK,8CAAkB,CAAC,GAAG;gBACvB,OAAO,qBAAqB,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;YACxF;gBACI,MAAM,IAAI,KAAK,CAAC,uCAAuC,kBAAkB,qBAAqB,CAAC,CAAC;QACxG,CAAC;IACL,CAAC;IAEO,yBAAyB;QAC7B,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;IACjC,CAAC;CACJ;AA7ED,0EA6EC","sourcesContent":["import { isEmpty, isInt, isNotEmpty, isString } from \"class-validator\";\nimport { SelectionValueType } from \"src/dtos/create-field-metadata.dto\";\nimport { FieldCrudManager, ValidationError } from \"src/interfaces\";\n\nexport interface SelectionStaticFieldOptions {\n selectionStaticValues: string[];\n selectionValueType: SelectionValueType;\n required: boolean | undefined | null;\n fieldName: string;\n isMultiSelect: boolean | undefined | null;\n}\n\nexport class SelectionStaticFieldCrudManager implements FieldCrudManager {\n\n constructor(private readonly options: SelectionStaticFieldOptions) {\n }\n\n async validate(dto: any): Promise<ValidationError[]> {\n const fieldValue: any = dto[this.options.fieldName];\n // return this.applyValidations(fieldValue);\n const isMultiSelect = this.options?.isMultiSelect;\n if (isMultiSelect) {\n let values: any[];\n \n try {\n // Try to parse the field value, which should be a JSON stringified array\n values = JSON.parse(fieldValue);\n } catch {\n // If parsing fails, fallback to a single value\n values = [fieldValue];\n }\n \n // Apply validations to each value asynchronously\n const allErrors = await Promise.all(values.map(value => this.applyValidations(value)));\n \n // Flatten the array of errors and return\n return allErrors.flat();\n } else {\n // For non-multi-select, apply validations to the single field value\n return this.applyValidations(fieldValue);\n }\n }\n\n private applyValidations(fieldValue: any): ValidationError[] {\n const errors: ValidationError[] = [];\n this.isApplyRequiredValidation() && isEmpty(fieldValue) ? errors.push({ field: this.options.fieldName, error: `Field: ${this.options.fieldName} is required` }) : \"no errors\";\n if (isNotEmpty(fieldValue)) {\n errors.push(...this.applyFormatValidations(fieldValue));\n }\n return errors;\n }\n\n private applyFormatValidations(fieldValue: any): ValidationError[] {\n const errors: ValidationError[] = [];\n !this.isValidSelectionValueType(fieldValue, this.options.selectionValueType) ? errors.push({ field: this.options.fieldName, error: 'Field value is invalid' }) : \"no errors\";\n !this.isValidSelectionStaticValue(fieldValue, this.options.selectionValueType, this.options.selectionStaticValues) ? errors.push({ field: this.options.fieldName, error: 'Field value is invalid' }) : \"no errors\";\n return errors;\n }\n\n transformForCreate(dto: any): any {\n return dto;\n }\n\n // Validation to be applied\n private isValidSelectionValueType(fieldValue: any, selectionValueType: SelectionValueType): boolean {\n switch (selectionValueType) {\n case SelectionValueType.string:\n return isString(fieldValue);\n case SelectionValueType.int:\n return isInt(fieldValue);\n default:\n throw new Error(`Validation for selection value type ${selectionValueType} is not implemented`);\n }\n }\n\n private isValidSelectionStaticValue(fieldValue: any, selectionValueType: any, selectionStaticValues: string[]): boolean {\n switch (selectionValueType) {\n case SelectionValueType.string:\n return selectionStaticValues.map(v => v.split(\":\")[0]).includes(fieldValue);\n case SelectionValueType.int:\n return selectionStaticValues.map(v => Number(v.split(\":\")[0])).includes(fieldValue);\n default:\n throw new Error(`Validation for selection value type ${selectionValueType} is not implemented`);\n }\n }\n\n private isApplyRequiredValidation(): boolean {\n return this.options.required;\n }\n}\n"]}
1
+ {"version":3,"file":"SelectionStaticFieldCrudManager.js","sourceRoot":"","sources":["../../../src/helpers/field-crud-managers/SelectionStaticFieldCrudManager.ts"],"names":[],"mappings":";;;AAAA,qDAAuE;AACvE,oFAAwE;AAWxE,MAAa,+BAA+B;IAExC,YAA6B,OAAoC;QAApC,YAAO,GAAP,OAAO,CAA6B;IACjE,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,GAAQ;QACnB,MAAM,UAAU,GAAQ,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QAEpD,MAAM,aAAa,GAAG,IAAI,CAAC,OAAO,EAAE,aAAa,CAAC;QAClD,IAAI,aAAa,IAAI,UAAU,EAAE,CAAC;YAC9B,MAAM,UAAU,GAAG,IAAI,CAAC,qBAAqB,CAAC,UAAU,CAAC,CAAC;YAE1D,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;gBACtB,OAAO;oBACH;wBACI,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,SAAS;wBAC7B,KAAK,EAAE,UAAU,IAAI,CAAC,OAAO,CAAC,SAAS,wBAAwB;qBAClE;iBACJ,CAAC;YACN,CAAC;YAED,MAAM,MAAM,GAAG,UAAU,CAAC,MAAM,CAAC;YAEjC,IAAI,IAAI,CAAC,yBAAyB,EAAE,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBAC1D,OAAO;oBACH;wBACI,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,SAAS;wBAC7B,KA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AM,IAAI,KAAK,CAAC,uCAAuC,kBAAkB,qBAAqB,CAAC,CAAC;QACxG,CAAC;IACL,CAAC;IAEO,2BAA2B,CAAC,UAAe,EAAE,kBAAuB,EAAE,qBAA+B;QACzG,QAAQ,kBAAkB,EAAE,CAAC;YACzB,KAAK,8CAAkB,CAAC,MAAM;gBAC1B,OAAO,qBAAqB,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;YAChF,KAAK,8CAAkB,CAAC,GAAG;gBACvB,OAAO,qBAAqB,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;YACxF;gBACI,MAAM,IAAI,KAAK,CAAC,uCAAuC,kBAAkB,qBAAqB,CAAC,CAAC;QACxG,CAAC;IACL,CAAC;IAEO,yBAAyB;QAC7B,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;IACjC,CAAC;CACJ;AAzGD,0EAyGC","sourcesContent":["import { isEmpty, isInt, isNotEmpty, isString } from \"class-validator\";\nimport { SelectionValueType } from \"src/dtos/create-field-metadata.dto\";\nimport { FieldCrudManager, ValidationError } from \"src/interfaces\";\n\nexport interface SelectionStaticFieldOptions {\n selectionStaticValues: string[];\n selectionValueType: SelectionValueType;\n required: boolean | undefined | null;\n fieldName: string;\n isMultiSelect: boolean | undefined | null;\n}\n\nexport class SelectionStaticFieldCrudManager implements FieldCrudManager {\n\n constructor(private readonly options: SelectionStaticFieldOptions) {\n }\n\n async validate(dto: any): Promise<ValidationError[]> {\n const fieldValue: any = dto[this.options.fieldName];\n // return this.applyValidations(fieldValue);\n const isMultiSelect = this.options?.isMultiSelect;\n if (isMultiSelect && fieldValue) {\n const arrayCheck = this.parseAndValidateArray(fieldValue);\n\n if (!arrayCheck.isValid) {\n return [\n {\n field: this.options.fieldName,\n error: `Field: ${this.options.fieldName} must be a valid array`,\n },\n ];\n }\n\n const values = arrayCheck.values;\n\n if (this.isApplyRequiredValidation() && values.length === 0) {\n return [\n {\n field: this.options.fieldName,\n error: `Field: ${this.options.fieldName} is required`,\n },\n ];\n }\n\n // Apply validations to each value\n const allErrors = await Promise.all(values.map((val) => this.applyValidations(val)));\n return allErrors.flat();\n } else {\n // For non-multi-select, apply validations to the single field value\n return this.applyValidations(fieldValue);\n }\n }\n\n private parseAndValidateArray(fieldValue: any): { isValid: boolean; values: any[] } {\n if (Array.isArray(fieldValue)) {\n return { isValid: true, values: fieldValue };\n }\n \n try {\n const parsed = typeof fieldValue === 'string' ? JSON.parse(fieldValue) : null;\n if (Array.isArray(parsed)) {\n return { isValid: true, values: parsed };\n }\n } catch {\n // fall through\n }\n \n return { isValid: false, values: [] };\n }\n \n\n private applyValidations(fieldValue: any): ValidationError[] {\n const errors: ValidationError[] = [];\n this.isApplyRequiredValidation() && isEmpty(fieldValue) ? errors.push({ field: this.options.fieldName, error: `Field: ${this.options.fieldName} is required` }) : \"no errors\";\n if (isNotEmpty(fieldValue)) {\n errors.push(...this.applyFormatValidations(fieldValue));\n }\n return errors;\n }\n\n private applyFormatValidations(fieldValue: any): ValidationError[] {\n const errors: ValidationError[] = [];\n !this.isValidSelectionValueType(fieldValue, this.options.selectionValueType) ? errors.push({ field: this.options.fieldName, error: 'Field value is invalid' }) : \"no errors\";\n !this.isValidSelectionStaticValue(fieldValue, this.options.selectionValueType, this.options.selectionStaticValues) ? errors.push({ field: this.options.fieldName, error: 'Field value is invalid' }) : \"no errors\";\n return errors;\n }\n\n transformForCreate(dto: any): any {\n return dto;\n }\n\n // Validation to be applied\n private isValidSelectionValueType(fieldValue: any, selectionValueType: SelectionValueType): boolean {\n switch (selectionValueType) {\n case SelectionValueType.string:\n return isString(fieldValue);\n case SelectionValueType.int:\n return isInt(fieldValue);\n default:\n throw new Error(`Validation for selection value type ${selectionValueType} is not implemented`);\n }\n }\n\n private isValidSelectionStaticValue(fieldValue: any, selectionValueType: any, selectionStaticValues: string[]): boolean {\n switch (selectionValueType) {\n case SelectionValueType.string:\n return selectionStaticValues.map(v => v.split(\":\")[0]).includes(fieldValue);\n case SelectionValueType.int:\n return selectionStaticValues.map(v => Number(v.split(\":\")[0])).includes(fieldValue);\n default:\n throw new Error(`Validation for selection value type ${selectionValueType} is not implemented`);\n }\n }\n\n private isApplyRequiredValidation(): boolean {\n return this.options.required;\n }\n}\n"]}
@@ -133,7 +133,7 @@ let AuthenticationService = AuthenticationService_1 = class AuthenticationServic
133
133
  catch (err) {
134
134
  const pgUniqueViolationErrorCode = '23505';
135
135
  if (err.code === pgUniqueViolationErrorCode) {
136
- throw new common_1.ConflictException();
136
+ throw new common_1.ConflictException(parseUniqueConstraintError(err.detail || 'A unique constraint violation occurred.'));
137
137
  }
138
138
  throw err;
139
139
  }
@@ -869,4 +869,19 @@ exports.AuthenticationService = AuthenticationService = AuthenticationService_1
869
869
  role_metadata_service_1.RoleMetadataService, void 0, user_activity_history_service_1.UserActivityHistoryService,
870
870
  request_context_service_1.RequestContextService])
871
871
  ], AuthenticationService);
872
+ function parseUniqueConstraintError(detail) {
873
+ const match = detail.match(/Key \(([^)]+)\)=\(([^)]+)\) already exists\./);
874
+ if (match) {
875
+ const field = match[1];
876
+ const value = match[2];
877
+ const fieldMap = {
878
+ username: 'username',
879
+ email: 'email address',
880
+ full_name_user_key: 'full name',
881
+ };
882
+ const friendlyField = fieldMap[field] || field;
883
+ return `A user with ${friendlyField} "${value}" already exists.`;
884
+ }
885
+ return detail;
886
+ }
872
887
  //# sourceMappingURL=authentication.service.js.map