Skip to content

Commit

Permalink
♻️ Subschema field fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Mar 16, 2024
1 parent c2c9c73 commit 93b7df7
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions lib/app/helper_field/schema_field.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,16 @@ SchemaField.setMethod(function resolveSchemaPath(context_schema, record, field_p
record_value = field.getRecordValue(record, field_path);
}

// If there is no value, return false
if (!record_value) {
return false;
}

// Get the correct field value
let enum_value = values.get(record_value);

if (!enum_value) {
schema = null;
schema = false;
} else if (enum_value[property_name]) {
schema = enum_value[property_name];
} else if (enum_value.schema) {
Expand Down Expand Up @@ -317,11 +322,8 @@ SchemaField.setMethod(function _toDatasource(value, holder, datasource, callback
*/
SchemaField.setMethod(function _toDatasourceFromValue(value, holder, datasource, callback) {

var that = this,
sub_schema,
record,
model,
temp;
let sub_schema,
record;

if (this.schema.name) {
record = {
Expand Down Expand Up @@ -371,13 +373,17 @@ SchemaField.setMethod(function _toDatasourceFromValueWithSubSchema(value, holder
return datasource.toDatasource(sub_schema, value, callback);
}

if (sub_schema === false) {
return callback(null, null);
}

// @WARNING:
// Doing a model.save with ONLY updates can break subschema behaviour!

// If it has not been found, and the linked schema is actually a string,
// it's possible the linked field was not provided
if (typeof this.options.schema == 'string') {
model = this.root_model;
let model = this.root_model;

if (model) {
model = Model.get(model);
Expand All @@ -394,7 +400,7 @@ SchemaField.setMethod(function _toDatasourceFromValueWithSubSchema(value, holder
}

// Add the newly found data
temp = Object.assign({}, holder);
let temp = Object.assign({}, holder);
record = {};
record[that.schema.name] = temp;

Expand Down Expand Up @@ -537,6 +543,12 @@ SchemaField.setMethod(async function _toAppFromValue(query, options, value, call
*/
SchemaField.setMethod(async function _toAppFromValueWithSubSchema(query, options, value, sub_schema, record, callback) {

// Explicit false means there is no schema at the moment,
// and the value can be ignored
if (sub_schema === false) {
return callback(null, null);
}

let that = this,
Dummy,
item,
Expand Down

0 comments on commit 93b7df7

Please sign in to comment.