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

JSON Array - Unable to remove all items #33

Closed
bretrouse opened this issue Dec 20, 2012 · 6 comments
Closed

JSON Array - Unable to remove all items #33

bretrouse opened this issue Dec 20, 2012 · 6 comments

Comments

@bretrouse
Copy link

Given the following schema:

        "parameters": {
            "title": "Parameters",
            "description": "Command Line Parameters",
            "type": "array",
            "required": false,
            "minItems": 0,
            "items": {
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "name": {
                        "title": "Name",
                        "type": "string",
                        "required": true
                    },
                    "value": {
                        "title": "Value",
                        "type": "any",
                        "required": true
                    }
                }
            }
        }

Result: The subsequent form is unable to save a parameters array with 0 items. It always displays a minimum of one item who's name is required preventing the form from being saved.

Expected: user should be able to remove all items from an array with minItems set to 0, or with no minItems set. This would save out an array [] when creating the JSON.

@tidoust
Copy link
Contributor

tidoust commented Dec 27, 2012

Hi @bretrouse,

Thanks for the report. If you replace any with string you should be able to submit the form (JSON Form would actually not set the parameters property in the returned object in that case).

For arrays, JSON Form always displays at least one item in the form. Upon submission, it takes for granted that a field left empty should be discarded from the result. To determine whether a field should be considered empty, JSON Form looks at the field's type, e.g. a string field is empty if the form submission returns an empty string for that field.

The problem with any is that JSON Form cannot assume much about that field, and it cannot tell whether an empty string is an empty value in particular. It keeps the empty string as a result, leading to:

{
  "parameters": [
    {
      "value": ""
    }
  ]
}

... which is invalid per the schema.

The code could perhaps assume that it can strip anything that looks like an empty value from the resulting object, i.e. null, "", {}, [].

@bretrouse
Copy link
Author

Either that or allow the user to explicitly remove the empty entry prior to
saving. Is there a need to display one item in the form at all times?

On Thu, Dec 27, 2012 at 4:15 AM, François Daoust
notifications@github.comwrote:

Hi @bretrouse https://github.com/bretrouse,

Thanks for the report. If you replace any with string you should be able
to submit the form (JSON Form would actually not set the parametersproperty in the returned object in that case).

For arrays, JSON Form always displays at least one item in the form. Upon
submission, it takes for granted that a field left empty should be
discarded from the result. To determine whether a field should be
considered empty, JSON Form looks at the field's type, e.g. a stringfield is empty if the form submission returns an empty string for that
field.

The problem with any is that JSON Form cannot assume much about that
field, and it cannot tell whether an empty string is an empty value in
particular. It keeps the empty string as a result, leading to:

{
"parameters": [
{
"value": ""
}
]}

... which is invalid per the schema.

The code could perhaps assume that it can strip anything that looks like
an empty value from the resulting object, i.e. null, "", {}, [].


Reply to this email directly or view it on GitHubhttps://github.com//issues/33#issuecomment-11704031.

@bretrouse
Copy link
Author

I'm seeing the same issues with the following schema:

{
    "id": "VALUE_ARRAYS",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "VALUE_ARRAYS": {
            "type": "array",
            "required": true,
            "items": {
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "namespace": {
                        "type": "string",
                        "required": true,
                        "enum": [
                            "SCOPE_ONE",
                            "SCOPE_TWO",
                            "SCOPE_THREE"
                        ]
                    },
                    "values": {
                        "type": "object",
                        "properties": {
                            "VALUE_ARRAY_ONE": {
                                "type": "array",
                                "required": false,
                                "items": {
                                    "type": "object",
                                    "additionalProperties": false,
                                    "properties": {
                                        "value": {
                                            "type": "integer",
                                            "required": true
                                        },
                                        "start": {
                                            "type": "integer",
                                            "required": false
                                        },
                                        "end": {
                                            "type": "integer",
                                            "required": false
                                        },
                                        "countsForCRC": {
                                            "type": "boolean",
                                            "required": false,
                                            "default": true
                                        }
                                    }
                                }
                            },
                            "VALUE_ARRAY_TWO": {
                                "type": "array",
                                "required": false,
                                "items": {
                                    "type": "object",
                                    "additionalProperties": false,
                                    "properties": {
                                        "value": {
                                            "type": "integer",
                                            "required": true
                                        },
                                        "start": {
                                            "type": "integer",
                                            "required": false
                                        },
                                        "end": {
                                            "type": "integer",
                                            "required": false
                                        },
                                        "countsForCRC": {
                                            "type": "boolean",
                                            "required": false,
                                            "default": true
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

@Smerity
Copy link

Smerity commented Mar 14, 2013

I have the same issue as @bretrouse.

I think we need to allow a user to delete all the array objects (and I'm not sure why it's forced to be one?) or at least have that as an option passed to JSONForm.

There are many cases where this will always break, such as whenever the array object uses an enum. This means the "empty" first object always be considered as filled in by the end user as there's no way to "deselect" the enum.

Other than this slight issue I've hit, JSONForm is a wonderful library. I hope it can be resolved, especially as it seems this was placed in there as a preference rather than for a specific reason. I'll be looking into removing this limitation in my own JSONForm version.

tidoust pushed a commit that referenced this issue Jun 11, 2013
There was no way to suppress the last item of an array. That
restriction was more for visual reasons (it usually does not look
great to only see "+/-" buttons) than for deep technical reasons.

In practice, not being able to remove all items creates artefact,
especially if one of the fields is rendered as a select box: there is
no way for JSONForm to tell whether the user actually selected
something in the select box or did not want to submit anything.
JSONForm always included the selected choice in the select box in the
submitted object, meaning there was no proper way to reset an array.

JSONForm now allows used to remove the last item in an array.

Note that, when an array is first rendered, it is still rendered with
one empty element as that's supposedly clearer than to display only
the "+/-" buttons. We may add an option later on to override that
setting and start with no array items if needed.
@tidoust
Copy link
Contributor

tidoust commented Jun 11, 2013

I just committed a fix for that issue. Users may now remove all items in an array (or a tabarray).
The "remove" button becomes disabled when there are no more items to remove.

@bretrouse I haven't checked your later schema, let me know if the fix is not enough.

Note that JSONForm still renders arrays with at least one item to start with as that feels more natural than displaying only +/- buttons.

@tidoust tidoust closed this as completed Jun 11, 2013
@tidoust tidoust reopened this Jun 11, 2013
tidoust pushed a commit that referenced this issue Jun 11, 2013
JSONForm renders an item for arrays the first time they are rendered
as it is usually better from a user experience perspective. However,
if the form has already been submitted and if previous values do not
contain any item for a given array, the array must be rendered without
items. JSONForm still rendered an item in that case.
@tidoust
Copy link
Contributor

tidoust commented Jun 11, 2013

As an exception to the rule, JSONForm now does not render an array with an item if the JSONForm structure received contains a value object that represents previously submitted values and that does not have any item for that array.

@tidoust tidoust closed this as completed Jun 11, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants