github.com/solo-io/cue@v0.4.7/cue/testdata/builtins/validators.txtar (about)

     1  -- in.cue --
     2  import (
     3      "struct"
     4      "encoding/json"
     5  )
     6  
     7  
     8  // non-monotonic builtins must fail with an "incomplete" error if there
     9  // is a possibility the constraint can get resolved by becoming more specific.
    10  incompleteError1: {
    11      MyType: {
    12              kv: struct.MinFields(1)
    13      }
    14  
    15      foo: MyType & {
    16              kv: joel: "testing"
    17      }
    18  }
    19  
    20  incompleteError2: {
    21      MyType: {
    22              kv: [string]: string
    23              kv: struct.MinFields(1)
    24      }
    25  
    26      foo: MyType & {
    27              kv: joel: "testing"
    28      }
    29  }
    30  
    31  incompleteError3: {
    32      t: string
    33      t: json.Validate(string)
    34  }
    35  
    36  uniqueConstrains1: {
    37      t: string
    38      t: json.Validate(string)
    39      t: json.Validate(string)
    40  }
    41  
    42  uniqueConstrains2: {
    43      t: struct.MaxFields(1)
    44      t: struct.MaxFields(1)
    45  }
    46  
    47  violation: {
    48      #MyType: {
    49              kv: [string]: string
    50              kv: struct.MinFields(1)
    51      }
    52  
    53      foo: #MyType & {
    54              kv: joel: "testing"
    55              kv: tony: "testing"
    56      }
    57  }
    58  
    59  conjuncts: {
    60      kv: struct.MinFields(1)
    61      kv: struct.MaxFields(3)
    62  }
    63  
    64  // TODO: stripe off conflicting pairs
    65  // conflicting: {
    66  //     kv: struct.MinFields(3)
    67  //     kv: struct.MaxFields(1)
    68  // }
    69  
    70  // Builtins with bool return that can be used as validator.
    71  
    72  bareBuiltin: {
    73      a: json.Valid
    74      a: json.Valid
    75  }
    76  
    77  bareBuiltinCheck: {
    78      a: json.Valid
    79      a: "3"
    80  }
    81  
    82  builtinValidator: {
    83      a: json.Valid()
    84      a: json.Valid()
    85  }
    86  
    87  builtinValidatorCheck: {
    88      a: json.Valid()
    89      a: "3"
    90  }
    91  
    92  callOfCallToValidator: {
    93      a: json.Valid
    94      b: a()
    95      e: b() // not allowed
    96      e: "5"
    97  }
    98  
    99  validatorAsFunction: {
   100      a: json.Valid
   101      b: a("3")
   102      c: json.Valid("3")
   103  }
   104  
   105  -- out/eval --
   106  Errors:
   107  callOfCallToValidator.e: cannot call previously called validator b:
   108      ./in.cue:94:8
   109  
   110  Result:
   111  (_|_){
   112    // [eval]
   113    incompleteError1: (struct){
   114      MyType: (struct){
   115        kv: (struct){ struct.MinFields(1) }
   116      }
   117      foo: (struct){
   118        kv: (struct){
   119          joel: (string){ "testing" }
   120        }
   121      }
   122    }
   123    incompleteError2: (struct){
   124      MyType: (struct){
   125        kv: (_|_){
   126          // [incomplete] incompleteError2.MyType.kv: invalid value {} (does not satisfy struct.MinFields(1)): len(fields) < MinFields(1) (0 < 1):
   127          //     ./in.cue:22:17
   128          //     ./in.cue:21:13
   129          //     ./in.cue:22:13
   130          //     ./in.cue:22:34
   131        }
   132      }
   133      foo: (struct){
   134        kv: (struct){
   135          joel: (string){ "testing" }
   136        }
   137      }
   138    }
   139    incompleteError3: (struct){
   140      t: (string){ &("encoding/json".Validate(string), string) }
   141    }
   142    uniqueConstrains1: (struct){
   143      t: (string){ &("encoding/json".Validate(string), string) }
   144    }
   145    uniqueConstrains2: (struct){
   146      t: (struct){ struct.MaxFields(1) }
   147    }
   148    violation: (struct){
   149      #MyType: (#struct){
   150        kv: (_|_){
   151          // [incomplete] violation.#MyType.kv: invalid value {} (does not satisfy struct.MinFields(1)): len(fields) < MinFields(1) (0 < 1):
   152          //     ./in.cue:49:17
   153          //     ./in.cue:48:13
   154          //     ./in.cue:49:13
   155          //     ./in.cue:49:34
   156        }
   157      }
   158      foo: (#struct){
   159        kv: (#struct){
   160          joel: (string){ "testing" }
   161          tony: (string){ "testing" }
   162        }
   163      }
   164    }
   165    conjuncts: (struct){
   166      kv: (struct){ &(struct.MinFields(1), struct.MaxFields(3)) }
   167    }
   168    bareBuiltin: (struct){
   169      a: ((string|bytes)){ "encoding/json".Valid() }
   170    }
   171    bareBuiltinCheck: (struct){
   172      a: (string){ "3" }
   173    }
   174    builtinValidator: (struct){
   175      a: ((string|bytes)){ "encoding/json".Valid() }
   176    }
   177    builtinValidatorCheck: (struct){
   178      a: (string){ "3" }
   179    }
   180    callOfCallToValidator: (_|_){
   181      // [eval]
   182      a: ((string|bytes)){ "encoding/json".Valid() }
   183      b: ((string|bytes)){ "encoding/json".Valid() }
   184      e: (_|_){
   185        // [eval] callOfCallToValidator.e: cannot call previously called validator b:
   186        //     ./in.cue:94:8
   187      }
   188    }
   189    validatorAsFunction: (struct){
   190      a: ((string|bytes)){ "encoding/json".Valid() }
   191      b: (bool){ true }
   192      c: (bool){ true }
   193    }
   194  }
   195  -- out/compile --
   196  --- in.cue
   197  {
   198    incompleteError1: {
   199      MyType: {
   200        kv: 〈import;struct〉.MinFields(1)
   201      }
   202      foo: (〈0;MyType〉 & {
   203        kv: {
   204          joel: "testing"
   205        }
   206      })
   207    }
   208    incompleteError2: {
   209      MyType: {
   210        kv: {
   211          [string]: string
   212        }
   213        kv: 〈import;struct〉.MinFields(1)
   214      }
   215      foo: (〈0;MyType〉 & {
   216        kv: {
   217          joel: "testing"
   218        }
   219      })
   220    }
   221    incompleteError3: {
   222      t: string
   223      t: 〈import;"encoding/json"〉.Validate(string)
   224    }
   225    uniqueConstrains1: {
   226      t: string
   227      t: 〈import;"encoding/json"〉.Validate(string)
   228      t: 〈import;"encoding/json"〉.Validate(string)
   229    }
   230    uniqueConstrains2: {
   231      t: 〈import;struct〉.MaxFields(1)
   232      t: 〈import;struct〉.MaxFields(1)
   233    }
   234    violation: {
   235      #MyType: {
   236        kv: {
   237          [string]: string
   238        }
   239        kv: 〈import;struct〉.MinFields(1)
   240      }
   241      foo: (〈0;#MyType〉 & {
   242        kv: {
   243          joel: "testing"
   244        }
   245        kv: {
   246          tony: "testing"
   247        }
   248      })
   249    }
   250    conjuncts: {
   251      kv: 〈import;struct〉.MinFields(1)
   252      kv: 〈import;struct〉.MaxFields(3)
   253    }
   254    bareBuiltin: {
   255      a: 〈import;"encoding/json"〉.Valid
   256      a: 〈import;"encoding/json"〉.Valid
   257    }
   258    bareBuiltinCheck: {
   259      a: 〈import;"encoding/json"〉.Valid
   260      a: "3"
   261    }
   262    builtinValidator: {
   263      a: 〈import;"encoding/json"〉.Valid()
   264      a: 〈import;"encoding/json"〉.Valid()
   265    }
   266    builtinValidatorCheck: {
   267      a: 〈import;"encoding/json"〉.Valid()
   268      a: "3"
   269    }
   270    callOfCallToValidator: {
   271      a: 〈import;"encoding/json"〉.Valid
   272      b: 〈0;a〉()
   273      e: 〈0;b〉()
   274      e: "5"
   275    }
   276    validatorAsFunction: {
   277      a: 〈import;"encoding/json"〉.Valid
   278      b: 〈0;a〉("3")
   279      c: 〈import;"encoding/json"〉.Valid("3")
   280    }
   281  }