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

Validate conditional fields #706

Answered by extemporalgenome
ostcar asked this question in Q&A
Discussion options

You must be logged in to vote

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
    aspirations: [string]
} | {
    name: string
    age:  >=18 & int
    reg…

Replies: 3 comments 7 replies

Comment options

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

Answer selected by ostcar
Comment options

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

@ostcar
Comment options

@myitcv
Comment options

@myitcv
Comment options

@ostcar
Comment options

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
4 participants