-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Server error during API validation when required custom field is empty #1489
Labels
type: bug
A confirmed report of unexpected behavior in the application
Comments
@arionl : hi, try to modify your custom fields in request like below. i try and it works for me. |
Ah, if I change:
..to:
the PUT request works. However, this custom field isn't marked as required so if it doesn't have a value (null) or I try to set it back to null, I then get the 'int' error:
|
--- a/netbox/extras/api/customfields.py
+++ b/netbox/extras/api/customfields.py
@@ -36,27 +36,37 @@ class CustomFieldsSerializer(serializers.BaseSerializer):
raise ValidationError("Invalid custom field for {} objects: {}".format(content_type, field_name))
# Validate boolean
- if cf.type == CF_TYPE_BOOLEAN and value not in [True, False, 1, 0]:
- raise ValidationError("Invalid value for boolean field {}: {}".format(field_name, value))
+ if cf.type == CF_TYPE_BOOLEAN:
+ if value and value not in [True, False, 1, 0]:
+ raise ValidationError("Invalid value for boolean field {}: {}".format(field_name, value))
+ elif not value and cf.required:
+ raise ValidationError("{} is required field and can't be None, you should pass a bool value.".format(field_nam
# Validate date
if cf.type == CF_TYPE_DATE:
- try:
- datetime.strptime(value, '%Y-%m-%d')
- except ValueError:
- raise ValidationError("Invalid date for field {}: {}. (Required format is YYYY-MM-DD.)".format(
- field_name, value
- ))
+ if value:
+ try:
+ datetime.strptime(value, '%Y-%m-%d')
+ except ValueError:
+ raise ValidationError("Invalid date for field {}: {}. (Required format is YYYY-MM-DD.)".format(
+ field_name, value
+ ))
+ elif not value and cf.required:
+ raise ValidationError("{} is required field and can't be None, you should pass a date.".format(field_name))
# Validate selected choice
if cf.type == CF_TYPE_SELECT:
- try:
- value = int(value)
- except ValueError:
- raise ValidationError("{}: Choice selections must be passed as integers.".format(field_name))
- valid_choices = [c.pk for c in cf.choices.all()]
- if value not in valid_choices:
- raise ValidationError("Invalid choice for field {}: {}".format(field_name, value))
+ if value:
+ try:
+ value = int(value)
+ except ValueError:
+ raise ValidationError("{}: Choice selections must be passed as integers.".format(field_name))
+ valid_choices = [c.pk for c in cf.choices.all()]
+ if value not in valid_choices:
+ raise ValidationError("Invalid choice for field {}: {}".format(field_name, value))
+
+ elif not value and cf.required:
+ raise ValidationError("{} is required field and can't be None, you should pass a integer.".format(field_name))
# Check for missing required fields
missing_fields = [] |
jeremystretch
changed the title
API error when updating objects with "Selection" type custom fields
Server error during API validation when required custom field is empty
Sep 25, 2017
jeremystretch
added
the
type: bug
A confirmed report of unexpected behavior in the application
label
Sep 25, 2017
Merged
lampwins
pushed a commit
to lampwins/netbox
that referenced
this issue
Oct 13, 2017
…empty required custom field
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Issue type
[ ] Feature request
[X] Bug report
[ ] Documentation
Environment
Description
If I try to manipulate an object via the API (a Site in my case) that has a custom field of type "Selection", the PUT request results in an error even if I'm not changing the contents of the custom field.
Example:
Site "1" named "Home" with two custom fields:
Changing something like the ASN field (from 55 to 56) and then attempting to resubmit:
..results in:
I also tried a Site that had both custom fields set to 'null' and same result. If I remove the "Customselection" custom field completely, everything works as intended.
I first stumbled upon this problem because I got an error while trying to use the pynetbox library. Working backwards it seems like the raw PUT request gives an error. I believe this is a NetBox API issue not pynetbox, but I'm still a bit fuzzy and learning. Hope I'm reporting this properly.
The text was updated successfully, but these errors were encountered: