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

Enable use of 'map' type from JSONSchema #934

Closed
MatthewJonD opened this issue Apr 19, 2021 · 6 comments · Fixed by #940
Closed

Enable use of 'map' type from JSONSchema #934

MatthewJonD opened this issue Apr 19, 2021 · 6 comments · Fixed by #940
Assignees
Labels
Type: Feature New features and feature requests
Milestone

Comments

@MatthewJonD
Copy link

Loving this library...saving so much time.
Quick question/ask
Getting the error-

Invariant Violation: Unsupported field type: map

Im using a 'map' in my models on a language field that allows data to be stored like -

"summary" : {
        "language" : {
            "en" : "Hello World"
        }
    },

With a schema that looks like this-

  "title": "epochItem",
  "type": "object",
  "properties":
    "name": {
      "type": "string",
      "description": "Human Readable"
    },
    "summary": {
      "title": "summary",
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
        "language": {
          "type": "map",
          "properties": {
            "$*": {
              "type": "string"
            }
          }
        }
      },
      "description": "Complex Description of Summary"
    }
...
(More fields)

Any chance this is on the roadmap to be supported?

@radekmie radekmie self-assigned this Apr 20, 2021
@radekmie radekmie added the Type: Question Questions and other discussions label Apr 20, 2021
@radekmie radekmie added this to Needs triage in Open Source (migrated) via automation Apr 20, 2021
@radekmie radekmie moved this from Needs triage to In progress in Open Source (migrated) Apr 20, 2021
@radekmie
Copy link
Contributor

Hi @MatthewJonD. I've never heard about map type in JSON schema, nor can I find it in the specification. What tool or library are you using for these? In general, it should be an object with patternProperties.

And as for the question: no, there's no out-of-the-box handling for such objects. You'd have to make a field (React component) that would allow users to add their own keys.

@MatthewJonD
Copy link
Author

For some context. Im using Mongoose->mongoose-schema-jsonschema->To generate this schema...
https://www.npmjs.com/package/mongoose-schema-jsonschema

So the mongoose schema looks like -

   let richMetaDataItemSchema = mongoose.Schema({
        _id: false,
        name: { type: String },
        language: {
            type: Map,
            of: { type: String }
        }
    });

It then gets converted to with mongoose-schema-jsonschema that turns that into -

 "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
        "language": {
          "type": "map",
          "properties": {
            "$*": {
              "type": "string"
            }
          }
        }

This lets me quickly build CRUD forms for my Mongoose based apps.

I'll see if there are some options to not use the map field...

And-
Create a component to handle that UI need.

Is there a way to register fields by the schema field type, and let any field that is rendered with the

 <AutoFields />

Just use that custom component?
Right now I am doing-

 <AutoField name={'startDate'} component={DateTimeField} />

Where the {DataTimeField} is my own custom component.

But I have the call each field out separately...
Vs rendering a component based on the type for the schema.

@radekmie
Copy link
Contributor

Yep, I checked the code and it looks like your schema hit this fallback. And thanks for opening DScheglov/mongoose-schema-jsonschema#26! I'll link it here so both sides would benefit.

About registering a component: yes, there is AutoField.componentDetectorContext. In your case it'd look like this:

function componentDetector(props, uniforms) {
    if (props.field.type === 'map') {
      return MapField;
    }

    return AutoField.defaultComponentDetector(props, uniforms);
  }

@MatthewJonD
Copy link
Author

@radekmie Thanks much.
@DScheglov Made changes to https://www.npmjs.com/package/mongoose-schema-jsonschema to support the map type. Output now looks like

 "summary": {
            "title": "summary",
            "type": "object",
            "properties": {
             
                "name": {
                    "type": "string"
                },
                "language": {
                    "type": "object",
                    "additionalProperties": {
                        "type": "string",
                        "__required": false
                    }
                }
            },
            "description": "Complex Description of Summary",
            "label": "Complex Description of Summary",
            "format": "language"
        }

Is now the output, which does have normal JSONSchema types/values.
Now getting the error-

Unhandled Runtime Error
TypeError: Cannot convert undefined or null to object

Call Stack
Function.keys
<anonymous>
JSONSchemaBridge.getSubfields
node_modules/uniforms-bridge-json-schema/es6/JSONSchemaBridge.js (228:0)

I have only added the new field to the <AutoFields /> component, so made sure it was narrowed down to our new type.
Will dig in more, but wanted to report some progress.

@DScheglov
Copy link

DScheglov commented Apr 25, 2021

@MatthewJonD hey,

Oh! I see the problem with unneeded field __required. I've removed it.
Use mongoose-schema-jsonschema@1.4.6

@radekmie
Copy link
Contributor

@MatthewJonD, @DScheglov: I'd say it's a problem in uniforms: we assume that the properties key is always present for object fields (source). We'll fix that.

@radekmie radekmie added Type: Feature New features and feature requests and removed Type: Question Questions and other discussions labels Apr 26, 2021
@radekmie radekmie moved this from In progress to To do in Open Source (migrated) Apr 26, 2021
@radekmie radekmie added this to the v3.4 milestone Apr 26, 2021
@radekmie radekmie linked a pull request Apr 29, 2021 that will close this issue
Open Source (migrated) automation moved this from To do to Closed May 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature New features and feature requests
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants