_findProperties()

in ui-modules/utils/yaml-editor/addon/hint/schema-matcher.js [78:144]


    _findProperties(json, schema, level, currentLevel) {
        // We are at the right level, let's return the resolved properties
        if (currentLevel === level) {
            return this._resolveProperties(schema);
        }

        // Retrieve the last key of the JSON to get the properties from
        let keys = Object.keys(json || {});
        let key = keys[keys.length - 1];

        // If the schema declare a type, then we resolve the properties based on that
        if (schema.hasOwnProperty('type')) {
            let subSchema;
            let subLevel = currentLevel;
            let subJson = json[key] || {};

            switch (schema.type) {
                case TYPE.object:
                    subSchema = schema.properties[key];
                    break;
                case TYPE.array:
                    subSchema = schema.items;
                    break;
            }

            if (!subSchema) {
                return [];
            }

            if (subSchema.type === TYPE.array) {
                subLevel = subLevel - 1;
            }

            if (subSchema.hasOwnProperty('$ref')) {
                return this._findProperties(subJson, Object.assign({}, this._resolveSchema(schema, subSchema.$ref), subSchema), level, subLevel + 1);
            }

            let condition = Object.keys(subSchema).find(key => Object.keys(CONDITION).some(condition => condition === key));
            if (condition) {
                return subSchema[condition].reduce((properties, subSchema) => {
                    let resolvedSubSchema = subSchema;
                    if (subSchema.hasOwnProperty('$ref')) {
                        resolvedSubSchema = Object.assign({}, this._resolveSchema(schema, subSchema.$ref), subSchema);
                    }
                    return properties.concat(this._findProperties(subJson, resolvedSubSchema, level, subLevel + 1));
                }, []);
            }

            return this._findProperties(subJson, Object.assign({definitions: schema.definitions}, subSchema), level, subLevel + 1);
        }

        // If we have any conditions (i.e. anyOf, oneOf, etc) we iterate over the sub-schemas, resolve them
        // if we need to and concat all their properties
        let condition = Object.keys(schema).find(key => Object.keys(CONDITION).some(condition => condition === key));
        if (condition) {
            return schema[condition].reduce((properties, subSchema) => {
                let resolvedSubSchema = subSchema;
                if (subSchema.hasOwnProperty('$ref')) {
                    resolvedSubSchema = Object.assign({}, this._resolveSchema(schema, subSchema.$ref), subSchema);
                }
                return properties.concat(this._findProperties(json, resolvedSubSchema, level, currentLevel));
            }, []);
        }

        // If we arrive here, it means tht the schema does not have anything useful so return an empty array
        return [];
    }