All files / app/codeCharta/util fileValidator.ts

98.94% Statements 94/95
97.95% Branches 48/49
100% Functions 17/17
98.93% Lines 93/94

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223  105x 105x   105x 105x   105x                         105x                             105x 42x 42x     42x 1x   42x     105x 65x 65x   2x 2x   5x 5x   1x 1x   65x 57x   65x       57x 57x 57x 57x 57x   57x 3x 55x 1x   54x     57x       63x 63x 63x 63x 63x       58x 58x       42x 42x     105x 253x             3x 3x 3x       54x 54x 54x 54x 54x       425x 263x     162x 373x 373x 2x   371x 371x                         300x 365x 312x   53x 53x 2x 2x     51x 3x     51x 290x           5x               300x 300x 300x 300x   300x   300x 1x     300x 3x     300x 5x     300x 375x 246x     300x       31x       234x                 915x       51x    
import { CodeMapNode, FixedPosition } from "../codeCharta.model"
import Ajv from "ajv"
import packageJson from "../../../package.json"
import { ExportCCFile } from "../codeCharta.api.model"
import jsonSchema from "./generatedSchema.json"
import { isLeaf } from "./codeMapHelper"
 
const latestApiVersion = packageJson.codecharta.apiVersion
 
interface ApiVersion {
    major: number
    minor: number
}
 
export interface CCFileValidationResult {
    fileName: string
    errors: string[]
    warnings: string[]
}
 
export const ERROR_MESSAGES = {
    fileIsInvalid: "File is empty or invalid.",
    apiVersionIsInvalid: "API Version is empty or invalid.",
    majorApiVersionIsOutdated: "API Version Outdated: Update CodeCharta API Version to match cc.json.",
    minorApiVersionOutdated: "Minor API Version Outdated.",
    nodesNotUnique: "Node names in combination with node types are not unique.",
    nodesEmpty: "The nodes array is empty. At least one node is required.",
    notAllFoldersAreFixed: "If at least one direct sub-folder of root is marked as fixed, all direct sub-folders of root must be fixed.",
    fixedFoldersOutOfBounds: "Coordinates of fixed folders must be within a range of 0 and 100.",
    fixedFoldersOverlapped: "Folders may not overlap.",
    fixedFoldersNotAllowed: "Fixated folders may not be defined in API-Version < 1.2.",
    fileAlreadyExists: "File already exists.",
    blacklistError: "Excluding all buildings is not possible."
}
 
export function checkWarnings(file: ExportCCFile) {
    const warnings: string[] = []
    Iif (!file) {
        return warnings
    }
    if (fileHasHigherMinorVersion(file)) {
        warnings.push(`${ERROR_MESSAGES.minorApiVersionOutdated} Found: ${file.apiVersion}`)
    }
    return warnings
}
 
export function checkErrors(file: ExportCCFile) {
    const errors: string[] = []
    switch (true) {
        case !file:
            errors.push(ERROR_MESSAGES.fileIsInvalid)
            break
        case !isValidApiVersion(file):
            errors.push(ERROR_MESSAGES.apiVersionIsInvalid)
            break
        case fileHasHigherMajorVersion(file):
            errors.push(ERROR_MESSAGES.majorApiVersionIsOutdated)
            break
    }
    if (errors.length === 0) {
        errors.push(...checkJsonSchema(file))
    }
    return errors
}
 
function checkJsonSchema(file: ExportCCFile) {
    const errors: string[] = []
    if (errors.length === 0) {
        const ajv = new Ajv({ allErrors: true })
        const validate = ajv.compile(jsonSchema)
        const valid = validate(file)
 
        if (!valid) {
            errors.push(...validate.errors.map((error: Ajv.ErrorObject) => getValidationMessage(error)))
        } else if (file.nodes.length === 0) {
            errors.push(ERROR_MESSAGES.nodesEmpty)
        } else {
            errors.push(...validateAllNodesAreUnique(file.nodes[0]), ...validateFixedFolders(file))
        }
    }
    return errors
}
 
function isValidApiVersion(file: ExportCCFile) {
    const { apiVersion } = file
    const hasApiVersion = apiVersion !== undefined
    const versionRegExp = /\d+\.\d+/
    const isValidVersion = versionRegExp.test(apiVersion)
    return hasApiVersion && isValidVersion
}
 
function fileHasHigherMajorVersion(file: ExportCCFile) {
    const apiVersion = getAsApiVersion(file.apiVersion)
    return apiVersion.major > getAsApiVersion(latestApiVersion).major
}
 
function fileHasHigherMinorVersion(file: ExportCCFile) {
    const apiVersion = getAsApiVersion(file.apiVersion)
    return apiVersion.minor > getAsApiVersion(latestApiVersion).minor
}
 
export function getAsApiVersion(version: string): ApiVersion {
    return {
        major: Number(version.split(".")[0]),
        minor: Number(version.split(".")[1])
    }
}
 
function getValidationMessage(error: Ajv.ErrorObject) {
    const errorType = error.keyword.charAt(0).toUpperCase() + error.keyword.slice(1)
    const errorParameter = error.dataPath.slice(1)
    return `${errorType} error: ${errorParameter} ${error.message}`
}
 
function validateAllNodesAreUnique(node: CodeMapNode) {
    const errors: string[] = []
    const names = new Set<string>()
    names.add(`${node.name}|${node.type}`)
    validateChildrenAreUniqueRecursive(node, errors, names, `/${node.name}`)
    return errors
}
 
function validateChildrenAreUniqueRecursive(node: CodeMapNode, errors: string[], names: Set<string>, subPath: string) {
    if (isLeaf(node)) {
        return
    }
 
    for (const child of node.children) {
        const path = `${subPath}/${child.name}`
        if (names.has(`${path}|${child.type}`)) {
            errors.push(`${ERROR_MESSAGES.nodesNotUnique} Found duplicate of ${child.type} with path: ${path}`)
        } else {
            names.add(`${path}|${child.type}`)
            validateChildrenAreUniqueRecursive(child, errors, names, path)
        }
    }
}
 
function checkChildNodes(
    childNodes: CodeMapNode[],
    notFixed: string[],
    file: ExportCCFile,
    errors: string[],
    outOfBounds: string[],
    intersections: Set<string>
) {
    for (const node of childNodes) {
        if (node.fixedPosition === undefined) {
            notFixed.push(`${node.name}`)
        } else {
            const apiVersion = getAsApiVersion(file.apiVersion)
            if (apiVersion.major < 1 || (apiVersion.major === 1 && apiVersion.minor < 2)) {
                errors.push(`${ERROR_MESSAGES.fixedFoldersNotAllowed} Found: ${file.apiVersion}`)
                return
            }
 
            if (isOutOfBounds(node)) {
                outOfBounds.push(getFoundFolderMessage(node))
            }
 
            for (const node2 of childNodes) {
                if (
                    node2.fixedPosition !== undefined &&
                    node !== node2 &&
                    rectanglesIntersect(node.fixedPosition, node2.fixedPosition) &&
                    !intersections.has(`${getFoundFolderMessage(node2)} and ${getFoundFolderMessage(node)}`)
                ) {
                    intersections.add(`${getFoundFolderMessage(node)} and ${getFoundFolderMessage(node2)}`)
                }
            }
        }
    }
}
 
function validateFixedFolders(file: ExportCCFile, childNodes: CodeMapNode[] = file.nodes[0].children) {
    const errors: string[] = []
    const notFixed: string[] = []
    const outOfBounds: string[] = []
    const intersections: Set<string> = new Set()
 
    checkChildNodes(childNodes, notFixed, file, errors, outOfBounds, intersections)
 
    if (notFixed.length > 0 && notFixed.length !== childNodes.length) {
        errors.push(`${ERROR_MESSAGES.notAllFoldersAreFixed} Found: ${notFixed.join(", ")}`)
    }
 
    if (outOfBounds.length > 0) {
        errors.push(`${ERROR_MESSAGES.fixedFoldersOutOfBounds} Found: ${outOfBounds.join(", ")}`)
    }
 
    if (intersections.size > 0) {
        errors.push(`${ERROR_MESSAGES.fixedFoldersOverlapped} Found: ${[...intersections].join(", ")}`)
    }
 
    for (const node of childNodes) {
        if (node.children) {
            errors.push(...validateFixedFolders(file, node.children))
        }
    }
    return errors
}
 
function getFoundFolderMessage(node: CodeMapNode) {
    return `${node.name} ${JSON.stringify(node.fixedPosition)}`
}
 
function rectanglesIntersect(rect1: FixedPosition, rect2: FixedPosition) {
    return (
        isInRectangle(rect1.left, rect1.top, rect2) ||
        isInRectangle(rect1.left, rect1.top + rect1.height, rect2) ||
        isInRectangle(rect1.left + rect1.width, rect1.top, rect2) ||
        isInRectangle(rect1.left + rect1.width, rect1.top + rect1.height, rect2)
    )
}
 
function isInRectangle(x: number, y: number, rect: FixedPosition) {
    return x >= rect.left && x <= rect.left + rect.width && y >= rect.top && y <= rect.top + rect.height
}
 
function isOutOfBounds({ fixedPosition: { left, top, width, height } }: CodeMapNode) {
    return left < 0 || top < 0 || left + width > 100 || top + height > 100 || width < 0 || height < 0
}