Skip to content
Discussion options

You must be logged in to vote

A type alias can only be a union of types and literal values ("a" | "b", 1 | 2, schema names). A, B and C are variables holding instances, so the type checker doesn't resolve them there, hence name 'A' is not defined. Two patterns that give you the guard (both tested with KCL 0.13.3):

1. Keep the instances, enforce membership with a check block

schema ContextType:
    id: int
    name: str
    description?: str

A = ContextType { id = 1, name = "A" }
B = ContextType { id = 2, name = "B" }
C = ContextType { id = 3, name = "C" }
_allowed = [A, B, C]

schema Service:
    name: str
    context: ContextType

    check:
        context in _allowed, "context must be one of A, B, C, got ${context…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by javierarrieta
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants