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

Why can't I store boolean values? #344

Open
roc opened this issue Sep 16, 2022 · 4 comments
Open

Why can't I store boolean values? #344

roc opened this issue Sep 16, 2022 · 4 comments

Comments

@roc
Copy link
Contributor

roc commented Sep 16, 2022

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:

Marvell-Consulting@e7d050a#diff-eb6d8b864cdb6ac8796aa9745b925510d8f56c8d9b5b0ef29fa2a9965948b172

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

@wardi
Copy link
Contributor

wardi commented Sep 20, 2022

try adding an output_validators: boolean_validator?

@roc
Copy link
Contributor Author

roc commented Oct 6, 2022

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
Copy link
Contributor

wardi commented Oct 6, 2022

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 added a commit to Marvell-Consulting/ckanext-scheming that referenced this issue Oct 6, 2022
- stop using `select_boolean` to hack the data back to being a boolean
  from a string
- instead use output_validators in order to transform the data from a
  string back to a boolean
See ckan#344 for more
roc added a commit to Marvell-Consulting/ckanext-scheming that referenced this issue Oct 25, 2022
- stop using `select_boolean` to hack the data back to being a boolean
  from a string
- instead use output_validators in order to transform the data from a
  string back to a boolean
See ckan#344 for more
@roc
Copy link
Contributor Author

roc commented Oct 31, 2022

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

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