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

Handle fixed items arrays #132

Merged
merged 17 commits into from
Apr 18, 2016
Merged

Handle fixed items arrays #132

merged 17 commits into from
Apr 18, 2016

Conversation

rhgb
Copy link
Contributor

@rhgb rhgb commented Apr 11, 2016

fixed #76
handle fixed items arrays:

{
  "type": "array",
  "title": "List of fixed items",
  "items": [
    {
      "type": "string",
      "title": "Some text"
    },
    {
      "type": "number",
      "title": "A number"
    }
  ]
}

NOTE: According to the spec, it should be valid that form data has less items in fixed array than the schema defined, but it seems hard to find an appropriate UI for this. I'm glad to implement it if there's any reasonable suggestion.


export function allowAdditionalItems(schema) {
return isObject(schema.additionalItems);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As spec says that "additionalItems" MUST be either a boolean or an object.

We should therefore fallback on the the default items schema if allowAdditionalItems is true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the spec it says:

8.2.3.1. If items is a schema, then the child instance must be valid against this schema, regardless of its index, and regardless of the value of "additionalItems".

Therefore it is only valid when items is an array, and when additionalItems is true it implies any value of additional items is valid. IMO falling back to items or items[0] are not best choices. (Maybe we need a input widget for "any" or "raw"?)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we need a input widget for "any" or "raw"?

Well, that's quite impossible, even if we decide to render a textarea it's unlikely to be able to reflect any JSON data structure, so we're stuck here.

Maybe we should simply raise a warning in such a case, and document this limitation in an appropriate section of the documentation? What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, maybe that's the best way currently. For the detail should we display a message in the error list, or just showing a warning in the console?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the detail should we display a message in the error list, or just showing a warning in the console?

A warning in the console sounds good enough to me. I'd like to keep form error reporting for strict jsonschema validation stuff, not lib limitations.

@n1k0
Copy link
Collaborator

n1k0 commented Apr 11, 2016

This looks great with a few comments to address. Thanks a lot for contributing.

@n1k0
Copy link
Collaborator

n1k0 commented Apr 11, 2016

BTW I landed a fix for #135 so the playground now works again and allows to run your added examples. You should probably rebase this PR on top of master to benefit from it.

@rhgb
Copy link
Contributor Author

rhgb commented Apr 12, 2016

Thanks for the comment: )

@rhgb
Copy link
Contributor Author

rhgb commented Apr 14, 2016

Fixed issues above. For the case additionalItems: true, a warning message will be printed to console, and some explanations are added to README.md

const itemIdPrefix = idSchema.id + "_" + index;
const itemIdSchema = toIdSchema(itemsSchema, itemIdPrefix, definitions);
return this.renderArrayFieldItem({
index, itemSchema: itemsSchema, itemIdSchema, itemErrorSchema,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Each key assignment on its own line please.

@n1k0
Copy link
Collaborator

n1k0 commented Apr 14, 2016

Looks great. Do you think it could be possible to extract rendered JSX from comp methods into their own separate stateless components?

@rhgb
Copy link
Contributor Author

rhgb commented Apr 14, 2016

I'll have a try. But it seems these components will have 7 or 8 props to be passed in. I'm not sure whether it is too many for a component. What's your suggestion?

@n1k0
Copy link
Collaborator

n1k0 commented Apr 14, 2016

But it seems these components will have 7 or 8 props to be passed in

I see what you mean, this could easily turn verbose and messy. Plus component methods are certainly convenient as they have natural immediate access to the prototype.

I tend to agree with the current approach, even if it bloats a little the whole ArrayField component; though that's expected as we're now covering a lot of use cases. We can always refactor later on if we keep adding stuff anyway, so that's definitely not a blocker here.

I'm going to do a second pass over the diff, but I have a feeling we are very close from having our final patch.

@@ -636,6 +636,13 @@ This library partially supports [inline schema definition dereferencing]( http:/

Note that it only supports local definition referencing, we do not plan on fetching foreign schemas over HTTP anytime soon. Basically, you can only reference a definition from the very schema object defining it.

## JSON Schema supporting status
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should run the npm run build:readme so this entry will be added to the TOC.

@n1k0
Copy link
Collaborator

n1k0 commented Apr 14, 2016

this whole approach is vulnerable to carefully picked names

Personally I can live with it, but I wonder if you had a better or more solid parsing strategy in mind?

Also, it seems we should add some unit tests to cover the new cases handled by this last change.

@rhgb
Copy link
Contributor Author

rhgb commented Apr 14, 2016

I'll add some test for that.

In my opinion maybe we should let components maintain their own error status. I'm not very experienced in react so I'd like to hear your suggestion. Also there seems to be a lot of work, maybe we discuss it later in another thread?

@n1k0
Copy link
Collaborator

n1k0 commented Apr 14, 2016

In my opinion maybe we should let components maintain their own error status

My initial idea was to prevent field components to deal with state at all; of course it's not possible for arrays and objects, as they both carry one while the user is "building" them -- which kinda defeats the purpose admittedly. For errors, validating at the field level may get harder are you often need the parent schema eg. for required or unique fields, but that's just plain theory from me.

Feel free to file an issue if you think there's a better way :)

@n1k0
Copy link
Collaborator

n1k0 commented Apr 15, 2016

@rhgb Sorry I had to land #137 and issue a v0.24.0 asap, so your patch is now conflicting. Just tell me if you prefer me to take over the branch if you don't have the time to rebase it on top of latest master, I'll happily do that.

@rhgb
Copy link
Contributor Author

rhgb commented Apr 16, 2016

It's fine, I'll rebase it myself and add tests we've discussed before. Just have too much other work to do yesterday, didn't finish those tests yet.

@rhgb
Copy link
Contributor Author

rhgb commented Apr 18, 2016

I've done rebasing master and modified some test to fit new changes of #139. Tests for error string parsing also added.

@n1k0
Copy link
Collaborator

n1k0 commented Apr 18, 2016

Fantastic work, thanks a lot for your work!

@n1k0 n1k0 merged commit 22962b2 into rjsf-team:master Apr 18, 2016
@rhgb rhgb deleted the array-tuple branch April 19, 2016 04:51
@n1k0 n1k0 mentioned this pull request Apr 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle fixed items arrays
2 participants