Sibyx / django_api_forms

Declarative Django request validation for RESTful APIs

Home Page:https://sibyx.github.io/django_api_forms/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Key validation in DictionaryField

Sibyx opened this issue · comments

I think, it will be very nice to have possibility to define a form field for the dictionary key in DictionaryField.

What would constitute an invalid key?

@harlansgs Hi, I am thinking of possibility to do something like this:

Example request

{
  "my_typed_dict": {
    "b061bb03-1eaa-47d0-948f-3ce1f15bf3bb": 2.718,
    "0a8912f0-6c10-4505-bc27-bbb099d2e395": 42
  }
}

Example form

class AlbumForm(Form):
    my_typed_dict = DictionaryField(key_field=fields.UUIDFIeld(), value_field=fields.DecimalField())

I am still undecided if it makes sense to have such a "feature". What do you think?

I would like to work on this.

@bumblebee211196 feel free to take the issue :) If you any questions don't hesitate to ask.

@bumblebee211196 feel free to take the issue :) If you any questions don't hesitate to ask.

Sure!. Let me first collect a list of field types we can provide for the keys and values in the DictionaryField. We shall discuss on what to include and what not to include.

Hi @bumblebee211196, I think it is not necessary to specify allowed fields for dictionary keys and values. I am aware that not all fields are making sense as keys, but enforcing such limitations will be a needless complication. If we want to keep the codebase as simple and as fast as possible we can't do everything for the programmer (checking his logical errors). If you have a different opinion I am here to listen of course :)

Hey @Sibyx I am currently working on other priority work. I will visit this back once I am done with that.

hi,
i am beginner in python/django..can you help to get a issue to solve at my level?

Hello @Sibyx, is this issue still opened and free to take? Can i take it?

Hello @AmberAnsari89, feel free to take the issue. Basically all you need to know is stated in this issue so read it carefully. If you have any questions feel free to ask. Implementation is suppose to happen in the DictionaryField. Validation is executed in the to_python and you can mark validation error by raising DetailValidationError (if you have path to the key in a dictionary) or ValidationError if you have a generic error. Check existing implementation. Feel free to offer a PR so we can discuss the source code in the more practical way. Let me know if you are going to take the issue.

thank you for your reply.. i ready to take the issue however i am new to open source area. so please help me with guidelines and source code and other details to do my analysis and get better insight.

You need to add new parameters to the DictionaryField constructor (check my example in this issue). Then in the to_python method you have to call validation (to_python methods for attributes from the constructor you created).