@@ -151,6 +151,19 @@ func (b *builder) failf(v cue.Value, format string, args ...interface{}) {
151
151
})
152
152
}
153
153
154
+ func (b * builder ) unsupported (v cue.Value ) {
155
+ if b .format == "" {
156
+ // Not strictly an error, but consider listing it as a warning
157
+ // in strict mode.
158
+ }
159
+ }
160
+
161
+ func (b * builder ) checkArgs (a []cue.Value , n int ) {
162
+ if len (a )- 1 != n {
163
+ b .failf (a [0 ], "%v must be used with %d arguments" , a [0 ], len (a )- 1 )
164
+ }
165
+ }
166
+
154
167
func (b * builder ) schema (name string , v cue.Value ) * oaSchema {
155
168
oldPath := b .ctx .path
156
169
b .ctx .path = append (b .ctx .path , name )
@@ -471,21 +484,18 @@ func (b *builder) object(v cue.Value) {
471
484
name := fmt .Sprint (a [0 ])
472
485
switch name {
473
486
case "struct.MinFields" :
474
- if len (a ) != 2 {
475
- b .failf (v , "builtin %v must be called with one argument" , name )
476
- }
487
+ b .checkArgs (a , 1 )
477
488
b .setFilter ("Schema" , "minProperties" , b .int (a [1 ]))
478
489
return
479
490
480
491
case "struct.MaxFields" :
481
- if len (a ) != 2 {
482
- b .failf (v , "builtin %v must be called with one argument" , name )
483
- }
492
+ b .checkArgs (a , 1 )
484
493
b .setFilter ("Schema" , "maxProperties" , b .int (a [1 ]))
485
494
return
486
495
487
496
default :
488
- b .failf (v , "builtin %v not supported in OpenAPI" , name )
497
+ b .unsupported (a [0 ])
498
+ return
489
499
}
490
500
491
501
case cue .NoOp :
@@ -549,28 +559,23 @@ func (b *builder) array(v cue.Value) {
549
559
name := fmt .Sprint (a [0 ])
550
560
switch name {
551
561
case "list.UniqueItems" :
552
- if len (a ) != 1 {
553
- b .failf (v , "builtin %v may only be used without arguments" , name )
554
- }
562
+ b .checkArgs (a , 0 )
555
563
b .setFilter ("Schema" , "uniqueItems" , true )
556
564
return
557
565
558
566
case "list.MinItems" :
559
- if len (a ) != 2 {
560
- b .failf (v , "builtin %v must be called with one argument" , name )
561
- }
567
+ b .checkArgs (a , 1 )
562
568
b .setFilter ("Schema" , "minItems" , b .int (a [1 ]))
563
569
return
564
570
565
571
case "list.MaxItems" :
566
- if len (a ) != 2 {
567
- b .failf (v , "builtin %v must be called with one argument" , name )
568
- }
572
+ b .checkArgs (a , 1 )
569
573
b .setFilter ("Schema" , "maxItems" , b .int (a [1 ]))
570
574
return
571
575
572
576
default :
573
- b .failf (v , "builtin %v not supported in OpenAPI" , name )
577
+ b .unsupported (a [0 ])
578
+ return
574
579
}
575
580
576
581
case cue .NoOp :
@@ -681,12 +686,12 @@ func (b *builder) number(v cue.Value) {
681
686
name := fmt .Sprint (a [0 ])
682
687
switch name {
683
688
case "math.MultipleOf" :
684
- if len (a ) != 2 {
685
- b .failf (v , "builtin %v may only be used with single argument" , name )
686
- }
689
+ b .checkArgs (a , 1 )
687
690
b .setFilter ("Schema" , "multipleOf" , b .int (a [1 ]))
691
+
688
692
default :
689
- b .failf (v , "builtin %v not supported in OpenAPI" , name )
693
+ b .unsupported (a [0 ])
694
+ return
690
695
}
691
696
692
697
case cue .NoOp :
@@ -754,28 +759,26 @@ func (b *builder) string(v cue.Value) {
754
759
b .setNot ("pattern" , s )
755
760
}
756
761
757
- // TODO: support the following JSON schema constraints
758
- // - maxLength
759
- // - minLength
760
-
761
762
case cue .NoOp , cue .SelectorOp :
762
763
// TODO: determine formats from specific types.
763
764
764
765
case cue .CallOp :
765
766
name := fmt .Sprint (a [0 ])
766
- field := ""
767
767
switch name {
768
768
case "strings.MinRunes" :
769
- field = "minLength"
769
+ b .checkArgs (a , 1 )
770
+ b .setFilter ("Schema" , "minLength" , b .int (a [1 ]))
771
+ return
772
+
770
773
case "strings.MaxRunes" :
771
- field = "maxLength"
774
+ b .checkArgs (a , 1 )
775
+ b .setFilter ("Schema" , "maxLength" , b .int (a [1 ]))
776
+ return
777
+
772
778
default :
773
- b .failf (v , "builtin %v not supported in OpenAPI" , name )
774
- }
775
- if len (a ) != 2 {
776
- b .failf (v , "builtin %v may only be used with single argument" , name )
779
+ b .unsupported (a [0 ])
780
+ return
777
781
}
778
- b .setFilter ("Schema" , field , b .int (a [1 ]))
779
782
780
783
default :
781
784
b .failf (v , "unsupported op %v for string type" , op )
0 commit comments