SPB Git

Repository Public License

Validation.swift 4.8 KB 96 lines swift

1import Foundation2import PrismCore34public struct ValidationIssue: Sendable, Hashable, CustomStringConvertible {5    public enum Severity: String, Sendable { case error, warning }6    public var severity: Severity7    public var code: String8    public var objectID: String?9    public var message: String1011    public init(_ severity: Severity, _ code: String, _ objectID: String?, _ message: String) {12        self.severity = severity; self.code = code; self.objectID = objectID; self.message = message13    }1415    public var description: String {16        "\(severity.rawValue) [\(code)]\(objectID.map { " \($0)" } ?? ""): \(message)"17    }1819    public var json: JSONValue {20        var o: [String: JSONValue] = ["severity": .string(severity.rawValue), "code": .string(code), "message": .string(message)]21        if let objectID { o["objectId"] = .string(objectID) }22        return .object(o)23    }24}2526extension Array where Element == ValidationIssue {27    public var errors: [ValidationIssue] { filter { $0.severity == .error } }28    public var warnings: [ValidationIssue] { filter { $0.severity == .warning } }29    public var hasErrors: Bool { contains { $0.severity == .error } }30}3132/// Structure-only checks that need no catalog: IDs, references, cycles, entrypoint, ports.33public enum StructuralValidator {34    public static func validate(_ bp: AppBlueprint) -> [ValidationIssue] {35        var issues: [ValidationIssue] = []36        func err(_ code: String, _ id: String?, _ m: String) { issues.append(.init(.error, code, id, m)) }37        func warn(_ code: String, _ id: String?, _ m: String) { issues.append(.init(.warning, code, id, m)) }3839        if bp.name.trimmingCharacters(in: .whitespaces).isEmpty { err("blueprint.name", nil, "The app needs a name.") }40        if !PrismID.isValid(bp.id) { err("blueprint.id", nil, "`\(bp.id)` is not a valid app id.") }4142        var seen = Set<String>()43        for o in bp.objects {44            if !PrismID.isValid(o.id) { err("object.id", o.id, "`\(o.id)` is not a valid id (letters, digits, `_`, `-`; starts with a letter).") }45            if !seen.insert(o.id).inserted { err("object.duplicate", o.id, "The id `\(o.id)` is used by more than one object.") }46            if o.schemaVersion != PrismObject.currentSchemaVersion {47                err("object.schemaVersion", o.id, "Object format version \(o.schemaVersion) is not supported.")48            }49            for p in o.inputs + o.outputs where !Port.knownTypes.contains(p.type) {50                err("port.type", o.id, "Port `\(p.name)` has unknown type `\(p.type)`.")51            }52            if Set(o.children).count != o.children.count { err("children.duplicate", o.id, "A child is listed twice.") }53        }5455        let ids = Set(bp.objects.map(\.id))56        var childGraph = DependencyGraph(), depGraph = DependencyGraph()57        var parents: [String: String] = [:]58        for o in bp.objects {59            childGraph.addNode(o.id); depGraph.addNode(o.id)60            for c in o.children {61                guard ids.contains(c) else { err("children.missing", o.id, "Child `\(c)` does not exist."); continue }62                childGraph.addEdge(from: o.id, to: c)63                if let other = parents[c], other != o.id {64                    err("children.shared", c, "`\(c)` is a child of both `\(other)` and `\(o.id)`.")65                }66                parents[c] = o.id67                if let child = bp.object(c), !child.isComponent {68                    err("children.kind", o.id, "`\(c)` is a \(child.type) and cannot be placed inside another object.")69                }70            }71            for d in o.dependencies {72                guard ids.contains(d) else { err("dependency.missing", o.id, "Dependency `\(d)` does not exist."); continue }73                depGraph.addEdge(from: o.id, to: d)74            }75            if let ref = o.dataRef, !ids.contains(ref) { err("dataRef.missing", o.id, "Data reference `\(ref)` does not exist.") }76        }77        if let cycle = childGraph.findCycle() { err("children.cycle", cycle.first, "Layout cycle: \(cycle.joined(separator: " → ")).") }78        if let cycle = depGraph.findCycle() { err("dependency.cycle", cycle.first, "Dependency cycle: \(cycle.joined(separator: " → ")).") }7980        if bp.pages.isEmpty { err("pages.none", nil, "The app has no page.") }81        if bp.entrypoint.isEmpty {82            err("entrypoint.missing", nil, "The app has no entry page.")83        } else if bp.object(bp.entrypoint)?.isPage != true {84            err("entrypoint.invalid", nil, "Entry page `\(bp.entrypoint)` is not a page of this app.")85        }8687        if childGraph.findCycle() == nil {88            let reachable = childGraph.reachable(from: bp.pages.map(\.id))89            for o in bp.components where !reachable.contains(o.id) {90                warn("object.orphan", o.id, "`\(o.id)` is not placed on any page.")91            }92        }93        return issues94    }95}96
Files main
Menu

Theme