Comments (10)
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.
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.
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.
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.
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.
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.
I use additional key for each of them:
'name': {'type': 'string', 'widget': 'hidden', 'default': 'a'}
from django-jsonform.
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.
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.
@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)
- Custom validation of object properties not showing error next to field HOT 2
- Form dynamics not working after reloading django-jsonform with HTMX HOT 3
- Using an object with empty Properties, having all properties inside AdditionalProperties HOT 3
- Setting JSONField schema dynamically HOT 3
- Copy-pasting / Duplications for Array items and Objects
- Horizontal display of properties HOT 1
- Dynamic header generation for array items HOT 2
- Customising "Add key" and "Add item" texts HOT 1
- Add button to open link in URLField HOT 5
- Decimal values not allowed when entering integer minimum and maximum for 'number' types HOT 4
- Define defaults for an array HOT 3
- Subfields in ArrayField should get the relevant input type
- Issue with errors for fields inside an anyOf schema not displaying correctly HOT 1
- validators for jsonfield are not updated with dynamic schema HOT 3
- Cooperation with other repository `django-pydantic-field` for model schema validation HOT 1
- Title is not populated for `oneOf` when referenced through `$ref` HOT 3
- tests dir in .venv broke project tests HOT 1
- JSONFieldWidget attrs HOT 1
- Bug: Nullable Subschema (anyOf with `null`) causes UI exception
- Type annotations / stubs
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-jsonform.