@tasolutions/express-core
Version:
All libs for express
51 lines (44 loc) • 1.97 kB
JavaScript
const _ = require('lodash');
module.exports = {
createFieldObject: (field, schema, actions, createSelectOptions) => {
const key = field.path;
const fieldOption = field.options || {};
const label = _.startCase(_.toLower(fieldOption.label || key));
const fieldObject = {
key: key,
type: null,
type_sub: null,
show: fieldOption.show ?? true,
is_required: field.isRequired || false,
label,
html: {
placeholder: `Please input ${label}`,
disabled: false,
read_only: false,
col: 12,
},
select_options: fieldOption.select_options ?? null,
db_type: field.instance,
max_length: fieldOption.max_length ?? null,
};
if (createSelectOptions && field.instance === 'ObjectId' && !fieldObject.select_options) {
fieldObject.select_options = createSelectOptions(field, schema, actions);
fieldObject.type = 'SELECT';
fieldObject.max_length = 1;
}
if (field.instance === 'Array') {
fieldObject.type = 'ARRAY';
if (field.caster && field.caster.options) {
const fieldCasterOptions = field.caster.options;
fieldObject.type = fieldCasterOptions.field_type || 'ARRAY';
fieldObject.type_sub = fieldCasterOptions.field_type_sub || null;
fieldObject.select_options = fieldCasterOptions.select_options || null;
fieldObject.max_length = fieldCasterOptions.max_length || 2;
}
} else {
fieldObject.type = fieldOption.field_type ? fieldOption.field_type.toUpperCase() : (field.instance === 'String' ? 'TEXT' : field.instance.toUpperCase());
}
fieldObject.type_sub = fieldObject.type_sub?.toUpperCase() || null;
return fieldObject;
}
}