@mondaydotcomorg/monday-authorization 3.5.2-feat-shaime-support-entity-attributes-4-ddec1d3 → 3.5.2-feat-shaime-support-entity-attributes-3-6202ab7

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 (89) hide show
  1. package/dist/authorization-attributes-ms-service.d.ts +1 -17
  2. package/dist/authorization-attributes-ms-service.d.ts.map +1 -1
  3. package/dist/authorization-attributes-ms-service.js +47 -58
  4. package/dist/authorization-attributes-sns-service.d.ts +2 -23
  5. package/dist/authorization-attributes-sns-service.d.ts.map +1 -1
  6. package/dist/authorization-attributes-sns-service.js +11 -49
  7. package/dist/base-attribute-assignment.d.ts +6 -7
  8. package/dist/base-attribute-assignment.d.ts.map +1 -1
  9. package/dist/base-attribute-assignment.js +5 -7
  10. package/dist/entity-attribute-assignment.d.ts +4 -8
  11. package/dist/entity-attribute-assignment.d.ts.map +1 -1
  12. package/dist/entity-attribute-assignment.js +4 -3
  13. package/dist/errors/argument-error.d.ts.map +1 -1
  14. package/dist/errors/argument-error.js +0 -1
  15. package/dist/esm/authorization-attributes-ms-service.d.ts +1 -17
  16. package/dist/esm/authorization-attributes-ms-service.d.ts.map +1 -1
  17. package/dist/esm/authorization-attributes-ms-service.mjs +47 -58
  18. package/dist/esm/authorization-attributes-sns-service.d.ts +2 -23
  19. package/dist/esm/authorization-attributes-sns-service.d.ts.map +1 -1
  20. package/dist/esm/authorization-attributes-sns-service.mjs +11 -49
  21. package/dist/esm/base-attribute-assignment.d.ts +6 -7
  22. package/dist/esm/base-attribute-assignment.d.ts.map +1 -1
  23. package/dist/esm/base-attribute-assignment.mjs +5 -7
  24. package/dist/esm/entity-attribute-assignment.d.ts +4 -8
  25. package/dist/esm/entity-attribute-assignment.d.ts.map +1 -1
  26. package/dist/esm/entity-attribute-assignment.mjs +4 -3
  27. package/dist/esm/errors/argument-error.d.ts.map +1 -1
  28. package/dist/esm/errors/argument-error.mjs +0 -1
  29. package/dist/esm/resource-attribute-assignment.d.ts +3 -7
  30. package/dist/esm/resource-attribute-assignment.d.ts.map +1 -1
  31. package/dist/esm/resource-attribute-assignment.mjs +3 -2
  32. package/dist/esm/resource-attributes-constants.d.ts +9 -9
  33. package/dist/esm/resource-attributes-constants.d.ts.map +1 -1
  34. package/dist/esm/resource-attributes-constants.mjs +2 -2
  35. package/dist/esm/testKit/index.d.ts +4 -4
  36. package/dist/esm/testKit/index.d.ts.map +1 -1
  37. package/dist/esm/types/authorization-attributes-contracts.d.ts +1 -2
  38. package/dist/esm/types/authorization-attributes-contracts.d.ts.map +1 -1
  39. package/dist/esm/types/authorization-attributes-service.interface.d.ts +2 -16
  40. package/dist/esm/types/authorization-attributes-service.interface.d.ts.map +1 -1
  41. package/dist/esm/types/general.d.ts +1 -1
  42. package/dist/esm/types/general.d.ts.map +1 -1
  43. package/dist/esm/utils/assigment-schema.d.ts +23 -0
  44. package/dist/esm/utils/assigment-schema.d.ts.map +1 -0
  45. package/dist/esm/utils/assigment-schema.mjs +29 -0
  46. package/dist/esm/utils/resource-schema.d.ts +17 -0
  47. package/dist/esm/utils/resource-schema.d.ts.map +1 -0
  48. package/dist/esm/utils/resource-schema.mjs +16 -0
  49. package/dist/esm/utils/validation.d.ts +9 -28
  50. package/dist/esm/utils/validation.d.ts.map +1 -1
  51. package/dist/esm/utils/validation.mjs +39 -121
  52. package/dist/resource-attribute-assignment.d.ts +3 -7
  53. package/dist/resource-attribute-assignment.d.ts.map +1 -1
  54. package/dist/resource-attribute-assignment.js +3 -2
  55. package/dist/resource-attributes-constants.d.ts +9 -9
  56. package/dist/resource-attributes-constants.d.ts.map +1 -1
  57. package/dist/resource-attributes-constants.js +2 -2
  58. package/dist/testKit/index.d.ts +4 -4
  59. package/dist/testKit/index.d.ts.map +1 -1
  60. package/dist/types/authorization-attributes-contracts.d.ts +1 -2
  61. package/dist/types/authorization-attributes-contracts.d.ts.map +1 -1
  62. package/dist/types/authorization-attributes-service.interface.d.ts +2 -16
  63. package/dist/types/authorization-attributes-service.interface.d.ts.map +1 -1
  64. package/dist/types/general.d.ts +1 -1
  65. package/dist/types/general.d.ts.map +1 -1
  66. package/dist/utils/assigment-schema.d.ts +23 -0
  67. package/dist/utils/assigment-schema.d.ts.map +1 -0
  68. package/dist/utils/assigment-schema.js +36 -0
  69. package/dist/utils/resource-schema.d.ts +17 -0
  70. package/dist/utils/resource-schema.d.ts.map +1 -0
  71. package/dist/utils/resource-schema.js +22 -0
  72. package/dist/utils/validation.d.ts +9 -28
  73. package/dist/utils/validation.d.ts.map +1 -1
  74. package/dist/utils/validation.js +39 -121
  75. package/package.json +1 -1
  76. package/src/authorization-attributes-ms-service.ts +93 -117
  77. package/src/authorization-attributes-sns-service.ts +14 -69
  78. package/src/base-attribute-assignment.ts +10 -26
  79. package/src/entity-attribute-assignment.ts +9 -6
  80. package/src/errors/argument-error.ts +0 -1
  81. package/src/resource-attribute-assignment.ts +10 -12
  82. package/src/resource-attributes-constants.ts +2 -2
  83. package/src/testKit/index.ts +5 -5
  84. package/src/types/authorization-attributes-contracts.ts +1 -1
  85. package/src/types/authorization-attributes-service.interface.ts +2 -33
  86. package/src/types/general.ts +1 -1
  87. package/src/utils/assigment-schema.ts +29 -0
  88. package/src/utils/resource-schema.ts +21 -0
  89. package/src/utils/validation.ts +51 -140
@@ -1,13 +1,6 @@
1
- import {
2
- ResourceAttributeAssignment as ResourceAttributeAssignmentContract,
3
- EntityAttributeAssignment as EntityAttributeAssignmentContract,
4
- ResourceAttributeOperation,
5
- EntityAttributeOperation,
6
- } from './authorization-attributes-contracts';
7
- import { ResourceAttributeAssignment } from '../resource-attribute-assignment';
8
- import { EntityAttributeAssignment } from '../entity-attribute-assignment';
9
- import { Resource } from './general';
10
1
  import { EntityType } from '../entity-attributes-constants';
2
+ import { ResourceAttributeOperation, EntityAttributeOperation } from './authorization-attributes-contracts';
3
+ import { Resource } from './general';
11
4
 
12
5
  /**
13
6
  * Resource type compatible with both MS and SNS services
@@ -24,18 +17,6 @@ export interface CompatibleResource {
24
17
  * Both MS (direct) and SNS (async) services implement this interface.
25
18
  */
26
19
  export interface AuthorizationAttributesService {
27
- /**authorization-attributes-service.ts
28
- * Upserts resource attributes.
29
- * For MS service: returns Promise<void>
30
- * For SNS service: requires appName and callerActionIdentifier, returns Promise<ResourceAttributesOperation[]>
31
- */
32
- upsertResourceAttributes(
33
- accountId: number,
34
- resourceAttributeAssignments: ResourceAttributeAssignment[] | ResourceAttributeAssignmentContract[],
35
- appName?: string,
36
- callerActionIdentifier?: string
37
- ): Promise<void | ResourceAttributeOperation[]>;
38
-
39
20
  /**
40
21
  * Deletes resource attributes.
41
22
  * For MS service: returns Promise<void>
@@ -49,18 +30,6 @@ export interface AuthorizationAttributesService {
49
30
  callerActionIdentifier?: string
50
31
  ): Promise<void | ResourceAttributeOperation[]>;
51
32
 
52
- /**
53
- * Upserts entity attributes.
54
- * For MS service: returns Promise<void>
55
- * For SNS service: requires appName and callerActionIdentifier, returns Promise<EntityAttributesOperation[]>
56
- */
57
- upsertEntityAttributes(
58
- accountId: number,
59
- entityAttributeAssignments: EntityAttributeAssignment[] | EntityAttributeAssignmentContract[],
60
- appName?: string,
61
- callerActionIdentifier?: string
62
- ): Promise<void | EntityAttributeOperation[]>;
63
-
64
33
  /**
65
34
  * Deletes entity attributes.
66
35
  * For MS service: returns Promise<void>
@@ -1,7 +1,7 @@
1
1
  import type { Request, Response } from 'express';
2
2
 
3
3
  export interface Resource {
4
- id?: number;
4
+ id: number;
5
5
  type: string;
6
6
  wrapperData?: object;
7
7
  }
@@ -0,0 +1,29 @@
1
+ import Ajv from 'ajv';
2
+ import { EntityType } from '../entity-attributes-constants';
3
+ import { ResourceType } from '../resource-attributes-constants';
4
+
5
+ const ajv = new Ajv({ allErrors: true });
6
+
7
+ export const entityAssignmentSchema = ajv.compile({
8
+ type: 'object',
9
+ properties: {
10
+ entityId: { type: 'number', multipleOf: 1 },
11
+ entityType: { type: 'string', enum: Object.values(EntityType) },
12
+ key: { type: 'string', minLength: 1 },
13
+ value: { type: 'string' },
14
+ },
15
+ required: ['entityId', 'entityType', 'key'],
16
+ additionalProperties: false,
17
+ });
18
+
19
+ export const resourceAssignmentSchema = ajv.compile({
20
+ type: 'object',
21
+ properties: {
22
+ id: { type: 'number', multipleOf: 1 },
23
+ type: { type: 'string', enum: Object.values(ResourceType) },
24
+ key: { type: 'string', minLength: 1 },
25
+ value: { type: 'string' },
26
+ },
27
+ required: ['id', 'type', 'key'],
28
+ additionalProperties: false,
29
+ });
@@ -0,0 +1,21 @@
1
+ import Ajv from 'ajv';
2
+ import { RESOURCE_TYPES } from '../resource-attributes-constants';
3
+
4
+ const ajv = new Ajv({ allErrors: true });
5
+
6
+ export interface Resource {
7
+ id?: number;
8
+ type: string;
9
+ wrapperData?: object;
10
+ }
11
+
12
+ export const resourceSchema = ajv.compile({
13
+ type: 'object',
14
+ properties: {
15
+ id: { type: 'number', multipleOf: 1 },
16
+ type: { type: 'string', enum: RESOURCE_TYPES },
17
+ wrapperData: { type: 'string', minLength: 1 },
18
+ },
19
+ required: ['id', 'type', 'wrapperData'],
20
+ additionalProperties: false,
21
+ });
@@ -1,171 +1,82 @@
1
- import Ajv from 'ajv';
1
+ import Ajv, { ValidateFunction, ErrorObject } from 'ajv';
2
2
  import { ArgumentError } from '../errors/argument-error';
3
+ import { ResourceAttributeAssignment, EntityAttributeAssignment } from '../types/authorization-attributes-contracts';
3
4
  import { Resource } from '../types/general';
5
+ import { entityAssignmentSchema, resourceAssignmentSchema } from './assigment-schema';
6
+ import { resourceSchema } from './resource-schema';
4
7
 
5
8
  /**
6
9
  * Utility class for common validation operations using AJV
7
10
  */
8
11
  export class ValidationUtils {
9
12
  private static ajv = new Ajv({ allErrors: true });
10
- /**
11
- * Validates that a value is an integer. Throws ArgumentError with a descriptive message on failure.
12
- */
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}`);
13
+
14
+ private static integerValidator: ValidateFunction = this.ajv.compile({ type: 'number', multipleOf: 1 });
15
+ private static stringValidator: ValidateFunction = this.ajv.compile({ type: 'string', minLength: 1 });
16
+ private static stringArrayValidator: ValidateFunction = this.ajv.compile({
17
+ type: 'array',
18
+ items: { type: 'string' },
19
+ });
20
+
21
+ public static validateInteger(value: number): void {
22
+ const valid = this.integerValidator(value);
23
+ if (!valid) {
24
+ throw new ArgumentError('Invalid integer');
19
25
  }
20
26
  }
21
27
 
22
- /**
23
- * Validates that a value is a non-empty string. Throws ArgumentError on failure.
24
- */
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}`);
28
+ public static validateString(value: string): void {
29
+ const valid = this.stringValidator(value);
30
+ if (!valid) {
31
+ throw new ArgumentError('Invalid string');
35
32
  }
36
33
  }
37
34
 
38
- /**
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.
41
- */
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);
35
+ public static validateStringArray(value: string[]): void {
36
+ const valid = this.stringArrayValidator(value);
37
+ if (!valid) {
38
+ throw new ArgumentError('Invalid string array');
39
+ }
40
+ }
41
+
42
+ public static validateResource(resource: Resource) {
43
+ const isValid = resourceSchema(resource);
53
44
  if (!isValid) {
54
- throw new ArgumentError(`Value must be an array of non-empty strings, got: ${value}`);
45
+ const errorMessage = this.formatValidationErrors(resourceSchema.errors);
46
+ throw new ArgumentError(errorMessage ? `Invalid resource: ${errorMessage}` : 'Invalid resource');
55
47
  }
56
- // Allow empty arrays to pass validation - caller should handle early return
57
- // Non-empty arrays will be validated for string content above
58
48
  }
59
49
 
60
50
  /**
61
51
  * Validates an attribute assignment object using a single AJV schema.
62
52
  * Preserves legacy error messages for each field.
63
53
  */
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);
54
+ static validateResourceAssignment(resource: ResourceAttributeAssignment): void {
55
+ const valid = resourceAssignmentSchema(resource);
83
56
  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');
104
- }
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');
57
+ const errorMessage = this.formatValidationErrors(resourceAssignmentSchema.errors);
58
+ throw new ArgumentError(
59
+ errorMessage
60
+ ? `Invalid resource attribute assignment: ${errorMessage}`
61
+ : 'Invalid resource attribute assignment'
62
+ );
116
63
  }
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
- };
124
64
  }
125
65
 
126
- /**
127
- * Validates a Resource-like object shape: { id: number; type: string }.
128
- * Throws ArgumentError with legacy-compatible messages.
129
- */
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');
66
+ static validateEntityAssignment(entity: EntityAttributeAssignment): void {
67
+ const valid = entityAssignmentSchema(entity);
68
+ if (!valid) {
69
+ const errorMessage = this.formatValidationErrors(entityAssignmentSchema.errors);
70
+ throw new ArgumentError(
71
+ errorMessage ? `Invalid entity attribute assignment: ${errorMessage}` : 'Invalid entity attribute assignment'
72
+ );
148
73
  }
149
74
  }
150
75
 
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
- );
168
- }
169
- }
76
+ /**
77
+ * Formats AJV validation errors into a readable error message
78
+ */
79
+ private static formatValidationErrors(errors: ErrorObject[] | null | undefined): string {
80
+ return (errors || []).join();
170
81
  }
171
82
  }