SPB Git

Repository Public MIT

RegistryValidator.swift 6.1 KB 112 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Sources/SPGlassRegistry/RegistryValidator.swift8// Module           : SPGlassRegistry9// Repository       : https://git.spboucher.ai/sp-glasskit10// Project Website  : https://glass.spboucher.ai11// Created          : 2026-12-0212// Last Modified    : 2026-12-0213//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation4243/// Structural rules for manifests and indexes. Everything read from a registry passes through here before use.44public enum RegistryValidator {45    public static let maximumFilesPerItem = 9646    public static let maximumDependencies = 3247    public static let maximumItems = 51248    public static let knownKinds: Set<String> = ["native-wrapper", "custom-glass", "content-component", "primitive"]49    public static let knownLicenses: Set<String> = ["MIT"]5051    public static func validate(_ manifest: ComponentManifest) throws(RegistryError) {52        func fail(_ detail: String) throws(RegistryError) -> Never { throw .invalidManifest(id: manifest.id, detail: detail) }53        guard manifest.schemaVersion == RegistrySchema.version else {54            throw .unsupportedSchema(what: "Manifest \"\(RegistryError.clip(manifest.id))\"", version: manifest.schemaVersion)55        }56        try RegistryPath.validateIdentifier(manifest.id)57        _ = try SemanticVersion(manifest.version)58        if manifest.name.isEmpty || manifest.name.count > 80 { try fail("the name must have 1 to 80 characters.") }59        if manifest.description.count > 600 { try fail("the description exceeds 600 characters.") }60        if !knownKinds.contains(manifest.kind) { try fail("unknown kind \"\(RegistryError.clip(manifest.kind))\".") }61        if !knownLicenses.contains(manifest.license) { try fail("unexpected licence \"\(RegistryError.clip(manifest.license))\".") }62        if manifest.files.isEmpty { try fail("it lists no file.") }63        if manifest.files.count > maximumFilesPerItem { try fail("it lists more than \(maximumFilesPerItem) files.") }64        if manifest.dependencies.count > maximumDependencies { try fail("it lists more than \(maximumDependencies) dependencies.") }65        try RegistryPath.validateSet(manifest.files.map(\.path))66        for file in manifest.files {67            if !RegistryHash.isSHA256(file.sha256) { try fail("the checksum of \"\(RegistryError.clip(file.path))\" is not a SHA-256.") }68            if file.bytes < 0 || file.bytes > RegistryJSON.maximumFileBytes { try fail("the size of \"\(RegistryError.clip(file.path))\" is out of range.") }69        }70        if manifest.sha256 != RegistryHash.contentDigest(of: manifest.files) { try fail("the content digest does not match the file list.") }71        var seen: Set<String> = []72        for dependency in manifest.dependencies {73            try RegistryPath.validateIdentifier(dependency.id)74            _ = try SemanticVersion(dependency.version)75            if dependency.id == manifest.id { throw .dependencyCycle([manifest.id, manifest.id]) }76            if !seen.insert(dependency.id).inserted { try fail("dependency \"\(dependency.id)\" is listed twice.") }77        }78        if manifest.symbols.count > 256 || manifest.examples.count > 16 || manifest.accessibility.count > 32 || manifest.changelog.count > 256 {79            try fail("a descriptive list is too long.")80        }81    }8283    public static func validate(_ index: RegistryIndex) throws(RegistryError) {84        guard index.schemaVersion == RegistrySchema.version else {85            throw .unsupportedSchema(what: "The registry index", version: index.schemaVersion)86        }87        guard index.items.count <= maximumItems, index.themes.count <= maximumItems else {88            throw .malformed(what: "The registry index", detail: "it lists too many entries.")89        }90        for (label, entries) in [("item", index.items), ("theme", index.themes)] {91            var seen: Set<String> = []92            for entry in entries {93                try RegistryPath.validateIdentifier(entry.id)94                guard seen.insert(entry.id).inserted else {95                    throw .malformed(what: "The registry index", detail: "\(label) \"\(entry.id)\" is listed twice.")96                }97                guard !entry.versions.isEmpty, entry.version(entry.latest) != nil else {98                    throw .malformed(what: "The registry index", detail: "\(label) \"\(entry.id)\" has no entry for its latest version.")99                }100                var versions: Set<String> = []101                for version in entry.versions {102                    _ = try SemanticVersion(version.version)103                    try RegistryPath.validate(version.path)104                    guard RegistryHash.isSHA256(version.sha256), versions.insert(version.version).inserted else {105                        throw .malformed(what: "The registry index", detail: "\(label) \"\(entry.id)\" has an invalid version entry.")106                    }107                }108            }109        }110    }111}112
Files main
Menu

Theme