-
I have the following example data:
I would like to write a schema that constraints my data, so that the I tried something like this:
But that will give me an error
Does anyone have any suggestions? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You need to dynamically produce an enum from the app names: #Name: "aaa" | "bbb" | "ccc" You can do that with the package main
// --- SCHEMA ---
#App: {
name: string
}
#Apps: [... #App]
// Given a list of Apps, dynamically produce a Name Enum
// #Name: "aaa" | "bbb" | "ccc"
#Name: or([ for a in apps {a.name} ])
// --- DATA ---
apps: #Apps & [
{name: "aaa"},
{name: "bbb"},
{name: "ccc"}
]
names: [...#Name]
names: ["aaa", "ccc"] If you want to move that schema to a separate package and/or produce multiple enums from different lists, you can use the function pattern to make #Name more flexible: package main
// --- SCHEMA ---
#App: {
name: string
}
#Apps: [... #App]
// Given a list of Apps, dynamically produce a Name Enum
// #Name: "aaa" | "bbb" | "ccc"
#Name: {
#apps: #Apps
or([ for a in #apps {a.name} ])
}
// --- DATA ---
apps: #Apps & [
{name: "aaa"},
{name: "bbb"},
{name: "ccc"}
]
names: [...{#Name, #apps: apps}]
names: ["aaa", "ccc"] |
Beta Was this translation helpful? Give feedback.
You need to dynamically produce an enum from the app names:
You can do that with the
or()
function. For example:If you want to move that schema to a separate package and/or produce multiple enums from different lists, you can use the function pattern to make #Name more flexible: