Skip to content

Commit

Permalink
Property validation for content types (#19)
Browse files Browse the repository at this point in the history
- add basic property validation for content types
  • Loading branch information
viaszkadi authored Oct 7, 2024
1 parent ada0fba commit 97f611c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 47 deletions.
48 changes: 1 addition & 47 deletions Sources/ToucanSDK/Source/ContentType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ struct ContentType: Codable {
}

let type: DataType
let required: Bool
}

struct Relation: Codable {
Expand Down Expand Up @@ -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: [
Expand Down
31 changes: 31 additions & 0 deletions Sources/ToucanSDK/Source/PageBundleLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)"
])
}
}
}
}

0 comments on commit 97f611c

Please sign in to comment.