mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
parent
ab03588db9
commit
3f68309148
2 changed files with 62 additions and 18 deletions
|
@ -728,7 +728,7 @@ func (f *frontmatterFieldHandlers) newDateFieldHandler(key string, setter func(d
|
||||||
return func(d *FrontMatterDescriptor) (bool, error) {
|
return func(d *FrontMatterDescriptor) (bool, error) {
|
||||||
v, found := d.PageConfig.Params[key]
|
v, found := d.PageConfig.Params[key]
|
||||||
|
|
||||||
if !found || v == "" {
|
if !found || v == "" || v == nil {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -52,47 +52,91 @@ func TestDateValidation(t *testing.T) {
|
||||||
-- hugo.toml --
|
-- hugo.toml --
|
||||||
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
|
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
|
||||||
-- content/_index.md --
|
-- content/_index.md --
|
||||||
+++
|
FRONT_MATTER
|
||||||
date = DATE
|
|
||||||
+++
|
|
||||||
-- layouts/index.html --
|
-- layouts/index.html --
|
||||||
{{ .Date.UTC.Format "2006-01-02" }}
|
{{ .Date.UTC.Format "2006-01-02" }}
|
||||||
--
|
--
|
||||||
`
|
`
|
||||||
errorMsg := `ERROR the "date" front matter field is not a parsable date`
|
errorMsg := `ERROR the "date" front matter field is not a parsable date`
|
||||||
|
|
||||||
// Valid (TOML)
|
// TOML: unquoted date/time (valid)
|
||||||
f := strings.ReplaceAll(files, "DATE", "2024-10-01")
|
f := strings.ReplaceAll(files, "FRONT_MATTER", `
|
||||||
|
+++
|
||||||
|
date = 2024-10-01
|
||||||
|
+++
|
||||||
|
`)
|
||||||
b := hugolib.Test(t, f)
|
b := hugolib.Test(t, f)
|
||||||
b.AssertFileContent("public/index.html", "2024-10-01")
|
b.AssertFileContent("public/index.html", "2024-10-01")
|
||||||
|
|
||||||
// Valid (string)
|
// TOML: string (valid)
|
||||||
f = strings.ReplaceAll(files, "DATE", `"2024-10-01"`)
|
f = strings.ReplaceAll(files, "FRONT_MATTER", `
|
||||||
|
+++
|
||||||
|
date = "2024-10-01"
|
||||||
|
+++
|
||||||
|
`)
|
||||||
b = hugolib.Test(t, f)
|
b = hugolib.Test(t, f)
|
||||||
b.AssertFileContent("public/index.html", "2024-10-01")
|
b.AssertFileContent("public/index.html", "2024-10-01")
|
||||||
|
|
||||||
// Valid (empty string)
|
// TOML: empty string (valid)
|
||||||
f = strings.ReplaceAll(files, "DATE", `""`)
|
f = strings.ReplaceAll(files, "FRONT_MATTER", `
|
||||||
|
+++
|
||||||
|
date = ""
|
||||||
|
+++
|
||||||
|
`)
|
||||||
b = hugolib.Test(t, f)
|
b = hugolib.Test(t, f)
|
||||||
b.AssertFileContent("public/index.html", "0001-01-01")
|
b.AssertFileContent("public/index.html", "0001-01-01")
|
||||||
|
|
||||||
// Valid (int)
|
// TOML: int (valid)
|
||||||
f = strings.ReplaceAll(files, "DATE", "0")
|
f = strings.ReplaceAll(files, "FRONT_MATTER", `
|
||||||
|
+++
|
||||||
|
date = 0
|
||||||
|
+++
|
||||||
|
`)
|
||||||
b = hugolib.Test(t, f)
|
b = hugolib.Test(t, f)
|
||||||
b.AssertFileContent("public/index.html", "1970-01-01")
|
b.AssertFileContent("public/index.html", "1970-01-01")
|
||||||
|
|
||||||
// Invalid (string)
|
// TOML: string (invalid)
|
||||||
f = strings.ReplaceAll(files, "DATE", `"2024-42-42"`)
|
f = strings.ReplaceAll(files, "FRONT_MATTER", `
|
||||||
|
+++
|
||||||
|
date = "2024-42-42"
|
||||||
|
+++
|
||||||
|
`)
|
||||||
b, _ = hugolib.TestE(t, f)
|
b, _ = hugolib.TestE(t, f)
|
||||||
b.AssertLogContains(errorMsg)
|
b.AssertLogContains(errorMsg)
|
||||||
|
|
||||||
// Invalid (bool)
|
// TOML: bool (invalid)
|
||||||
f = strings.ReplaceAll(files, "DATE", "true")
|
f = strings.ReplaceAll(files, "FRONT_MATTER", `
|
||||||
|
+++
|
||||||
|
date = true
|
||||||
|
+++
|
||||||
|
`)
|
||||||
b, _ = hugolib.TestE(t, f)
|
b, _ = hugolib.TestE(t, f)
|
||||||
b.AssertLogContains(errorMsg)
|
b.AssertLogContains(errorMsg)
|
||||||
|
|
||||||
// Invalid (float)
|
// TOML: float (invalid)
|
||||||
f = strings.ReplaceAll(files, "DATE", "6.7")
|
f = strings.ReplaceAll(files, "FRONT_MATTER", `
|
||||||
|
+++
|
||||||
|
date = 6.7
|
||||||
|
+++
|
||||||
|
`)
|
||||||
b, _ = hugolib.TestE(t, f)
|
b, _ = hugolib.TestE(t, f)
|
||||||
b.AssertLogContains(errorMsg)
|
b.AssertLogContains(errorMsg)
|
||||||
|
|
||||||
|
// JSON: null (valid)
|
||||||
|
f = strings.ReplaceAll(files, "FRONT_MATTER", `
|
||||||
|
{
|
||||||
|
"date": null
|
||||||
|
}
|
||||||
|
`)
|
||||||
|
b = hugolib.Test(t, f)
|
||||||
|
b.AssertFileContent("public/index.html", "0001-01-01")
|
||||||
|
|
||||||
|
// YAML: null (valid)
|
||||||
|
f = strings.ReplaceAll(files, "FRONT_MATTER", `
|
||||||
|
---
|
||||||
|
date:
|
||||||
|
---
|
||||||
|
`)
|
||||||
|
b = hugolib.Test(t, f)
|
||||||
|
b.AssertFileContent("public/index.html", "0001-01-01")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue