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

Open
guillett opened this issue May 5, 2020 · 2 comments
Open

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

guillett opened this issue May 5, 2020 · 2 comments

Comments

@guillett
Copy link

@guillett 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 the type: bug label May 5, 2020
@erezrokah erezrokah added this to Triage in Priority Issues via automation May 5, 2020
@erezrokah erezrokah assigned guillett and unassigned guillett May 5, 2020
@guillett
Copy link
Author

@guillett 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

@vrabe 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.