Git Product home page Git Product logo

Comments (10)

bhch avatar bhch commented on June 20, 2024 1

I will provide support for the const keyword which will solve this issue.

Something like: 'name': {'const': 'a', 'widget': 'hidden'}.

It will be released in the next version.

from django-jsonform.

bhch avatar bhch commented on June 20, 2024

Even though you manually select Option B, the UI generator doesn't generate the form UI directly for the selected option.

What really happens is when you select an option, in the background the widget first updates the underlying form data. Then the UI generator looks at the data and tries to find a match for that data from the available schema options.

Since a and b, both, have the same key called number and of the same type integer, so data like {"number": 1}, will always get matched to the first found option (i.e. Option A).


But what is the purpose of having two subschemas with same keys, types, etc? Aren't a and b effectively describing the same object?

from django-jsonform.

maciejpolanczyk avatar maciejpolanczyk commented on June 20, 2024

Thanks for explanation.

They are similar from dev point of view but they are not the same in the business meaning. The meaning of A is different from the meaning of B. So although they look the same, I need user to be able to choose between A and B.

from django-jsonform.

bhch avatar bhch commented on June 20, 2024

I think the best way to solve cases like this is to have some unique hidden keys to differentiate the schema.

I'll add support for constants values which will allow doing this:

"$defs": {
    "a": {
        "keys": {
+           "option_id": {"const": "a", "widget": "hidden"},
            "number": {"type": "integer", "minimum": 1, "default": 1}
        }
    },
    "b": {
        "keys": {
+           "option_id": {"const": "b", "widget": "hidden"},
            "number": {"type": "integer", "minimum": 1, "default": 1}
        }
    }
}

If have you some other ideas, please let me know.

from django-jsonform.

maciejpolanczyk avatar maciejpolanczyk commented on June 20, 2024

Best would be to make it without any additional fields, for example distinguish base on "a" and "b" keys in $defs.
If this is not possible and the only way is to use option_id please add it to the docs in the way that everyone will easily notice it, to avoid loosing time on debugging the issue which is already solved.

from django-jsonform.

bhch avatar bhch commented on June 20, 2024

for example distinguish base on "a" and "b" keys in $defs.

This won't work because the form data will not have any information about the keys.

This is the data that your schema will result in:

[
    {"number": 1},
    {"number": 2},
]

May I ask how you will know on the server side which item belongs to which option since it has no keys?

from django-jsonform.

maciejpolanczyk avatar maciejpolanczyk commented on June 20, 2024

I use additional key for each of them:
'name': {'type': 'string', 'widget': 'hidden', 'default': 'a'}

from django-jsonform.

bhch avatar bhch commented on June 20, 2024

Support for const keyword has been added in v2.20.0.

I believe Using a constant value will solve this issue. Please upgrade and test it out.

Thank you.

from django-jsonform.

MooseV2 avatar MooseV2 commented on June 20, 2024

Is there any way to resolve the problem when using additionalProperties?

{
  "type": "dict",
  "keys": {},
  "flexfield": {
    "anyOf": [
      {"type": "string", "title": "String"},
      {"type": "number", "title": "Number"},
      {"type": "list", "title": "Number List", "items": {"type": "number"}},
      {"type": "list", "title": "String List", "items": {"type": "string"}},
      {"type": "dict", "title": "Dictionary", "additionalProperties": {"$ref": "#/flexfield"}}
    ]
  },
  "additionalProperties": {
    "$ref": "#/flexfield"
  }
}

I want to have a flexible schema so that I can use the form controls to create, for example:

{
    "list_a": [1, 2, 3, 4, 5],
    "list_b": ["A", "B", "C"]
}

However, this doesn't seem possible since both "Number List" and "String List" are of type list (even though the contained data types are different).

The only workaround I found is to replace "Number List" and "String List" with a single list type with another anyOf inside, but that means I have to select the data type for every list element, and it can accidentally make a list like:

{
   "list_c": [1, "B", 2, "C"]
}

from django-jsonform.

bhch avatar bhch commented on June 20, 2024

@MooseV2 You can set a default value for the string list with minItems to 1.

{
    "type": "list", 
    "title": "String List", 
    "items": {"type": "string"},
    "default": [""],
    "minItems": 1 
}

from django-jsonform.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.