jsonschema指向数据指针的指针

时间:2017-10-25 06:38:09

标签: javascript json object converter jsonschema

我有以下 jsonSchema :(请注意,有些字段称为'属性'及其不是' json-schema peoperties(代表字段)&#39 ;

{
    "type": "object",
    "properties": {
        "name": {
            "type": "string"
        },
        "image": {
            "type": "string",
            "media": {
                "binaryEncoding": "base64",
                "type": "image/jpeg"
            }
        },
        "properties": {
            "type": "string"
        },
        "nameObj": {
            "type": "object",
            "properties": {
                "properties": {
                    "type": "string"
                },
                "firstName": {
                    "title": "First Name",
                    "type": "string",
                    "maxLength": 100
                }
            }
        }
    }
}

数据类似

    {
    "name": "person1",
    "properties": "myProperties",
    "nameObj": {
        "properties": "nameProperties",
        "firstName:": "myPerson"
    }
}

我有字段的注释路径" firstName"在架构下:

properties.nameObj.properties.firstName

我希望将其转换为数据路径,如下所示:

nameObj.firstName

我不能忽视'属性'字段因为(正如您在上面的架构中看到的)'属性'可以是数据字段名称或json-schema属性。

JaveScript的例子很有用。

由于

1 个答案:

答案 0 :(得分:0)

我已经创建了一个转换此路径的函数:

        function schemaPathToEntityPath(schema,path) {
    let fields = path.split('.');
    let dataFields;
    for (let field of fields) {
        if (field === 'properties') {
            //if its json-schema property
            if (schema.hasOwnProperty('type') && schema.type === 'object') {
                //go next level inside schema
                schema = schema[field];
                continue;
            }

        }

        //if field actually exist in schema
        if(schema.hasOwnProperty(field)) {
            //go next level inside schema
            schema = schema[field];
            //for the first time just add field, later add '.' + field
            dataFields = dataFields ? dataFields += '.' + field : field;
        }
    }
    return dataFields;
}

示例:

       let schema={
    "type": "object",
    "properties": {
        "name": {
            "type": "string"
        },
        "image": {
            "type": "string",
            "media": {
                "binaryEncoding": "base64",
                "type": "image/jpeg"
            }
        },
        "properties": {
            "type": "string"
        },
        "nameObj": {
            "type": "object",
            "properties": {
                "properties": {
                    "type": "string"
                },
                "firstName": {
                    "type": "string",
                    "maxLength": 100
                }
            }
        }
    }
}

let schemaPath1='properties.nameObj.properties.firstName'
let schemaPath2='properties.nameObj.properties.properties'

console.log(schemaPathToEntityPath(schema,schemaPath1));
//output= nameObj.firstName

console.log(schemaPathToEntityPath(schema,schemaPath2));
//output= nameObj.properties