export function flattenSchema()

in packages/form-render/src/models/flattenSchema.ts [43:86]


export function flattenSchema(_schema = {}, name?: any, parent?: any, _result?: any) {
  // 排序
  // _schema = orderBy(_schema, item => item.order, ['asc']);

  const result = _result || {};

  const schema: any = _cloneDeep(_schema) || {};
  let _name = name || '#';
  if (!schema.$id) {
    schema.$id = _name; // path as $id, for easy access to path in schema
  }
  const children: any[] = [];
  if (isObjType(schema)) {
    sortProperties(Object.entries(schema.properties)).forEach(
      ([key, value]) => {
        const _key = isListType(value) ? key + '[]' : key;
        const uniqueName = _name === '#' ? _key : _name + '.' + _key;
        children.push(uniqueName);

        flattenSchema(value, uniqueName, _name, result);
      }
    );

    schema.properties = {};
  }
  if (isListType(schema)) {
    sortProperties(Object.entries(schema.items.properties)).forEach(
      ([key, value]) => {
        const _key = isListType(value) ? key + '[]' : key;
        const uniqueName = _name === '#' ? _key : _name + '.' + _key;
        children.push(uniqueName);
        flattenSchema(value, uniqueName, _name, result);
      }
    );

    schema.items.properties = {};
  }

  if (schema.type) {
    result[_name] = { parent, schema, children };
  }
 
  return result;
}