View Source Ash.Type.Union (ash v2.7.1)

A union between multiple types, distinguished with a tag or by attempting to validate.

constraints

Constraints

  • :types - The types to be unioned, a map of an identifier for the enum value to its configuration.
    When using tag and tag_value we are referring to a map key that must equal a certain value in order for the value to be considered an instance of that type.
    For example:
    types:  [
      int: [
        type: :integer,
        constraints: [
          max: 10
        ]
      ],
      object: [
        type: MyObjectType,
        tag: :type,
        tag_value: "my_object"
      ],
      other_object: [
        type: MyOtherObjectType,
        tag: :type,
        tag_value: "my_other_object"
      ],
      other_object_without_type: [
        type: MyOtherObjectTypeWithoutType,
        tag: :type,
        tag_value: nil
      ]
    ]  
    IMPORTANT:
    This is stored as a map under the hood. Filters over the data will need to take this into account.
    Additionally, if you are not using a tag, a value will be considered to be of the given type if it successfully casts. This means that, for example, if you try to cast "10" as a union of a string and an integer, it will end up as "10" because it is a string. If you put the integer type ahead of the string type, it will cast first and 10 will be the value.