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

Add ability to constrain elements to have distinct nested values. #129

Open
popematt opened this issue Jan 30, 2024 · 0 comments
Open

Add ability to constrain elements to have distinct nested values. #129

popematt opened this issue Jan 30, 2024 · 0 comments
Labels
enhancement New feature or enhancement for the Ion Schema _specification_ requires new version Something that should be considered for next version of the Ion Schema Specification

Comments

@popematt
Copy link
Contributor

Problem

Sometimes a list of elements need to be distinct by a specific nested value. For example, we might want to ensure that list of customers have different customer IDs.

[
  { customer_id: 101, name: "Alice" },
  { customer_id: 102, name: "Bob" },
  { customer_id: 103, name: "Bob" },
  { customer_id: 104, name: "Cassidy" },
  { customer_id: 104, name: "Dave" },
]

Using the ISL 2.0 element: distinct::customer, the best we can do is ensure that no two customers have the ID and the same name.

Proposal

This is an admittedly incomplete straw-man proposal, intended to start further discussion about the topic.

type::{
  name: passenger_list,
  type: list,
  element: passenger,
  // Each passenger on the flight must have a distinct rewards account.
  // We specify the location using a "path" syntax where an empty sexp indicates the (relative) root of the child element
  // and each symbol indicates a field name that should be traversed. An integer value indicates indexing into a list or sexp.
  element_distinct_by: (rewards_account id)
}

type::{
  name: passenger,
  fields: {
    rewards_account: {
      fields: { 
        id: int
      }
    },
    name: string,
  }
}

Sharp edges

This is not necessarily a complete list.

  • What if we want elements to be distinct by field "foo", but the struct does not have a field "foo"?
  • What if we want elements to be distinct by field "foo", but the struct has more than one field "foo"?
  • What if we want elements to be distinct by field "foo", but one of the container elements is not a struct (and therefore does not have field names)?
  • What if our list contains elements that are a union of two types, and we want the elements to all be distinct, but the field in question is foo for variant 1, but field bar in variant 2?
@popematt popematt added enhancement New feature or enhancement for the Ion Schema _specification_ requires new version Something that should be considered for next version of the Ion Schema Specification labels Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or enhancement for the Ion Schema _specification_ requires new version Something that should be considered for next version of the Ion Schema Specification
Projects
None yet
Development

No branches or pull requests

1 participant