Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

How do I constrain map keys #656

Answered by mpvl
khepin asked this question in Q&A
Discussion options

You must be logged in to vote

Or just

#UUID: =~"^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"
entities:[ID=string]: {
    id: ID
    id: #UUID
    name: string
    type: "Human"
}

Another common way, btw, is to "close the struct". In that case the below would do the trick. Basically, creating a definition (a field starting with #) has the effect of recursively closing it. That means that any other field that is not already defined, or matches a pattern constraint, is not allowed.

#UUID: =~"^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"
entities: #Entities
#Entities:[uuid=#UUID]: {
    id: uuid
    name: string
    type: "Human"
}

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by khepin
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
Category
Q&A
Labels
None yet
3 participants