URI: 
       resources/page: Improve front matter date validation - hugo - [fork] hugo port for 9front
  HTML git clone git@git.drkhsh.at/hugo.git
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
   DIR README
   DIR LICENSE
       ---
   DIR commit ab03588db95c885e30173fb23c3b69d3ed528201
   DIR parent 0450d69fc6ec2e3369d106547b6e49c539081cdc
  HTML Author: Joe Mooring <joe.mooring@veriphor.com>
       Date:   Fri,  4 Oct 2024 18:10:51 -0700
       
       resources/page: Improve front matter date validation
       
       Improve the error message and treat empty strings as zero dates.
       
       Closes #12898
       
       Diffstat:
         M resources/page/pagemeta/page_front… |       4 ++--
         M resources/page/pagemeta/pagemeta_i… |      53 ++++++++++++++++++++++++++++++
       
       2 files changed, 55 insertions(+), 2 deletions(-)
       ---
   DIR diff --git a/resources/page/pagemeta/page_frontmatter.go b/resources/page/pagemeta/page_frontmatter.go
       @@ -728,7 +728,7 @@ func (f *frontmatterFieldHandlers) newDateFieldHandler(key string, setter func(d
                return func(d *FrontMatterDescriptor) (bool, error) {
                        v, found := d.PageConfig.Params[key]
        
       -                if !found {
       +                if !found || v == "" {
                                return false, nil
                        }
        
       @@ -739,7 +739,7 @@ func (f *frontmatterFieldHandlers) newDateFieldHandler(key string, setter func(d
                                var err error
                                date, err = htime.ToTimeInDefaultLocationE(v, d.Location)
                                if err != nil {
       -                                return false, fmt.Errorf("invalid front matter: %s: %s: see %s", key, v, d.PathOrTitle)
       +                                return false, fmt.Errorf("the %q front matter field is not a parsable date: see %s", key, d.PathOrTitle)
                                }
                                d.PageConfig.Params[key] = date
                        }
   DIR diff --git a/resources/page/pagemeta/pagemeta_integration_test.go b/resources/page/pagemeta/pagemeta_integration_test.go
       @@ -14,6 +14,7 @@
        package pagemeta_test
        
        import (
       +        "strings"
                "testing"
        
                "github.com/gohugoio/hugo/hugolib"
       @@ -43,3 +44,55 @@ Lastmod: 2024-03-13 06:00:00 &#43;0000 GMT
        Eq: true
        `)
        }
       +
       +func TestDateValidation(t *testing.T) {
       +        t.Parallel()
       +
       +        files := `
       +-- hugo.toml --
       +disableKinds = ['page','rss','section','sitemap','taxonomy','term']
       +-- content/_index.md --
       ++++
       +date = DATE
       ++++
       +-- layouts/index.html --
       +{{ .Date.UTC.Format "2006-01-02" }}
       +--
       +`
       +        errorMsg := `ERROR the "date" front matter field is not a parsable date`
       +
       +        // Valid (TOML)
       +        f := strings.ReplaceAll(files, "DATE", "2024-10-01")
       +        b := hugolib.Test(t, f)
       +        b.AssertFileContent("public/index.html", "2024-10-01")
       +
       +        // Valid (string)
       +        f = strings.ReplaceAll(files, "DATE", `"2024-10-01"`)
       +        b = hugolib.Test(t, f)
       +        b.AssertFileContent("public/index.html", "2024-10-01")
       +
       +        // Valid (empty string)
       +        f = strings.ReplaceAll(files, "DATE", `""`)
       +        b = hugolib.Test(t, f)
       +        b.AssertFileContent("public/index.html", "0001-01-01")
       +
       +        // Valid (int)
       +        f = strings.ReplaceAll(files, "DATE", "0")
       +        b = hugolib.Test(t, f)
       +        b.AssertFileContent("public/index.html", "1970-01-01")
       +
       +        // Invalid (string)
       +        f = strings.ReplaceAll(files, "DATE", `"2024-42-42"`)
       +        b, _ = hugolib.TestE(t, f)
       +        b.AssertLogContains(errorMsg)
       +
       +        // Invalid (bool)
       +        f = strings.ReplaceAll(files, "DATE", "true")
       +        b, _ = hugolib.TestE(t, f)
       +        b.AssertLogContains(errorMsg)
       +
       +        // Invalid (float)
       +        f = strings.ReplaceAll(files, "DATE", "6.7")
       +        b, _ = hugolib.TestE(t, f)
       +        b.AssertLogContains(errorMsg)
       +}