gpt4 book ai didi

javascript - 从 JSON Schema 模型派生 JSON 路径列表

转载 作者:行者123 更新时间:2023-12-03 23:14:01 30 4
gpt4 key购买 nike

我正在寻找一个 Javascript 库来列出基于 Json 模式的可能的 Json 路径。

对于像下面这样的 json 模式,我想列出可能的 json 路径。

{
"$id": "https://example.com/person.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Customer",
"type": "object",
"properties": {
"firstName": {
"type": "string",
"description": "The person's first name."
},
"lastName": {
"type": "string",
"description": "The person's last name."
},
"age": {
"description": "Age in years which must be equal to or greater than zero.",
"type": "integer",
"minimum": 0
},
"address": {
"type": "object",
"city": {
"type": "string",
},
"country": {
"type": "string",
}
}
}
}

可能的 Json 路径:firstName、lastName、age、address.city 和 address.country

最佳答案

基于@customcommander 的回答

  • 添加对 $ref 的支持(防止递归)
  • 也添加层次结构父路径(即 a.b.c -> a + a.b + a.b.c)
  • 添加对数组项的支持(使用 [] 作为通用索引器)


  • const _derivePathsFromSchema = (schema, properties, defined) => {
    let paths = [];
    if (!properties) return paths;
    return Object.keys(properties).reduce((paths, childKey) => {
    let child = properties[childKey];
    const { $ref, ...childProperties } = child;
    if ($ref?.startsWith('#/definitions/')) {
    const definition = $ref.substr($ref.lastIndexOf('/') + 1);
    if (!defined.includes(definition)) // prevent recursion of definitions
    {
    defined.push(definition);
    child = {
    ...schema.definitions[definition], // load $ref properties
    ...childProperties, // child properties override those of the $ref
    };
    }
    }
    if (child.type === 'object') {
    return paths.concat(childKey, _derivePathsFromSchema(schema, child.properties, defined.slice()).map(p => `${childKey}.${p}`));
    }
    if (child.type === 'array' && child.items?.properties) {
    return paths.concat(childKey, `${childKey}[]`, _derivePathsFromSchema(schema, child.items.properties, defined.slice()).map(p => `${childKey}[].${p}`));
    }

    return paths.concat(childKey);
    }, paths);
    };
    const derivePathsFromSchema = schema => _derivePathsFromSchema(schema, schema.properties, []);


    console.log(derivePathsFromSchema({
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
    "BType": {
    "type": "object",
    "properties": {
    "a": {
    "$ref": "#/definitions/AType"
    }
    }
    },
    "AType": {
    "type": "object",
    "properties": {
    "b": {
    "$ref": "#/definitions/BType"
    }
    }
    }
    },
    "type": "object",
    "properties": {
    "a": {
    "$ref": "#/definitions/AType"
    },
    "b": {
    "$ref": "#/definitions/BType"
    },
    "id": {
    "type": "string"
    },
    "array": {
    "type": "array",
    "items": {
    "type": "object",
    "properties": {
    "item": { "type": "string" }
    }
    }
    },
    "obj": {
    "type": "object",
    "properties": {
    "nested": { "type": "string" }
    }
    }
    }
    }));

    关于javascript - 从 JSON Schema 模型派生 JSON 路径列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54179784/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com