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

Is it possible to allow date strings to have a value of empty string ("") ? #1519

Open
SamSedivy opened this issue Mar 28, 2024 · 4 comments
Assignees
Labels

Comments

@SamSedivy
Copy link

General information

Is it possible to allow date strings to have a value of empty string ("") ? I have date fields that aren't required and their values are empty strings. I don't want to validate them. I want to have date fields that aren't required.

  • json-editor version:
    ^2.14.1

Expected behavior

Allow fields of type: string format: date to have the value ""

Actual behavior

Fields of type: string format: date show this error
Date must be in the format "YYYY-MM-DD".

Steps to reproduce the behavior

Use a startval of "" (empty string) for a field that is type: string format: date

Direct link to example: https://json-editor.github.io/json-editor/

{
  "title": "json schema example",
  "type": "object",
  "properties": {
    "example": {
      "type": "string",
      "format": "date",
      "description": "This is an example schema.",
      "default": ""
    }
  }
}
@SamSedivy
Copy link
Author

Is it possible to remove the validation for fields with format = "date" ?

@schmunk42
Copy link
Collaborator

Sounds tricky...

What's possible is to define string,null for the property, but this does not exactly do what you want.

{
  "title": "json schema example",
  "type": "object",
  "properties": {
    "example": {
      "type": [
        "string",
        "null"
      ],
      "format": "date",
      "description": "This is an example schema.",
      "default": ""
    }
  }
}

Or you could use show_opt_in, but that removes the property completely if not set.

@SamSedivy Do you validate the data on the server also? Is an empty string valid?

@germanbisurgi any ideas? Is it possible to override the validation?

@germanbisurgi
Copy link
Collaborator

germanbisurgi commented Apr 5, 2024

The problem is that json-editor shouldn't use format keyword for validation per default. More on that here.
A couple of solutions that came in mind:

With pattern

{
  "type": "string",
  "description": "Format 'YYYY-MM-DD', or empty.",
  "pattern": "^(|\\d{4}-\\d{2}-\\d{2})$"
}

with oneOf

{
  "type": "string",
  "description": "Format 'YYYY-MM-DD', or empty.",
  "oneOf": [
    {
      "title": "Date Format",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
    },
    {
      "title": "Empty String",
      "pattern": "^$"
    }
  ]
}

@matthiask
Copy link

matthiask commented May 2, 2024

This has bitten me as well.

Previously, this schema has worked fine:

    schema={
        "type": "object",
        "properties": {
            "title": {"type": "string"},
            "issues": {
                "type": "array",
                "format": "table",
                "minItems": 1,
                "items": {
                    "type": "object",
                    "properties": {
                        "number": {
                            "type": "string",
                        },
                        "published_on": {
                            "type": "string",
                            "format": "date",
                        },
                    },
                },
            },
        },
    }

Now it already fails when initializing the editor for a new object. Changing minItems to zero allows the initialization to proceed. The oneOf thing isn't an option really since an empty string isn't valid but there's no way to even enter any data. The exact error is t.errmsg is undefined in the addInputError handler, but I'm not sure if that helps.

EDIT: The big problem is that the initialization fails and the je-not-loaded isn't removed; pointer-events:none makes it impossible to select the fields to start adding valid data.

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

No branches or pull requests

4 participants