github.com/joomcode/cue@v0.4.4-0.20221111115225-539fe3512047/cue/testdata/builtins/validators.txtar (about)

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