JSONSchema形式数据和普通数组对象结构数据相互转化

JSONSchema形式数据和普通数组对象结构数据相互转化

例子:

let arr = [
    {
        paramsName: "skuList",
        paramsDesc: "sku列表",
        paramsType: "array",
        paramsRequired: true,
        children: [
            {
                paramsName: "",
                paramsDesc: "列表项",
                paramsType: "object",
                paramsRequired: true,
                children: [
                    {
                        paramsName: "skuId",
                        paramsDesc: "商品id",
                        paramsType: "number",
                        paramsRequired: true
                    },
                    {
                        paramsName: "skuName",
                        paramsDesc: "商品名称",
                        paramsType: "string",
                        paramsRequired: true
                    }
                ]
            }
        ]
    }
]

let jsonSchema = {
	"type": "object",
	"properties": {
		"skuList": {
			"type": "array",
			"description": "商品列表",
			"items": {
				"type": "object",
				"description": "商品项",
				"properties": {
					"skuId": {
						"type": "number",
						"description": "商品id"
					},
					"skuName": {
						"type": "string",
						"description": "商品名称"
					}
				},
				"required": ["skuId", "skuName"]
			}
		}
	},
	"required": ["skuList"]
}
// 将JSONSchema形式数据处理成普通数组结构----转换成数组
convertFromJSONSchema(jsonSchema) {
    // 递归函数来处理每个属性和它的子属性
    const processProperties = (properties, required = []) =>
        Object.entries(properties).map(([key, prop]) => {
            const item = {
                paramsName: key,
                paramsDesc: prop.description || '',
                paramsType: prop.type,
                paramsRequired: required.includes(key)
            };

            if (prop.type === 'object' && prop.properties) {
                item.children = processProperties(prop.properties, prop.required || []);
            } else if (prop.type === 'array' && prop.items && prop.items.properties) {
                item.children = processProperties(prop.items.properties, prop.items.required || []);
            }

            return item;
        });

    // 开始转换的入口点
    return jsonSchema && jsonSchema.properties
        ? processProperties(jsonSchema.properties, jsonSchema.required || [])
        : [];
},


// 将JSONSchema形式数据处理成普通数组结构----转换成对象(这种是正常JSONscheme形式)
convertFromJSONSchema(jsonSchema) {
    // 递归函数来处理每个属性和它的子属性
    const processProperties = (properties, required = []) =>
        Object.entries(properties).map(([key, prop]) => {
            const item = {
                paramsName: key,
                paramsDesc: prop.description || '',
                paramsType: prop.type,
                paramsRequired: required.includes(key)
            };

            if (prop.type === 'object' && prop.properties) {
                item.children = processProperties(prop.properties, prop.required || []);
            } else if (prop.type === 'array' && prop.items) {
                // 添加一个中间对象来表示数组的每个元素
                const arrayItem = {
                    paramsName: "",
                    paramsDesc: prop.items.description,
                    paramsType: prop.items.type,
                    paramsRequired: true, // 数组的子项通常是必需的
                    children: []
                };

                if (prop.items.type === 'object' && prop.items.properties) {
                    arrayItem.children = processProperties(prop.items.properties, prop.items.required || []);
                }

                item.children = [arrayItem];
            }

            return item;
        });

    // 开始转换的入口点
    return jsonSchema && jsonSchema.properties
        ? processProperties(jsonSchema.properties, jsonSchema.required || [])
        : [];
},



// 将数据处理成JSONSchema形式
 convertToJSONSchema(data) {
    const convertParamsToSchema = ({ paramsType, paramsDesc, paramsRequired, children }) => {
        const paramSchema = {
            type: paramsType,
            description: paramsDesc,
        };

        if (paramsType === "object") {
            const properties = {};
            const required = [];

            children.forEach(child => {
                properties[child.paramsName] = convertParamsToSchema(child);
                if (child.paramsRequired) {
                    required.push(child.paramsName);
                }
            });

            paramSchema.properties = properties;
            if (required.length) {
                paramSchema.required = required;
            }
        } else if (paramsType === "array" && children && children.length) {
            // Assuming only one child for array type which is the object structure of items
            const childSchema = convertParamsToSchema(children[0]);
            paramSchema.items = childSchema;
        }

        return paramSchema;
    };

    const properties = {};
    const required = [];

    data.forEach(item => {
        properties[item.paramsName] = convertParamsToSchema(item);
        if (item.paramsRequired) {
            required.push(item.paramsName);
        }
    });

    const schema = {
        type: "object",
        properties,
        ...(required.length && { required })
    };

    return schema;
}     

你可能感兴趣的:(前端,javascript)