@autofleet/sadot 1.0.2 → 1.0.3

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.
@@ -4,36 +4,34 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const joi_1 = __importDefault(require("joi"));
7
- // Schema for validating JSON Schema objects
8
7
  const jsonSchemaValidation = joi_1.default.object().unknown(true);
8
+ const schemaObject = joi_1.default.object({
9
+ type: joi_1.default.string().valid('object'),
10
+ properties: joi_1.default.object({
11
+ before: jsonSchemaValidation,
12
+ after: jsonSchemaValidation,
13
+ }).required(),
14
+ required: joi_1.default.array().items(joi_1.default.string()),
15
+ allOf: joi_1.default.array().items(joi_1.default.object()),
16
+ anyOf: joi_1.default.array().items(joi_1.default.object()),
17
+ oneOf: joi_1.default.array().items(joi_1.default.object()),
18
+ additionalProperties: joi_1.default.alternatives().try(joi_1.default.boolean(), joi_1.default.object()),
19
+ $id: joi_1.default.string(),
20
+ $schema: joi_1.default.string(),
21
+ if: joi_1.default.object(),
22
+ then: joi_1.default.object(),
23
+ else: joi_1.default.object(),
24
+ });
9
25
  const validationSchemas = {
10
26
  create: joi_1.default.object({
11
27
  entityId: joi_1.default.string().uuid().required(),
12
28
  entityType: joi_1.default.string().required(),
13
- schema: joi_1.default.object({
14
- type: joi_1.default.string().valid('object').required(),
15
- properties: joi_1.default.object({
16
- before: jsonSchemaValidation,
17
- after: jsonSchemaValidation,
18
- }).required(),
19
- if: joi_1.default.object().optional(),
20
- then: joi_1.default.object().optional(),
21
- else: joi_1.default.object().optional(),
22
- }).required(),
29
+ schema: schemaObject.required(),
23
30
  }),
24
31
  update: joi_1.default.object({
25
32
  entityId: joi_1.default.string().uuid(),
26
33
  entityType: joi_1.default.string(),
27
- schema: joi_1.default.object({
28
- type: joi_1.default.string().valid('object'),
29
- properties: joi_1.default.object({
30
- before: jsonSchemaValidation,
31
- after: jsonSchemaValidation,
32
- }),
33
- if: joi_1.default.object().optional(),
34
- then: joi_1.default.object().optional(),
35
- else: joi_1.default.object().optional(),
36
- }),
34
+ schema: schemaObject,
37
35
  disabled: joi_1.default.boolean(),
38
36
  }).min(1),
39
37
  };
@@ -90,6 +90,21 @@ const getCompleteCustomFields = async (instance, options) => {
90
90
  }
91
91
  return instance.customFields || {};
92
92
  };
93
+ const buildPreChangeState = (instance) => {
94
+ const beforeFull = { ...instance.dataValues };
95
+ const changedKeys = instance.changed?.() || [];
96
+ changedKeys.forEach((key) => {
97
+ const prevVal = instance.previous?.(key);
98
+ if (prevVal !== undefined) {
99
+ beforeFull[key] = prevVal;
100
+ }
101
+ });
102
+ const prevCF = instance.previous?.('customFields');
103
+ if (prevCF !== undefined) {
104
+ beforeFull.customFields = prevCF;
105
+ }
106
+ return beforeFull;
107
+ };
93
108
  const formatAjvErrors = (errors) => errors.reduce((acc, err) => {
94
109
  const basePath = (err.instancePath || '')
95
110
  .split('/')
@@ -150,10 +165,7 @@ const validateModel = async (instance, options, scopeAttributes, modelOptions =
150
165
  // For updates, get the previous values
151
166
  let originalValues = null;
152
167
  if (!isCreate) {
153
- // Create originalValues with our helper function
154
- originalValues = manualObjectCopy(instance.previous());
155
- // Add customFields separately
156
- originalValues.customFields = instance.previous('customFields') || {};
168
+ originalValues = buildPreChangeState(instance);
157
169
  }
158
170
  // Get complete custom fields by merging DB values with update values
159
171
  // This is especially important for partial updates to ensure all related fields are available
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -1,37 +1,36 @@
1
1
  import Joi from 'joi';
2
2
 
3
- // Schema for validating JSON Schema objects
4
3
  const jsonSchemaValidation = Joi.object().unknown(true);
5
4
 
5
+ const schemaObject = Joi.object({
6
+ type: Joi.string().valid('object'),
7
+ properties: Joi.object({
8
+ before: jsonSchemaValidation,
9
+ after: jsonSchemaValidation,
10
+ }).required(),
11
+ required: Joi.array().items(Joi.string()),
12
+ allOf: Joi.array().items(Joi.object()),
13
+ anyOf: Joi.array().items(Joi.object()),
14
+ oneOf: Joi.array().items(Joi.object()),
15
+ additionalProperties: Joi.alternatives().try(Joi.boolean(), Joi.object()),
16
+ $id: Joi.string(),
17
+ $schema: Joi.string(),
18
+ if: Joi.object(),
19
+ then: Joi.object(),
20
+ else: Joi.object(),
21
+ });
22
+
6
23
  const validationSchemas = {
7
24
  create: Joi.object({
8
25
  entityId: Joi.string().uuid().required(),
9
26
  entityType: Joi.string().required(),
10
- schema: Joi.object({
11
- type: Joi.string().valid('object').required(),
12
- properties: Joi.object({
13
- before: jsonSchemaValidation,
14
- after: jsonSchemaValidation,
15
- }).required(),
16
- if: Joi.object().optional(),
17
- then: Joi.object().optional(),
18
- else: Joi.object().optional(),
19
- }).required(),
27
+ schema: schemaObject.required(),
20
28
  }),
21
29
 
22
30
  update: Joi.object({
23
31
  entityId: Joi.string().uuid(),
24
32
  entityType: Joi.string(),
25
- schema: Joi.object({
26
- type: Joi.string().valid('object'),
27
- properties: Joi.object({
28
- before: jsonSchemaValidation,
29
- after: jsonSchemaValidation,
30
- }),
31
- if: Joi.object().optional(),
32
- then: Joi.object().optional(),
33
- else: Joi.object().optional(),
34
- }),
33
+ schema: schemaObject,
35
34
  disabled: Joi.boolean(),
36
35
  }).min(1),
37
36
  };
@@ -78,6 +78,25 @@ const getCompleteCustomFields = async (instance, options): Promise<Record<string
78
78
  return instance.customFields || {};
79
79
  };
80
80
 
81
+ const buildPreChangeState = (instance: any) => {
82
+ const beforeFull: any = { ...instance.dataValues };
83
+
84
+ const changedKeys: string[] = instance.changed?.() || [];
85
+ changedKeys.forEach((key) => {
86
+ const prevVal = instance.previous?.(key);
87
+ if (prevVal !== undefined) {
88
+ beforeFull[key] = prevVal;
89
+ }
90
+ });
91
+
92
+ const prevCF = instance.previous?.('customFields');
93
+ if (prevCF !== undefined) {
94
+ beforeFull.customFields = prevCF;
95
+ }
96
+
97
+ return beforeFull;
98
+ };
99
+
81
100
  const formatAjvErrors = (
82
101
  errors: {
83
102
  instancePath?: string;
@@ -169,11 +188,7 @@ const validateModel = async (
169
188
  // For updates, get the previous values
170
189
  let originalValues = null;
171
190
  if (!isCreate) {
172
- // Create originalValues with our helper function
173
- originalValues = manualObjectCopy(instance.previous());
174
-
175
- // Add customFields separately
176
- originalValues.customFields = instance.previous('customFields') || {};
191
+ originalValues = buildPreChangeState(instance);
177
192
  }
178
193
 
179
194
  // Get complete custom fields by merging DB values with update values