Skip to content

Validate conditional fields #706

Answered by cueckoo
cueckoo asked this question in Q&A
Jul 3, 2021 · 2 comments · 7 replies
Discussion options

You must be logged in to vote

Original reply by @extemporalgenome in cuelang/cue#706 (comment)

You can open a definition struct using ..., such as:

#Person: {
  name: string
  age: int

  if age < 18 {
      responsible: string
  }
  ...
}

However, that means data or other types that refer to #Person are now open ended to accept any other fields, which is often not desired.

I'm fairly new to CUE, but I believe the conventional approach is:

#Person: {
  name: string
  age:  int & >=0
}

#Person: {
  age:         <18
  responsible: string
  aspirations: [string]
} | {
  age: >=18
  regrets: [string]
}

This evaluates to:

#Person: {
    name:        string
    age:         uint & <18
    responsible: string
    aspirati…

Replies: 2 comments 7 replies

Comment options

cueckoo
Jul 3, 2021
Collaborator Author

You must be logged in to vote
1 reply
@cueckoo
Comment options

cueckoo Jul 3, 2021
Collaborator Author

Answer selected by cueckoo
Comment options

cueckoo
Jul 3, 2021
Collaborator Author

You must be logged in to vote
6 replies
@cueckoo
Comment options

cueckoo Jul 3, 2021
Collaborator Author

@cueckoo
Comment options

cueckoo Jul 3, 2021
Collaborator Author

@cueckoo
Comment options

cueckoo Jul 3, 2021
Collaborator Author

@cueckoo
Comment options

cueckoo Jul 3, 2021
Collaborator Author

@cueckoo
Comment options

cueckoo Jul 3, 2021
Collaborator Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant