@mondaydotcomorg/monday-authorization 3.5.3-feat-shaime-support-entity-attributes-in-authorization-sdk-a77c130 → 3.7.0-feat-shaime-support-entity-attributes-4-49e1de0

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.
Files changed (98) hide show
  1. package/dist/authorization-attributes-ms-service.d.ts +6 -8
  2. package/dist/authorization-attributes-ms-service.d.ts.map +1 -1
  3. package/dist/authorization-attributes-ms-service.js +13 -75
  4. package/dist/authorization-attributes-service.d.ts +1 -2
  5. package/dist/authorization-attributes-service.d.ts.map +1 -1
  6. package/dist/authorization-attributes-service.js +0 -1
  7. package/dist/authorization-attributes-sns-service.d.ts +4 -3
  8. package/dist/authorization-attributes-sns-service.d.ts.map +1 -1
  9. package/dist/authorization-middleware.d.ts +2 -2
  10. package/dist/authorization-middleware.d.ts.map +1 -1
  11. package/dist/authorization-service.d.ts +3 -3
  12. package/dist/authorization-service.d.ts.map +1 -1
  13. package/dist/base-attribute-assignment.d.ts +2 -0
  14. package/dist/base-attribute-assignment.d.ts.map +1 -1
  15. package/dist/base-attribute-assignment.js +11 -18
  16. package/dist/entity-attribute-assignment.js +1 -1
  17. package/dist/entity-attributes-constants.d.ts +6 -6
  18. package/dist/entity-attributes-constants.d.ts.map +1 -1
  19. package/dist/entity-attributes-constants.js +7 -5
  20. package/dist/errors/argument-error.d.ts.map +1 -1
  21. package/dist/errors/argument-error.js +0 -1
  22. package/dist/esm/authorization-attributes-ms-service.d.ts +6 -8
  23. package/dist/esm/authorization-attributes-ms-service.d.ts.map +1 -1
  24. package/dist/esm/authorization-attributes-ms-service.mjs +13 -75
  25. package/dist/esm/authorization-attributes-service.d.ts +1 -2
  26. package/dist/esm/authorization-attributes-service.d.ts.map +1 -1
  27. package/dist/esm/authorization-attributes-service.mjs +0 -1
  28. package/dist/esm/authorization-attributes-sns-service.d.ts +4 -3
  29. package/dist/esm/authorization-attributes-sns-service.d.ts.map +1 -1
  30. package/dist/esm/authorization-middleware.d.ts +2 -2
  31. package/dist/esm/authorization-middleware.d.ts.map +1 -1
  32. package/dist/esm/authorization-service.d.ts +3 -3
  33. package/dist/esm/authorization-service.d.ts.map +1 -1
  34. package/dist/esm/base-attribute-assignment.d.ts +2 -0
  35. package/dist/esm/base-attribute-assignment.d.ts.map +1 -1
  36. package/dist/esm/base-attribute-assignment.mjs +7 -18
  37. package/dist/esm/entity-attribute-assignment.mjs +1 -1
  38. package/dist/esm/entity-attributes-constants.d.ts +6 -6
  39. package/dist/esm/entity-attributes-constants.d.ts.map +1 -1
  40. package/dist/esm/entity-attributes-constants.mjs +8 -6
  41. package/dist/esm/errors/argument-error.d.ts.map +1 -1
  42. package/dist/esm/errors/argument-error.mjs +0 -1
  43. package/dist/esm/index.d.ts +0 -9
  44. package/dist/esm/index.d.ts.map +1 -1
  45. package/dist/esm/index.mjs +0 -7
  46. package/dist/esm/prometheus-service.d.ts +2 -1
  47. package/dist/esm/prometheus-service.d.ts.map +1 -1
  48. package/dist/esm/resource-attribute-assignment.d.ts +1 -1
  49. package/dist/esm/resource-attribute-assignment.d.ts.map +1 -1
  50. package/dist/esm/resource-attributes-constants.d.ts +12 -2
  51. package/dist/esm/resource-attributes-constants.d.ts.map +1 -1
  52. package/dist/esm/resource-attributes-constants.mjs +15 -12
  53. package/dist/esm/types/authorization-attributes-contracts.d.ts +1 -1
  54. package/dist/esm/types/authorization-attributes-contracts.d.ts.map +1 -1
  55. package/dist/esm/types/authorization-attributes-service.interface.d.ts +4 -3
  56. package/dist/esm/types/authorization-attributes-service.interface.d.ts.map +1 -1
  57. package/dist/esm/types/general.d.ts +2 -1
  58. package/dist/esm/types/general.d.ts.map +1 -1
  59. package/dist/esm/utils/validation.d.ts +30 -30
  60. package/dist/esm/utils/validation.d.ts.map +1 -1
  61. package/dist/esm/utils/validation.mjs +121 -86
  62. package/dist/index.d.ts +0 -9
  63. package/dist/index.d.ts.map +1 -1
  64. package/dist/index.js +0 -15
  65. package/dist/prometheus-service.d.ts +2 -1
  66. package/dist/prometheus-service.d.ts.map +1 -1
  67. package/dist/resource-attribute-assignment.d.ts +1 -1
  68. package/dist/resource-attribute-assignment.d.ts.map +1 -1
  69. package/dist/resource-attributes-constants.d.ts +12 -2
  70. package/dist/resource-attributes-constants.d.ts.map +1 -1
  71. package/dist/resource-attributes-constants.js +14 -11
  72. package/dist/types/authorization-attributes-contracts.d.ts +1 -1
  73. package/dist/types/authorization-attributes-contracts.d.ts.map +1 -1
  74. package/dist/types/authorization-attributes-service.interface.d.ts +4 -3
  75. package/dist/types/authorization-attributes-service.interface.d.ts.map +1 -1
  76. package/dist/types/general.d.ts +2 -1
  77. package/dist/types/general.d.ts.map +1 -1
  78. package/dist/utils/validation.d.ts +30 -30
  79. package/dist/utils/validation.d.ts.map +1 -1
  80. package/dist/utils/validation.js +125 -86
  81. package/package.json +4 -3
  82. package/src/authorization-attributes-ms-service.ts +20 -97
  83. package/src/authorization-attributes-service.ts +1 -2
  84. package/src/authorization-attributes-sns-service.ts +3 -3
  85. package/src/authorization-middleware.ts +2 -2
  86. package/src/authorization-service.ts +4 -4
  87. package/src/base-attribute-assignment.ts +14 -23
  88. package/src/entity-attribute-assignment.ts +1 -1
  89. package/src/entity-attributes-constants.ts +6 -6
  90. package/src/errors/argument-error.ts +0 -1
  91. package/src/index.ts +0 -9
  92. package/src/prometheus-service.ts +3 -1
  93. package/src/resource-attribute-assignment.ts +1 -1
  94. package/src/resource-attributes-constants.ts +13 -21
  95. package/src/types/authorization-attributes-contracts.ts +0 -2
  96. package/src/types/authorization-attributes-service.interface.ts +3 -3
  97. package/src/types/general.ts +2 -3
  98. package/src/utils/validation.ts +139 -82
@@ -1,4 +1,6 @@
1
1
  import { ValidationUtils } from './utils/validation';
2
+ import isEqual from 'lodash/isEqual.js';
3
+ import { EntityAttributeAssignment, ResourceAttributeDelete } from './types/authorization-attributes-contracts';
2
4
 
3
5
  /**
4
6
  * Base class for attribute assignments (Resource or Entity)
@@ -19,21 +21,16 @@ export abstract class BaseAttributeAssignment<TId extends number, TType extends
19
21
  idFieldName: string,
20
22
  typeFieldName: string
21
23
  ) {
22
- // Validate id
23
- ValidationUtils.validateInteger(id, idFieldName);
24
-
25
- // Validate type
26
- this.type = ValidationUtils.validateEnum(type, validTypes as readonly TType[], typeFieldName) as TType;
27
-
28
- // Validate attributeKey
29
- ValidationUtils.validateString(attributeKey, 'attributeKey');
30
-
31
- // Validate attributeValue
32
- ValidationUtils.validateString(attributeValue, 'attributeValue');
24
+ const validated = ValidationUtils.validateAssignment<TType>(
25
+ { id, type, attributeKey, attributeValue },
26
+ validTypes as readonly TType[],
27
+ { id: idFieldName, type: typeFieldName }
28
+ );
33
29
 
34
- this.id = id;
35
- this.attributeKey = attributeKey;
36
- this.attributeValue = attributeValue;
30
+ this.id = validated.id as TId;
31
+ this.type = validated.type as TType;
32
+ this.attributeKey = validated.attributeKey;
33
+ this.attributeValue = validated.attributeValue;
37
34
  }
38
35
 
39
36
  /**
@@ -42,14 +39,8 @@ export abstract class BaseAttributeAssignment<TId extends number, TType extends
42
39
  * @returns true if all properties are equal
43
40
  */
44
41
  equals(other: BaseAttributeAssignment<TId, TType>): boolean {
45
- if (!(other instanceof this.constructor)) {
46
- return false;
47
- }
48
- return (
49
- this.id === other.id &&
50
- this.type === other.type &&
51
- this.attributeKey === other.attributeKey &&
52
- this.attributeValue === other.attributeValue
53
- );
42
+ return isEqual(this, other);
54
43
  }
44
+
45
+ abstract toDataTransferObject(): EntityAttributeAssignment | ResourceAttributeDelete;
55
46
  }
@@ -6,7 +6,7 @@ export class EntityAttributeAssignment extends BaseAttributeAssignment<number, E
6
6
  public readonly entityType: EntityType;
7
7
 
8
8
  constructor(entityId: number, entityType: string, attributeKey: string, attributeValue: string) {
9
- super(entityId, entityType, attributeKey, attributeValue, Object.values(ENTITY_TYPES), 'entityId', 'entityType');
9
+ super(entityId, entityType, attributeKey, attributeValue, ENTITY_TYPES, 'entityId', 'entityType');
10
10
  this.entityId = entityId;
11
11
  this.entityType = this.type;
12
12
  }
@@ -1,7 +1,7 @@
1
- export const ENTITY_TYPES = {
2
- USER: 'user',
3
- TEAM: 'team',
4
- ACCOUNT: 'account',
5
- } as const;
1
+ export enum EntityType {
2
+ User = 'user',
3
+ Team = 'team',
4
+ Account = 'account',
5
+ }
6
6
 
7
- export type EntityType = (typeof ENTITY_TYPES)[keyof typeof ENTITY_TYPES];
7
+ export const ENTITY_TYPES = Object.freeze(Object.values(EntityType));
@@ -2,6 +2,5 @@ export class ArgumentError extends Error {
2
2
  constructor(message: string) {
3
3
  super(message);
4
4
  this.name = 'ArgumentError';
5
- Object.setPrototypeOf(this, ArgumentError.prototype);
6
5
  }
7
6
  }
package/src/index.ts CHANGED
@@ -58,15 +58,6 @@ export {
58
58
  } from './authorization-middleware';
59
59
  export { AuthorizationService, AuthorizeResponse } from './authorization-service';
60
60
  export { AuthorizationAttributesService } from './authorization-attributes-service';
61
- export { AuthorizationAttributesSnsService } from './authorization-attributes-sns-service';
62
- export { AuthorizationAttributesMsService } from './authorization-attributes-ms-service';
63
- export { IAuthorizationAttributesService } from './types/authorization-attributes-service.interface';
64
- export { ResourceAttributeAssignment } from './resource-attribute-assignment';
65
- export { RESOURCE_TYPES, RESOURCE_ATTRIBUTES_CONSTANTS } from './resource-attributes-constants';
66
- export { EntityAttributeAssignment } from './entity-attribute-assignment';
67
- export { ENTITY_TYPES } from './entity-attributes-constants';
68
- export { ArgumentError } from './errors/argument-error';
69
- export type { EntityType } from './entity-attributes-constants';
70
61
  export { RolesService } from './roles-service';
71
62
  export { MembershipsService } from './memberships';
72
63
  export { AuthorizationObject, Resource, BaseRequest, ResourceGetter, ContextGetter } from './types/general';
@@ -1,3 +1,5 @@
1
+ import { Action } from './types/general';
2
+
1
3
  let prometheus: any = null;
2
4
  let authorizationCheckResponseTimeMetric: any = null;
3
5
 
@@ -34,7 +36,7 @@ export function getMetricsManager() {
34
36
 
35
37
  export function sendAuthorizationCheckResponseTimeMetric(
36
38
  resourceType: string,
37
- action: string,
39
+ action: Action,
38
40
  isAuthorized: boolean,
39
41
  responseStatus: number,
40
42
  time: number
@@ -5,7 +5,7 @@ export class ResourceAttributeAssignment extends BaseAttributeAssignment<number,
5
5
  public readonly resourceId: number;
6
6
  public readonly resourceType: ResourceType;
7
7
 
8
- constructor(resourceId: number, resourceType: string, attributeKey: string, attributeValue: string) {
8
+ constructor(resourceId: number, resourceType: ResourceType, attributeKey: string, attributeValue: string) {
9
9
  super(
10
10
  resourceId,
11
11
  resourceType,
@@ -11,25 +11,17 @@ export const RESOURCE_ATTRIBUTES_CONSTANTS = {
11
11
  },
12
12
  } as const;
13
13
 
14
- export type ResourceType =
15
- | 'account'
16
- | 'account_product'
17
- | 'workspace'
18
- | 'board'
19
- | 'item'
20
- | 'team'
21
- | 'overview'
22
- | 'document'
23
- | 'crm';
14
+ export enum ResourceType {
15
+ Account = 'account',
16
+ AccountProduct = 'account_product',
17
+ Workspace = 'workspace',
18
+ Board = 'board',
19
+ Item = 'item',
20
+ Team = 'team',
21
+ Overview = 'overview',
22
+ Document = 'document',
23
+ Crm = 'crm',
24
+ }
24
25
 
25
- export const RESOURCE_TYPES = {
26
- ACCOUNT: 'account',
27
- ACCOUNT_PRODUCT: 'account_product',
28
- WORKSPACE: 'workspace',
29
- BOARD: 'board',
30
- ITEM: 'item',
31
- TEAM: 'team',
32
- OVERVIEW: 'overview',
33
- DOCUMENT: 'document',
34
- CRM: 'crm',
35
- } as Record<string, ResourceType>;
26
+ // Define the array of strings and use 'as const' to make its contents literal types
27
+ export const RESOURCE_TYPES = Object.freeze(Object.values(ResourceType));
@@ -2,8 +2,6 @@ import { Resource } from './general';
2
2
  import type { EntityType } from '../entity-attributes-constants';
3
3
  import type { ResourceType } from '../resource-attributes-constants';
4
4
 
5
- export type { EntityType, ResourceType };
6
-
7
5
  interface AttributeAssignment {
8
6
  key: string;
9
7
  value: string;
@@ -1,13 +1,13 @@
1
1
  import {
2
2
  ResourceAttributeAssignment as ResourceAttributeAssignmentContract,
3
3
  EntityAttributeAssignment as EntityAttributeAssignmentContract,
4
- EntityType,
5
4
  ResourceAttributeOperation,
6
5
  EntityAttributeOperation,
7
6
  } from './authorization-attributes-contracts';
8
7
  import { ResourceAttributeAssignment } from '../resource-attribute-assignment';
9
8
  import { EntityAttributeAssignment } from '../entity-attribute-assignment';
10
9
  import { Resource } from './general';
10
+ import { EntityType } from '../entity-attributes-constants';
11
11
 
12
12
  /**
13
13
  * Resource type compatible with both MS and SNS services
@@ -23,8 +23,8 @@ export interface CompatibleResource {
23
23
  * Interface for authorization attributes operations.
24
24
  * Both MS (direct) and SNS (async) services implement this interface.
25
25
  */
26
- export interface IAuthorizationAttributesService {
27
- /**
26
+ export interface AuthorizationAttributesService {
27
+ /**authorization-attributes-service.ts
28
28
  * Upserts resource attributes.
29
29
  * For MS service: returns Promise<void>
30
30
  * For SNS service: requires appName and callerActionIdentifier, returns Promise<ResourceAttributesOperation[]>
@@ -5,17 +5,16 @@ export interface Resource {
5
5
  type: string;
6
6
  wrapperData?: object;
7
7
  }
8
-
8
+ export type Action = string;
9
9
  export interface Context {
10
10
  accountId: number;
11
11
  userId: number;
12
12
  }
13
-
14
13
  export interface AuthorizationObject {
15
14
  resource_id?: Resource['id'];
16
15
  resource_type: Resource['type'];
17
16
  wrapper_data?: Resource['wrapperData'];
18
- action: string;
17
+ action: Action;
19
18
  }
20
19
  export interface AuthorizationParams {
21
20
  authorizationObjects: AuthorizationObject[];
@@ -1,114 +1,171 @@
1
- import { z } from 'zod';
1
+ import Ajv from 'ajv';
2
2
  import { ArgumentError } from '../errors/argument-error';
3
+ import { Resource } from '../types/general';
3
4
 
4
5
  /**
5
- * Utility class for common validation operations using Zod
6
+ * Utility class for common validation operations using AJV
6
7
  */
7
8
  export class ValidationUtils {
9
+ private static ajv = new Ajv({ allErrors: true });
8
10
  /**
9
- * Validates that a value is an integer
10
- * @param value The value to validate
11
- * @param fieldName The name of the field for error messages
12
- * @throws ArgumentError if value is not an integer
11
+ * Validates that a value is an integer. Throws ArgumentError with a descriptive message on failure.
13
12
  */
14
- static validateInteger(value: any, fieldName: string): void {
15
- const schema = z.number().int();
16
- try {
17
- schema.parse(value);
18
- } catch (error) {
19
- if (error instanceof z.ZodError) {
20
- throw new ArgumentError(`${fieldName} must be an integer, got: ${value}`);
21
- }
22
- throw error;
13
+ static validateInteger(value: number): void {
14
+ const numberSchema = { type: 'number', multipleOf: 1 };
15
+ const validate = ValidationUtils.ajv.compile(numberSchema);
16
+ const isValid = validate(value);
17
+ if (!isValid) {
18
+ throw new ArgumentError(`Value must be an integer, got: ${value}`);
23
19
  }
24
20
  }
25
21
 
26
22
  /**
27
- * Validates that a value is a non-empty string
28
- * @param value The value to validate
29
- * @param fieldName The name of the field for error messages
30
- * @throws ArgumentError if value is not a string or is empty
23
+ * Validates that a value is a non-empty string. Throws ArgumentError on failure.
31
24
  */
32
- static validateString(value: any, fieldName: string): void {
33
- const schema = z.string().min(1);
34
- try {
35
- schema.parse(value);
36
- } catch (error) {
37
- if (error instanceof z.ZodError) {
38
- if (typeof value !== 'string') {
39
- throw new ArgumentError(`${fieldName} must be a string, got: ${typeof value}`);
40
- }
41
- throw new ArgumentError(`${fieldName} must be a non-empty string`);
42
- }
43
- throw error;
25
+ static validateString(value: string): void {
26
+ const non_empty_string_schema = {
27
+ type: 'string',
28
+ minLength: 1,
29
+ pattern: '\\S',
30
+ };
31
+ const validate = ValidationUtils.ajv.compile(non_empty_string_schema);
32
+ const isValid = validate(value);
33
+ if (!isValid) {
34
+ throw new ArgumentError(`Value must be a non-empty string, got: ${value}`);
44
35
  }
45
36
  }
46
37
 
47
38
  /**
48
- * Validates that a value is an array and optionally checks minimum length
49
- * @param value The value to validate
50
- * @param fieldName The name of the field for error messages
51
- * @param minLength Minimum required length (default: 0)
52
- * @returns The validated array
53
- * @throws ArgumentError if value is not an array or doesn't meet minimum length
39
+ * Validates that a value is an array of non-empty strings. Throws ArgumentError on failure.
40
+ * Allows empty arrays - caller should handle early return.
54
41
  */
55
- static validateArray<T>(value: any, fieldName: string, minLength = 0): T[] {
56
- const schema = z.array(z.any()).min(minLength);
57
- try {
58
- return schema.parse(value) as T[];
59
- } catch (error) {
60
- if (error instanceof z.ZodError) {
61
- if (!Array.isArray(value)) {
62
- throw new ArgumentError(`${fieldName} must be an array`);
63
- }
64
- throw new ArgumentError(`${fieldName} must have at least ${minLength} items`);
65
- }
66
- throw error;
42
+ static validateStringArray(value: string[]): void {
43
+ const string_array_schema = {
44
+ type: 'array',
45
+ items: {
46
+ type: 'string',
47
+ minLength: 1,
48
+ pattern: '\\S',
49
+ },
50
+ };
51
+ const validateStringArray = ValidationUtils.ajv.compile(string_array_schema);
52
+ const isValid = validateStringArray(value);
53
+ if (!isValid) {
54
+ throw new ArgumentError(`Value must be an array of non-empty strings, got: ${value}`);
67
55
  }
56
+ // Allow empty arrays to pass validation - caller should handle early return
57
+ // Non-empty arrays will be validated for string content above
68
58
  }
69
59
 
70
60
  /**
71
- * Validates that a value is one of the allowed enum values
72
- * @param value The value to validate
73
- * @param validValues Array of valid values
74
- * @param fieldName The name of the field for error messages
75
- * @returns The validated value as the enum type
76
- * @throws ArgumentError if value is not in validValues
61
+ * Validates an attribute assignment object using a single AJV schema.
62
+ * Preserves legacy error messages for each field.
77
63
  */
78
- static validateEnum<T extends string>(value: string, validValues: readonly T[], fieldName: string): T {
79
- const schema = z.enum(validValues as [T, ...T[]]);
80
- try {
81
- return schema.parse(value) as T;
82
- } catch (error) {
83
- if (error instanceof z.ZodError) {
84
- throw new ArgumentError(`${fieldName} must be one of [${validValues.join(', ')}], got: ${value}`);
64
+ static validateAssignment<TType extends string>(
65
+ data: { id: any; type: string; attributeKey: any; attributeValue: any },
66
+ validTypes: readonly TType[],
67
+ fieldNames: { id: string; type: string }
68
+ ): { id: number; type: TType; attributeKey: string; attributeValue: string } {
69
+ const schema = {
70
+ type: 'object',
71
+ properties: {
72
+ id: { type: 'number', multipleOf: 1 },
73
+ type: { type: 'string', enum: validTypes as TType[] },
74
+ attributeKey: { type: 'string', minLength: 1 },
75
+ attributeValue: { type: 'string', minLength: 1 },
76
+ },
77
+ required: ['id', 'type', 'attributeKey', 'attributeValue'],
78
+ additionalProperties: false,
79
+ } as const;
80
+
81
+ const validate = this.ajv.compile(schema);
82
+ const valid = validate(data);
83
+ if (!valid) {
84
+ // Map to legacy error messages deterministically
85
+ const { id, type, attributeKey, attributeValue } = data;
86
+
87
+ // id must be integer
88
+ const isInteger = typeof id === 'number' && Number.isFinite(id) && Math.floor(id) === id;
89
+ if (!isInteger) {
90
+ throw new ArgumentError(`${fieldNames.id} must be an integer, got: ${id}`);
91
+ }
92
+
93
+ // type must be within enum
94
+ if (typeof type !== 'string' || !validTypes.includes(type as TType)) {
95
+ throw new ArgumentError(`${fieldNames.type} must be one of [${validTypes.join(', ')}], got: ${type}`);
96
+ }
97
+
98
+ // attributeKey
99
+ if (typeof attributeKey !== 'string') {
100
+ throw new ArgumentError(`attributeKey must be a string, got: ${typeof attributeKey}`);
101
+ }
102
+ if (!attributeKey) {
103
+ throw new ArgumentError('attributeKey must be a non-empty string');
85
104
  }
86
- throw error;
105
+
106
+ // attributeValue
107
+ if (typeof attributeValue !== 'string') {
108
+ throw new ArgumentError(`attributeValue must be a string, got: ${typeof attributeValue}`);
109
+ }
110
+ if (!attributeValue) {
111
+ throw new ArgumentError('attributeValue must be a non-empty string');
112
+ }
113
+
114
+ // Fallback
115
+ throw new ArgumentError('Invalid attribute assignment');
87
116
  }
117
+
118
+ return {
119
+ id: data.id as number,
120
+ type: data.type as TType,
121
+ attributeKey: data.attributeKey as string,
122
+ attributeValue: data.attributeValue as string,
123
+ };
88
124
  }
89
125
 
90
126
  /**
91
- * Validates that all items in an array are strings
92
- * @param value Array to validate
93
- * @param fieldName The name of the field for error messages
94
- * @throws ArgumentError if any item is not a string
127
+ * Validates a Resource-like object shape: { id: number; type: string }.
128
+ * Throws ArgumentError with legacy-compatible messages.
95
129
  */
96
- static validateStringArray(value: any[], fieldName: string): void {
97
- const schema = z.array(z.string());
98
- try {
99
- schema.parse(value);
100
- } catch (error) {
101
- if (error instanceof z.ZodError) {
102
- const zodError = error as z.ZodError;
103
- const firstError = zodError.issues[0];
104
- // Check if it's an array item validation error
105
- if (firstError.path.length > 0 && typeof firstError.path[0] === 'number') {
106
- const index = firstError.path[0];
107
- throw new ArgumentError(`All ${fieldName} must be strings, but item at index ${index} is not`);
108
- }
109
- throw new ArgumentError(`${fieldName} must be an array`);
130
+ static validateResource(resource: Resource): void {
131
+ if (!resource || typeof resource !== 'object') {
132
+ throw new ArgumentError('resource must be an object');
133
+ }
134
+ const schema = {
135
+ type: 'object',
136
+ properties: {
137
+ id: { type: 'number', multipleOf: 1 },
138
+ type: { type: 'string', minLength: 1 },
139
+ wrapperData: { type: 'string' },
140
+ },
141
+ required: ['type', 'id'],
142
+ additionalProperties: false,
143
+ } as const;
144
+ const validate = this.ajv.compile(schema);
145
+ const isValid = validate(resource);
146
+ if (!isValid) {
147
+ throw new ArgumentError('Invalid resource');
148
+ }
149
+ }
150
+
151
+ static validateArrayTypeOf<T>(attributesMessages: T[], messageClass: abstract new (...args: any[]) => T): void {
152
+ const arraySchema = {
153
+ type: 'array',
154
+ };
155
+ const validateArray = ValidationUtils.ajv.compile(arraySchema);
156
+ const isArrayValid = validateArray(attributesMessages);
157
+ if (!isArrayValid) {
158
+ throw new ArgumentError(`attributesMessages must be an array`);
159
+ }
160
+ for (let i = 0; i < attributesMessages.length; i++) {
161
+ const item = attributesMessages[i];
162
+ if (!(item instanceof messageClass)) {
163
+ throw new ArgumentError(
164
+ `attributesMessages[${i}] must be an instance of ${messageClass.name}, got: ${
165
+ item?.constructor?.name || typeof item
166
+ }`
167
+ );
110
168
  }
111
- throw error;
112
169
  }
113
170
  }
114
171
  }