ckan / ckanext-scheming

Easy, shareable custom CKAN schemas
Other
83 stars 161 forks source link

Why can't I store boolean values? #344

Open roc opened 2 years ago

roc commented 2 years ago

My ckan_dataset.yaml

  - field_name: mandated
    label: Nationally Mandated
    required: true
    preset: select
    choices:
      - value: false
        label: "No"
      - value: true
        label: "Yes"
    validators: boolean_validator

How it looks in the CKAN form

Capture d’écran 2022-09-16 à 10 54 15

How it gets stored in the API

Capture d’écran 2022-09-16 à 10 51 51

WHY?

What's happening here? Is the boolean validator causing it to be set as a string "true" or "false"?

To get around this I've made a horrible hack to our select preset form and assigned it in the case where we have a ckan-scheming option that sets boolean values:

https://github.com/Marvell-Consulting/ckanext-scheming/commit/e7d050aa8abf82e2a3981ba927009eb22cb18c22#diff-eb6d8b864cdb6ac8796aa9745b925510d8f56c8d9b5b0ef29fa2a9965948b172

There must be a better way! What am I missing?!

wardi commented 2 years ago

try adding an output_validators: boolean_validator?

roc commented 2 years ago

That worked! Why would I need to add this additional step instead of just adding a validator though? This really tripped me up, I completely missed them in the docs

wardi commented 2 years ago

internally all extra dataset fields are stored as strings. output_validators is a validator that is run to convert the internal string representation of a field to something else. This is used to return things like lists of strings or nested objects.

The boolean case is pretty common, if you can think of a good way to add this to the docs the contribution would be very welcome!

roc commented 1 year ago

Finally found the time to add a quick example. I hope #347 helps! I didn't know exactly where to put it in the readme, so it is where it is. Will gladly move/modify as seems appropriate. Thanks