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

required=False not working as expected for string fields #109

Closed
kadeeraziz opened this issue Jun 15, 2023 · 2 comments
Closed

required=False not working as expected for string fields #109

kadeeraziz opened this issue Jun 15, 2023 · 2 comments

Comments

@kadeeraziz
Copy link

kadeeraziz commented Jun 15, 2023

Description:

Expected behavior:
The request should be processed successfully, even if the string field is left empty, since required=False is specified for the field.

Actual behavior:
The request fails, and a validation error is returned, indicating that the string field is required, despite setting required=False.

Additional information:
I've tested this issue on django_jsonform version 2.17.1, using Python 3.8.10 and Django 3.2.18

Relevant code snippets:

schema = {
        "type": "dict",
        "keys": {
            "required_field": {
                   "type": "string", 
                   "required": False
            },
            "non_required_field": {
                  "type": "string"
             }
        },
    }

value = {"required_field": "Hello, world!"}

validator = JSONSchemaValidator(schema=schema)
validator(value)

This throws an exception, saying: These fields are missing from the data: non_required_field is required.

It's possible that the issue lies within the validation process or the way the JSON schema is handled.

Please let me know if there's any additional information required or if you have further questions.

@bhch
Copy link
Owner

bhch commented Jun 15, 2023

Yes, this is kind of a "known" issue at this point.

As per the JSON Schema spec, all properties are optional by default. But I've chosen not to follow that rule and instead stick to how things work in Django.

E.g., even if a field is not required, such as — name = models.CharField(required=False) — Django will still insert an empty string in the database.

So, we interpret the required keyword as to whether this field (key) can be empty or not.

But we still require that the submitted data must contain all the keys which are declared in the schema.

So, the built-in JSONSchemaValidator will consider this valid: {"required_field": "", "non_required_field": ""}

@bhch
Copy link
Owner

bhch commented Jul 8, 2023

I'm closing this issue because of lack of activity. And also because the existing behaviour will most likely not change.

If you have more arguments to make or questions to ask, feel free to reopen the issue.

@bhch bhch closed this as completed Jul 8, 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