Skip to content

Dynamic validation #1944

Answered by eadlam
phtj asked this question in General
Discussion options

You must be logged in to vote

You need to dynamically produce an enum from the app names:

#Name: "aaa" | "bbb" | "ccc"

You can do that with the or() function. For example:

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: s…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by phtj
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants