# Dynamic enum

**URL:** <https://jsonforms.discourse.group/t/dynamic-enum/66>\
**Category:** General\
**Created:** [May 14, 2021, 11:33am UTC](https://jsonforms.discourse.group/t/dynamic-enum/66 "2021-05-14T11:33:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![discobot](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/discobot/32/3_2.png) [@discobot](https://jsonforms.discourse.group/u/discobot)\
**Post date:** [May 14, 2021, 11:33am UTC](https://jsonforms.discourse.group/t/dynamic-enum/66/1 "2021-05-14T11:33:13Z")

</div>

Hi all,

I’m using the react flavour of json forms and currently using the material renderers. I’d love to be able to have a drop down in my form where the enumerations are populated using values from my elsewhere in my data.

i’ve seen that ajv has $data reference that can be used in enum that seems to be what I want from a validation perspective however I believe that this $data reference is not supported in the Json4 or Json7 meta schemas that json forms supports.

has anybody done something like this or got any pointers?

apologies if my terminology is a bit off, i’m quite new to all this.

_[[original thread](https://spectrum.chat/jsonforms/general/dynamic-enum~86af381f-aada-4734-8012-ec2b9c225fdf) by James Morris]_

---

<div class="post-metadata">

**Author:** ![discobot](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/discobot/32/3_2.png) [@discobot](https://jsonforms.discourse.group/u/discobot)\
**Post date:** [May 15, 2021, 12:24pm UTC](https://jsonforms.discourse.group/t/dynamic-enum/66/2 "2021-05-15T12:24:58Z")

</div>

_[Benjamin Börngen-Schmidt]_

Hey James,

in the i18n example [jsonforms/util.tsx at master · eclipsesource/jsonforms · GitHub](https://github.com/eclipsesource/jsonforms/blob/master/packages/example/src/util.tsx#L96) the values of the labels are overwritten. I guess you could do the same with the enum values, before you pass the schema to JsonForms.

---

<div class="post-metadata">

**Author:** ![sdirix](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/sdirix/32/1073_2.png) [@sdirix](https://jsonforms.discourse.group/u/sdirix)\
**Post date:** [May 17, 2021, 8:06am UTC](https://jsonforms.discourse.group/t/dynamic-enum/66/3 "2021-05-17T08:06:41Z")

</div>

Hi @james-morris(james-morris), you’re right, I don’t think there is any way to express this with JSON Schema. We support the `$data` reference in JSON Forms via Ajv: when it’s only used for validation then it will also work for JSON Forms when you hand in a properly configured Ajv.

At the moment you can handle them via a custom renderer or exchanging the JSON Schema whenever you calculate a new set of enums. I elaborated on this in [this question](https://spectrum.chat/jsonforms/general/need-to-fetch-enum-of-select-based-on-the-value-in-other-field~60975cfe-ecfd-4ddd-89b3-73eef782adf3). Please let me know if you have further questions.

---

<div class="post-metadata">

**Author:** ![discobot](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/discobot/32/3_2.png) [@discobot](https://jsonforms.discourse.group/u/discobot)\
**Post date:** [May 26, 2021, 9:12am UTC](https://jsonforms.discourse.group/t/dynamic-enum/66/4 "2021-05-26T09:12:19Z")

</div>

_[James Morris]_

Hi @sdirix(sdirix) thanks for all your help so far!

I think i’ve managed to get the enum: {$data: …} propertly configured in my schema and have set $data = true in my ajv however when I try to use the schema in JsonForms I get the following error:  
…  
‘Type ‘{ type: string; enum: { $data: string; }; }’ is not assignable to type ‘JsonSchema7’. Types of property ‘enum’ are incompatible.’  
…

Is this something I can fix or am I not going in the direction you initially suggested?

---

<div class="post-metadata">

**Author:** ![sdirix](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/sdirix/32/1073_2.png) [@sdirix](https://jsonforms.discourse.group/u/sdirix)\
**Post date:** [May 26, 2021, 10:47am UTC](https://jsonforms.discourse.group/t/dynamic-enum/66/5 "2021-05-26T10:47:04Z")

</div>

This type error can be “fixed” by casting your schema to `any`. However you might be running into issues. As you use the `$data` not only for validation but to also as a part of the schema structure we check for our renderers. For example the enum renderer will try to look into the `enum` array, but will find an object containing `$data` instead. I could imagine that the enum renderer will therefore not work.

---

<div class="post-metadata">

**Author:** ![discobot](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/discobot/32/3_2.png) [@discobot](https://jsonforms.discourse.group/u/discobot)\
**Post date:** [May 28, 2021, 1:06pm UTC](https://jsonforms.discourse.group/t/dynamic-enum/66/6 "2021-05-28T13:06:20Z")

</div>

_[James Morris]_

Can I get round that by adding a custom renderer that is used in place of the traditional enum renderer in the case where the enum isn’t an array, where enum: {$data: …}?

---

<div class="post-metadata">

**Author:** ![sdirix](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/sdirix/32/1073_2.png) [@sdirix](https://jsonforms.discourse.group/u/sdirix)\
**Post date:** [May 31, 2021, 9:51am UTC](https://jsonforms.discourse.group/t/dynamic-enum/66/7 "2021-05-31T09:51:22Z")

</div>

Yes definitely. Using a custom renderer you can cover any structure you want. In theory the schema doesn’t even need to be a JSON Schema then. The only real constraint from JSON Forms core is that AJV can handle your schema (or you disable validation completely). In your case that should be of no concern.
