vaticle / typedb

TypeDB: the polymorphic database powered by types
https://typedb.com
Mozilla Public License 2.0
3.72k stars 337 forks source link

TypeDB 3.0: enumerated value restrictions #7023

Open flyingsilverfin opened 3 months ago

flyingsilverfin commented 3 months ago

Problem to Solve

TypeQL doesn't have a way to represent enums, or even values restricted to a set of values for an attribute, a very common use case (example: a form's title may be Mr|Ms...).

Current Workaround

Users try to use a regex: @regex("Mr|Ms"), which works but is non-canonical and not particularly performant to insert.

Proposed Solution

We will introduce a value restriction such as @values(...) which takes a list of values. This allows restricting values to a set of values. (note: syntax not finalised?)

define
  title sub attribute, value string @values("Mr", "Ms"...);

This also allows using specific longs or dates very naturally:

define
  teenage-years sub attribute, value long @values(11,12,13,14,15...);

Although the use case for these may be somewhat unusual.

brettforbes commented 2 months ago

This is brilliant, but can we slightly expand the use case please?

Controlled dictionaries using Vocabularies and Enums are very common in many fields, including cybersecurity, and super valuable. The key difference between a vocab or an enum is whether one can add more values or not, viz:

Controlled vocabularies are most often displayed in User Interfaces in drop down list boxes (Enum), or combo list boxes (Vocab) that enables extra values to be added.

The key differences between your current proposition and more formal Controlled Vocabulary Types is:

  1. It is not a list of string, instead it is a key-value dictionary
  2. The key is the term being used; the value is its formal definition. Mostly only the key is required in a certain field, but sometimes people want to refer to the definition (e.g. mouseover tooltip description).
  3. The dictionary has a formal name, that acts as its binding property, account-type-ov or windows-registry-datatype-enum in the above examples. One would query using this name.

This goes beyond the simple enum, but perhaps this functionality could be put together by combining an enum and a struct.

Assuming this can be done, then the critical issue is that we need a second type of Enum, one that can be added to dynamically