Skip to content

Adding then removing from a number widget field causes the field to have an empty string as it's value #2848

@TidyIQ

Description

@TidyIQ

Describe the bug

When using widget: "number, required: false, if you:

  1. Working correctly: Leave the field blank and publish the item, the yaml file will not include the field name under the item. For example, if the field name is foo and there is another bar field that is required, the yaml file will be:
item:
  - bar: 7
  1. Working correctly: Enter a number to the field and republish the item, the yaml file will include the field with the correct value. For example:
item
  - bar: 7
  - foo: 3
  1. NOT WORKING CORRECTLY: Complete step 2 above, then go back to the item and delete the number from the field then republish the item, the yaml file will include the field with an empty string as it's value. For example:
item
  - bar: 7
  - foo: ''

This causes issues when using GraphQL as:

  • The schema type cannot be inferred. In situation 1 above, there is no foo field so attempting to query foo will result in an error. This means schema type must be explicitly set.

  • You can't explicitly set the schema type for foo as it can be either String or Int, and GraphQL doesn't support union scalar types.

To Reproduce

Create a field with widget: "number", required: false then run through the three situations above.

Expected behavior

If no number is entered then it should either leave the field out of the yaml file, or define the value as null.

Applicable Versions:

  • Netlify CMS version: "netlify-cms-app": "^2.9.7"
  • Git provider: File system ("gatsby-source-filesystem": "^2.1.33")
  • OS: Windows 10
  • Browser version: Chrome Version 78.0.3904.87 (Official Build) (64-bit)
  • Node.JS version: v10.15.3

CMS configuration

Relevant sections only:

const config = {
  load_config_file: false,
  media_folder: "static/img",
  public_folder: "img",
  collections: [
    {
      label: "Settings",
      name: "settings",
      delete: false,
      editor: {
        preview: false
      },
      files: [
        {
          label: "Typography",
          name: "typography",
          file: `${__dirname}/data/typography/typography.yaml`,
          fields: [
            {
              label: "Titles (H1)",
              name: "h1",
              widget: "object",
              fields: [
                {
                  label: "Font",
                  name: "fontFamily",
                  widget: "string"
                },
                {
                  label: "Size",
                  name: "size",
                  required: false,
                  widget: "number"
                }
              ]
            }    
          ]
        }
      ]
    },
  ]
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions