@autofleet/sadot 0.13.0-beta.1 → 0.13.0-beta.10

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.
@@ -69,7 +69,7 @@ router.get('/', async (req, res) => {
69
69
  ...(entityType && { entityType }),
70
70
  };
71
71
  const validators = await ValidatorRepo.findAll(where);
72
- return res.status(http_status_codes_1.StatusCodes.OK).json(validators);
72
+ return res.status(http_status_codes_1.StatusCodes.OK).json({ validators });
73
73
  }
74
74
  catch (err) {
75
75
  return (0, errors_2.default)(err, res, { logger: logger_1.default, message: `Error in get all ${ENTITY} request` });
@@ -23,7 +23,6 @@ const validationSchemas = {
23
23
  }),
24
24
  update: joi_1.default.object({
25
25
  entityId: joi_1.default.string().uuid(),
26
- entityType: joi_1.default.string(),
27
26
  schema: joi_1.default.object({
28
27
  type: joi_1.default.string().valid('object'),
29
28
  properties: joi_1.default.object({
@@ -28,6 +28,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.beforeBulkUpdate = exports.beforeBulkCreate = exports.beforeUpdate = exports.beforeCreate = void 0;
30
30
  const ajv_1 = __importDefault(require("ajv"));
31
+ const ajv_formats_1 = __importDefault(require("ajv-formats"));
31
32
  const errors_1 = require("@autofleet/errors");
32
33
  const logger_1 = __importDefault(require("../utils/logger"));
33
34
  const ValidatorRepo = __importStar(require("../repository/validator"));
@@ -40,7 +41,9 @@ const ajv = new ajv_1.default({
40
41
  allErrors: true,
41
42
  strict: false, // Disable strict mode to avoid warnings
42
43
  strictTypes: false, // Disable strict type checking
44
+ $data: true, // Enable $data references
43
45
  });
46
+ (0, ajv_formats_1.default)(ajv);
44
47
  /**
45
48
  * Validates the model using custom validators
46
49
  */
@@ -104,31 +107,41 @@ const validateModel = async (instance, options, scopeAttributes, isCreate = fals
104
107
  after: typedSchema.properties.after,
105
108
  },
106
109
  });
107
- const isValid = validateSchema({
110
+ const isValid = validateSchema(JSON.stringify({
108
111
  after: {
109
112
  ...instance.dataValues,
110
- ...instance.customFields,
113
+ customFields: instance.customFields,
111
114
  },
112
- });
115
+ }));
113
116
  if (!isValid) {
114
117
  const errorDetails = validateSchema.errors?.map((err) => `${err.instancePath || ''} ${err.message || 'Invalid value'}`).join(', ');
115
- throw new errors_1.BadRequest([new Error(`Validation failed for ${modelType}: ${errorDetails}`)], [`Validation failed for ${modelType}`]);
118
+ throw new errors_1.BadRequest([new Error(`Validation failed for ${modelType}: ${errorDetails}`)]);
116
119
  }
117
120
  }
118
121
  }
119
122
  else {
120
123
  // For update operations, we need both before and after
121
124
  const validateSchema = ajv.compile(typedSchema);
122
- const isValid = validateSchema({
125
+ const isValid = validateSchema(JSON.stringify({
123
126
  before: originalValues,
124
127
  after: {
125
128
  ...instance.dataValues,
126
- ...instance.customFields,
129
+ customFields: instance.customFields,
130
+ },
131
+ }));
132
+ logger_1.default.info('sadot - validation result', {
133
+ isValid,
134
+ test: {
135
+ before: originalValues,
136
+ after: {
137
+ ...instance.dataValues,
138
+ ...instance.customFields,
139
+ },
127
140
  },
128
141
  });
129
142
  if (!isValid) {
130
143
  const errorDetails = validateSchema.errors?.map((err) => `${err.instancePath || ''} ${err.message || 'Invalid value'}`).join(', ');
131
- throw new errors_1.BadRequest([new Error(`Validation failed for ${modelType}: ${errorDetails}`)], [`Validation failed for ${modelType}`]);
144
+ throw new errors_1.BadRequest([new Error(`Validation failed for ${modelType}: ${errorDetails}`)]);
132
145
  }
133
146
  }
134
147
  }
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.validateValidatorSchema = void 0;
7
7
  const ajv_1 = __importDefault(require("ajv"));
8
+ const ajv_formats_1 = __importDefault(require("ajv-formats"));
8
9
  const errors_1 = require("@autofleet/errors");
9
10
  const logger_1 = __importDefault(require("../../logger"));
10
11
  // Instantiate Ajv for meta-validation
@@ -12,7 +13,9 @@ const metaValidator = new ajv_1.default({
12
13
  allErrors: true,
13
14
  strict: false,
14
15
  strictTypes: false,
16
+ $data: true, // Enable $data references
15
17
  });
18
+ (0, ajv_formats_1.default)(metaValidator);
16
19
  /**
17
20
  * Schema for validating JSON Schema objects in custom validators
18
21
  * This is a meta-schema to ensure that custom validator schemas are valid Ajv schemas
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "0.13.0-beta.1",
3
+ "version": "0.13.0-beta.10",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -30,6 +30,7 @@
30
30
  "@autofleet/common-types": "^4.4.0",
31
31
  "@autofleet/events": "^4.0.0",
32
32
  "ajv": "^8.12.0",
33
+ "ajv-formats": "^3.0.1",
33
34
  "http-status-codes": "^2.3.0",
34
35
  "joi": "^17.7.0",
35
36
  "pg": "^8.10.0",
@@ -39,7 +39,7 @@ router.post<{ modelName: string }>('/', async (req, res) => {
39
39
  */
40
40
  router.get<
41
41
  { modelName: string },
42
- CustomValidator[],
42
+ { validators: CustomValidator[] },
43
43
  never,
44
44
  { entityId?: string; entityType?: string }
45
45
  >('/', async (req, res) => {
@@ -55,7 +55,7 @@ router.get<
55
55
 
56
56
  const validators = await ValidatorRepo.findAll(where);
57
57
 
58
- return res.status(StatusCodes.OK).json(validators);
58
+ return res.status(StatusCodes.OK).json({ validators });
59
59
  } catch (err) {
60
60
  return handleError(err, res, { logger, message: `Error in get all ${ENTITY} request` });
61
61
  }
@@ -21,7 +21,6 @@ const validationSchemas = {
21
21
 
22
22
  update: Joi.object({
23
23
  entityId: Joi.string().uuid(),
24
- entityType: Joi.string(),
25
24
  schema: Joi.object({
26
25
  type: Joi.string().valid('object'),
27
26
  properties: Joi.object({
@@ -1,5 +1,6 @@
1
1
  import type { WhereOptions } from 'sequelize';
2
2
  import Ajv from 'ajv';
3
+ import addFormats from 'ajv-formats';
3
4
  import { BadRequest } from '@autofleet/errors';
4
5
  import logger from '../utils/logger';
5
6
  import * as ValidatorRepo from '../repository/validator';
@@ -14,8 +15,11 @@ const ajv = new Ajv({
14
15
  allErrors: true,
15
16
  strict: false, // Disable strict mode to avoid warnings
16
17
  strictTypes: false, // Disable strict type checking
18
+ $data: true, // Enable $data references
17
19
  });
18
20
 
21
+ addFormats(ajv);
22
+
19
23
  /**
20
24
  * Validates the model using custom validators
21
25
  */
@@ -103,29 +107,40 @@ const validateModel = async (
103
107
  },
104
108
  });
105
109
 
106
- const isValid = validateSchema({
110
+ const isValid = validateSchema(JSON.stringify({
107
111
  after: {
108
112
  ...instance.dataValues,
109
- ...instance.customFields,
113
+ customFields: instance.customFields,
110
114
  },
111
- });
115
+ }));
112
116
 
113
117
  if (!isValid) {
114
118
  const errorDetails = validateSchema.errors?.map((err) =>
115
119
  `${(err as any).instancePath || ''} ${(err as any).message || 'Invalid value'}`).join(', ');
116
120
 
117
- throw new BadRequest([new Error(`Validation failed for ${modelType}: ${errorDetails}`)], [`Validation failed for ${modelType}`]);
121
+ throw new BadRequest([new Error(`Validation failed for ${modelType}: ${errorDetails}`)]);
118
122
  }
119
123
  }
120
124
  } else {
121
125
  // For update operations, we need both before and after
122
126
  const validateSchema = ajv.compile(typedSchema);
123
127
 
124
- const isValid = validateSchema({
128
+ const isValid = validateSchema(JSON.stringify({
125
129
  before: originalValues,
126
130
  after: {
127
131
  ...instance.dataValues,
128
- ...instance.customFields,
132
+ customFields: instance.customFields,
133
+ },
134
+ }));
135
+
136
+ logger.info('sadot - validation result', {
137
+ isValid,
138
+ test: {
139
+ before: originalValues,
140
+ after: {
141
+ ...instance.dataValues,
142
+ ...instance.customFields,
143
+ },
129
144
  },
130
145
  });
131
146
 
@@ -133,7 +148,7 @@ const validateModel = async (
133
148
  const errorDetails = validateSchema.errors?.map((err) =>
134
149
  `${(err as any).instancePath || ''} ${(err as any).message || 'Invalid value'}`).join(', ');
135
150
 
136
- throw new BadRequest([new Error(`Validation failed for ${modelType}: ${errorDetails}`)], [`Validation failed for ${modelType}`]);
151
+ throw new BadRequest([new Error(`Validation failed for ${modelType}: ${errorDetails}`)]);
137
152
  }
138
153
  }
139
154
  }
@@ -1,4 +1,5 @@
1
1
  import Ajv from 'ajv';
2
+ import addFormats from 'ajv-formats';
2
3
  import { BadRequest } from '@autofleet/errors';
3
4
  import logger from '../../logger';
4
5
 
@@ -7,7 +8,9 @@ const metaValidator = new Ajv({
7
8
  allErrors: true,
8
9
  strict: false,
9
10
  strictTypes: false,
11
+ $data: true, // Enable $data references
10
12
  });
13
+ addFormats(metaValidator);
11
14
 
12
15
  /**
13
16
  * Schema for validating JSON Schema objects in custom validators
@@ -0,0 +1,79 @@
1
+ const Ajv = require("ajv");
2
+ const addFormats = require("ajv-formats");
3
+ const ajv = new Ajv({
4
+ allErrors: true,
5
+ strict: false, // Disable strict mode to avoid warnings
6
+ strictTypes: false, // Disable strict type checking
7
+ $data: true, // Enable $data references
8
+ }); // options can be passed, e.g. {allErrors: true}
9
+ addFormats(ajv);
10
+
11
+
12
+ const schema = {
13
+ "type": "object",
14
+ "properties": {
15
+ "after": {
16
+ "type": "object",
17
+ "properties": {
18
+ "customFields": {
19
+ "type": "object",
20
+ "properties": {
21
+ "actualStartTime": {
22
+ "type": "string",
23
+ "format": "date-time"
24
+ },
25
+ "actualEndTime": {
26
+ "type": "string",
27
+ "format": "date-time",
28
+ "formatMinimum": {
29
+ "$data": "/after/customFields/actualStartTime"
30
+ }
31
+ }
32
+ }
33
+ }
34
+ }
35
+ }
36
+ }
37
+ };
38
+
39
+ const validate = ajv.compile(schema);
40
+
41
+ const data = {
42
+ "id": "c9c87cec-28c7-4a1f-9376-4d8a4ba0f49c",
43
+ "typeId": "4fcd7096-4c90-46a2-a20e-91bb5bff3ced",
44
+ "priorityId": null,
45
+ "statusId": "51d28a37-c042-429f-a9cd-fbc81bba2b39",
46
+ "subjectId": null,
47
+ "subjectType": null,
48
+ "title": "ff",
49
+ "description": null,
50
+ "dueTime": null,
51
+ "startTime": null,
52
+ "endTime": null,
53
+ "driverId": null,
54
+ "vendorId": null,
55
+ "userId": null,
56
+ "businessModelId": "72ecf740-42a1-46f4-81ca-8132b08c4f04",
57
+ "createdAt": "2025-03-12T12:58:05.529Z",
58
+ "updatedAt": "2025-03-12T12:58:05.529Z",
59
+ "deletedAt": null,
60
+ "actions": [],
61
+ "customFields": {
62
+ "actualStartTime": "2025-03-14T12:48:01.373Z",
63
+ "actualEndTime": "2025-03-01T12:51:46.685Z"
64
+ },
65
+ "isBlocker": false
66
+ };
67
+
68
+ const valid = validate({after: data});
69
+
70
+ if (!valid) {
71
+ console.log("invalid, the errors is: ");
72
+ validate.errors.forEach((error) =>
73
+ console.log(error.instancePath, error.message)
74
+ );
75
+ } else {
76
+ console.log("valid");
77
+ }
78
+
79
+ console.log("--------------------");