Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date and datetime persisted values may not conform to specified format #3702

Closed
guillett opened this issue May 5, 2020 · 4 comments · Fixed by #3923
Closed

Date and datetime persisted values may not conform to specified format #3702

guillett opened this issue May 5, 2020 · 4 comments · Fixed by #3923

Comments

@guillett
Copy link

guillett commented May 5, 2020

Hi folks many thanks for your great work.

Describe the bug

Date and datetime can be improperly persisted.

To Reproduce

  • Add the following section to a config file
  - name: test
    label: Tests
    create: true
    folder: "content/_tests"
    fields:
      - name: title
        widget: string
      - name: date
        widget: date
        format: YYYY-MM-DD
        dateFormat: YYYY-MM-DD
        default: ''
      - name: datetime
        widget: datetime
        dateFormat: YYYY-MM-DD
        timeFormat: false
        default: ''
  • Create a new entry with title and dates (pick 2020-05-06 for dates)
  • Persist entry
  • Check persisted data, it is OK (values in the file are 2020-05-06)
  • Refresh admin site
  • Update title only
  • Save
  • Check persisted data (values in the file are now 2020-05-07T00:00:00.000Z)

Applicable Versions:

netlify-cms-app 2.12.12 index.js:25:12
netlify-cms-core 2.26.0 bootstrap.js:83:12
netlify-cms 2.10.48 index.js:27:12

CMS configuration

https://github.com/betagouv/beta.gouv.fr/blob/master/admin/config.yml

https://github.com/betagouv/beta.gouv.fr/

Additional context

In my specific context, I found a hack to prevent that inappropriate behavior.

    var fixDateSerialization = {
      deserialize: function() {
        throw 'Not implemented' // Not used, deserialize is never called
      },
      serialize: function(value) {
        // Hack to ensure only the date part is persisted
        // Force format to YYYY-MM-DD
        return CMS.moment(value).format('YYYY-MM-DD')
      }
    }
    CMS.registerWidgetValueSerializer('date', fixDateSerialization)
@erezrokah erezrokah added type: bug code to address defects in shipped code good first issue area: extensions/widgets labels May 5, 2020
@erezrokah erezrokah assigned guillett and unassigned guillett May 5, 2020
@guillett
Copy link
Author

guillett commented May 5, 2020

I think this is actually a pretty complex issue.

I did not find an widget with the required logic to mimic it nor an abstraction to build on top of it. The hack I found is too outside of the packages to be easily integrated as a long term solution.

Related to

@vrabe
Copy link
Contributor

vrabe commented May 7, 2020

It's because the value is a Date object when the frontmatter is loaded. The initial value of date / datetime widget is not formatted. If you check the preview after you refresh admin site, you'll find the date is like Thu May 07 2020 09:15:33 GMT+0000 (Coordinated Universal Time).

There may be some solutions but I haven't tested:

  1. Make it parsed as a string.
    a. Use yaml 1.2
    b. Wrapped the date with quotes when saving to the file.
  2. Call handleChange(value) even if value !== undefined

@erezrokah
Copy link
Contributor

  1. Make it parsed as a string.
    a. Use yaml 1.2
    b. Wrapped the date with quotes when saving to the file.

Your analysis is correct and thank you for posting the PR.
Your first suggestion is actually a breaking change - see #3567 so going with the second one is good 😄

@grexen
Copy link

grexen commented Sep 8, 2020

I came across the same issue in netlify-cms 2.10.49

Sadly I can't figure out how to fix this without implementing a custom date-widget.

Could someone describe where to call the handleChange? Is it configurable for each field?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants