From 97f611c6417aa348b17023910e2c552385448839 Mon Sep 17 00:00:00 2001 From: Ferenc Viasz-Kadi Date: Mon, 7 Oct 2024 18:33:25 +0200 Subject: [PATCH] Property validation for content types (#19) - add basic property validation for content types --- Sources/ToucanSDK/Source/ContentType.swift | 48 +------------------ .../ToucanSDK/Source/PageBundleLoader.swift | 31 ++++++++++++ 2 files changed, 32 insertions(+), 47 deletions(-) diff --git a/Sources/ToucanSDK/Source/ContentType.swift b/Sources/ToucanSDK/Source/ContentType.swift index f018cb57..fe657543 100644 --- a/Sources/ToucanSDK/Source/ContentType.swift +++ b/Sources/ToucanSDK/Source/ContentType.swift @@ -38,7 +38,6 @@ struct ContentType: Codable { } let type: DataType - let required: Bool } struct Relation: Codable { @@ -112,52 +111,7 @@ extension ContentType { location: nil, template: "pages.single.page", pagination: nil, - properties: [ - : - // "type": .init( - // type: .string, - // required: false - // ), - // "slug": .init( - // type: .string, - // required: false - // ), - // "title": .init( - // type: .string, - // required: false - // ), - // "description": .init( - // type: .string, - // required: false - // ), - // "image": .init( - // type: .string, - // required: false - // ), - // "draft": .init( - // type: .bool, - // required: false - // ), - // "publication": .init( - // type: .date, - // required: false - // ), - // "expiration": .init( - // type: .date, - // required: false - // ), - //case template - //case output - //case assets - //case redirects - // - //case noindex - //case canonical - //case hreflang - //case css - //case js - - ], + properties: [:], relations: nil, context: .init( site: [ diff --git a/Sources/ToucanSDK/Source/PageBundleLoader.swift b/Sources/ToucanSDK/Source/PageBundleLoader.swift index cbc907a5..a88cb99f 100644 --- a/Sources/ToucanSDK/Source/PageBundleLoader.swift +++ b/Sources/ToucanSDK/Source/PageBundleLoader.swift @@ -425,6 +425,8 @@ public struct PageBundleLoader { return nil } + validateFrontMatter(frontMatter, for: contentType, at: slug) + let title = title(frontMatter: frontMatter) let description = description(frontMatter: frontMatter) let image = image(frontMatter: frontMatter) @@ -520,3 +522,32 @@ public struct PageBundleLoader { } } } + +extension PageBundleLoader { + + func validateFrontMatter( + _ frontMatter: [String: Any], + for contentType: ContentType, + at slug: String + ) { + // properties + for property in contentType.properties ?? [:] { + if frontMatter[property.key] == nil { + logger.warning("Missing content type property", metadata: [ + "content": "\(slug)", + "property": "\(property.key)" + ]) + } + } + + // relations + for relation in contentType.relations ?? [:] { + if frontMatter[relation.key] == nil { + logger.warning("Missing content type relation", metadata: [ + "content": "\(slug)", + "relation": "\(relation.key)" + ]) + } + } + } +}