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

Error if the value is null and the schema expects a different thing #121

Closed
alexgmin opened this issue Sep 12, 2023 · 4 comments
Closed

Comments

@alexgmin
Copy link

WIth this schema:

    field = NullableJSONFieldWithSchema(
        default=None,
        blank=True,
        null=True,
        schema={
            'type': 'object',
            'keys': {
                'foo': {
                    'type': 'array',
                    'items': {
                        'type': 'object',
                        'keys': {},
                        'additionalProperties': {
                            'anyOf': [
                                {
                                    'title': 'String',
                                    'type': 'string',
                                },
                            ],
                        },
                    },
                }
            }
        },
    )

If the value of the field in database is {'foo': None}, the following happens:
image

If you update the value only via the admin it will never be null but if you update via outside sources it could be, so maybe there's should be an option to indicate that a certain key is nullable.

@bhch
Copy link
Owner

bhch commented Sep 12, 2023

What do you think if the form should implicitly convert the data from {"foo": null} to {"foo": []} when it renders?

@alexgmin
Copy link
Author

That would work too, but I'm not sure if in more complex schemas or other use cases the nullable property would be better.

@bhch
Copy link
Owner

bhch commented Sep 14, 2023

You mean something like this: {'type': ['array', 'null']}? Related: issue #113.

Only way I can think of to make it work is by allowing the user to select/toggle between the types, similar to how oneOf works. What do you think of this?

@bhch
Copy link
Owner

bhch commented Nov 10, 2023

I've tried to address this in the new release v2.20.0. The form is now a bit more tolerant if it receives null when it expects something else. In that case, it will implicitly replace null with appropriate/expected data type.

I've also tested it with some complex, deeply nested schemas and this seems to work well.

Supporting multiple types required a rewrite of significant portion of the code which I didn't think was worth it since it's a rare use case.

For now, I'll close this issue. But if you should find some cases where it doesn't work, feel free to re-open the issue.

Please upgrade and test it out.

Thank you.

@bhch bhch closed this as completed Nov 10, 2023
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

No branches or pull requests

2 participants