public async validateRelationshipsByPath()

in source/packages/services/assetlibrary/src/types/types.full.service.ts [462:519]


    public async validateRelationshipsByPath(templateId: string, rels: DirectionStringToArrayMap): Promise<boolean> {
        // example:  in: templateId:edge, in:{}, out:{"manufactured_by":["/suppliers/bosch"]
        logger.debug(`types.full.service validateRelationshipsByPath: in: templateId:${templateId}, rels:${JSON.stringify(rels)}`);

        ow(templateId, 'templateId', ow.string.nonEmpty);

        if (rels === undefined || (rels.in === undefined && rels.out === undefined)) {
            // nothing to validate
            logger.debug(`types.full.service validateRelationshipsByPath: exit: true (nothing)`);
            return true;
        }

        // any ids need to be lowercase
        templateId = templateId.toLowerCase();

        // retrieve the associated group types and allowed relations
        const groupInfo = await this.typesDao.validateRelationshipsByPath(templateId, rels);

        // ensure the provided group paths are valid
        if (groupInfo.invalidGroups !== undefined && groupInfo.invalidGroups.length > 0) {
            logger.debug(`types.full.service validateRelationshipsByPath: exit: false (invalid group paths: ${groupInfo.invalidGroups})`);
            return false;
        }

        // ensure the provided relations are valid
        for (const in_out of Object.keys(rels)) {
            for (const rel_name of Object.keys(rels[in_out])) {

                // is the relation type allowed?
                const allowed_rel = groupInfo.rels.filter(r => r.name === rel_name.toLowerCase());
                if (allowed_rel === undefined || allowed_rel === null || allowed_rel.length === 0) {
                    logger.debug(`types.full.service validateRelationshipsByPath: exit: false (invalid relation: ${rel_name})`);
                    return false;
                }

                for (const rel_path of rels[in_out][rel_name]) {

                    // is the type of target groups allowed for this relation?
                    let group: GroupType;
                    let valid = false;
                    if (in_out === 'in') {
                        group = groupInfo.groupTypes_in.filter(gt => gt.path === rel_path.toLowerCase())[0];
                        valid = allowed_rel.filter(r => r.outType === group.template).length > 0;
                    } else {
                        group = groupInfo.groupTypes_out.filter(gt => gt.path === rel_path.toLowerCase())[0];
                        valid = allowed_rel.filter(r => r.inType === group.template).length > 0;
                    }
                    if (!valid) {
                        logger.debug(`types.full.service validateRelationshipsByPath: exit: false (invalid group ${rel_path} for relation: ${rel_name})`);
                        return false;
                    }
                }
            }
        }

        logger.debug('types.full.service validateRelationshipsByPath: exit: true');
        return true;
    }