openchs-models 1.33.5 → 1.33.7

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.
@@ -51,19 +51,39 @@ class RealmProxy {
51
51
  */
52
52
  create(schemaName, object, updateMode = "never") {
53
53
  const entityClass = this.entityMappingConfig.getEntityClass(schemaName);
54
- const underlyingObject = (0, _RealmCollectionHelper.getUnderlyingRealmObject)(object);
55
54
  const schema = entityClass.schema;
56
55
 
57
- // 🚀 FRAMEWORK-LEVEL: Automatically process embedded objects for Realm 12+ safety
58
- const processedObject = _RealmEmbeddedObjectHandler.default.processEmbeddedObjects(underlyingObject, schema);
59
- const mandatoryObjectSchemaProperties = _lodash.default.keys(_lodash.default.pickBy(schema.properties, property => !property.optional));
56
+ // Extract the underlying data from entity wrapper if needed
57
+ const rawObject = (0, _RealmCollectionHelper.getUnderlyingRealmObject)(object) || object;
58
+
59
+ // Automatically process embedded objects for Realm 12+ safety
60
+ const processedObject = _RealmEmbeddedObjectHandler.default.processEmbeddedObjects(rawObject, schema);
61
+
62
+ // Only validate properties that are truly mandatory (no optional: true AND no default value)
63
+ // This maintains backward compatibility with existing data and Realm's native behavior
64
+ const mandatoryObjectSchemaProperties = _lodash.default.keys(_lodash.default.pickBy(schema.properties, property => !property.optional && !property.hasOwnProperty('default')));
60
65
  const emptyMandatoryProperties = [];
61
66
  const saveObjectKeys = Object.keys(processedObject);
62
67
  if (updateMode === "never" || updateMode === false || _lodash.default.intersection(mandatoryObjectSchemaProperties, saveObjectKeys).length > 0) {
68
+ // Check for null/undefined mandatory properties that are present in the object
63
69
  saveObjectKeys.forEach(x => {
64
70
  const propertyValue = processedObject[x];
65
- if (_lodash.default.isNil(propertyValue) && _lodash.default.some(mandatoryObjectSchemaProperties, y => y === x)) emptyMandatoryProperties.push(x);
71
+ if (_lodash.default.isNil(propertyValue) && _lodash.default.some(mandatoryObjectSchemaProperties, y => y === x)) {
72
+ emptyMandatoryProperties.push(x);
73
+ }
66
74
  });
75
+
76
+ // Enhanced validation: Only check for missing mandatory properties in strict update modes
77
+ // or when we have some mandatory properties present (indicating intent to save a complete entity)
78
+ const isStrictUpdateMode = updateMode === "never" || updateMode === false;
79
+ const hasSomeMandatoryProperties = _lodash.default.intersection(mandatoryObjectSchemaProperties, saveObjectKeys).length > 0;
80
+ if (isStrictUpdateMode && hasSomeMandatoryProperties) {
81
+ mandatoryObjectSchemaProperties.forEach(mandatoryProp => {
82
+ if (!saveObjectKeys.includes(mandatoryProp)) {
83
+ emptyMandatoryProperties.push(mandatoryProp);
84
+ }
85
+ });
86
+ }
67
87
  if (emptyMandatoryProperties.length > 0) {
68
88
  throw new Error(`${emptyMandatoryProperties.join(",")} are mandatory for ${schemaName}, Keys being saved - ${saveObjectKeys}. UUID: ${processedObject.uuid}`);
69
89
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "openchs-models",
3
3
  "description": "OpenCHS data model to be used by front end clients",
4
- "version": "1.33.5",
4
+ "version": "1.33.7",
5
5
  "private": false,
6
6
  "repository": {
7
7
  "type": "git",