Skip to content

Commit 6cea136

Browse files
committed
encoding/openapi: introduce support validator methods
ensure consistent error messages and reduce boilerplate Issue #56 Change-Id: Ideea2b82ba2d803a0601574c916532b4065097fd Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2723 Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 64cb20a commit 6cea136

File tree

1 file changed

+36
-33
lines changed

1 file changed

+36
-33
lines changed

encoding/openapi/build.go

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,19 @@ func (b *builder) failf(v cue.Value, format string, args ...interface{}) {
151151
})
152152
}
153153

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+
154167
func (b *builder) schema(name string, v cue.Value) *oaSchema {
155168
oldPath := b.ctx.path
156169
b.ctx.path = append(b.ctx.path, name)
@@ -471,21 +484,18 @@ func (b *builder) object(v cue.Value) {
471484
name := fmt.Sprint(a[0])
472485
switch name {
473486
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)
477488
b.setFilter("Schema", "minProperties", b.int(a[1]))
478489
return
479490

480491
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)
484493
b.setFilter("Schema", "maxProperties", b.int(a[1]))
485494
return
486495

487496
default:
488-
b.failf(v, "builtin %v not supported in OpenAPI", name)
497+
b.unsupported(a[0])
498+
return
489499
}
490500

491501
case cue.NoOp:
@@ -549,28 +559,23 @@ func (b *builder) array(v cue.Value) {
549559
name := fmt.Sprint(a[0])
550560
switch name {
551561
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)
555563
b.setFilter("Schema", "uniqueItems", true)
556564
return
557565

558566
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)
562568
b.setFilter("Schema", "minItems", b.int(a[1]))
563569
return
564570

565571
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)
569573
b.setFilter("Schema", "maxItems", b.int(a[1]))
570574
return
571575

572576
default:
573-
b.failf(v, "builtin %v not supported in OpenAPI", name)
577+
b.unsupported(a[0])
578+
return
574579
}
575580

576581
case cue.NoOp:
@@ -681,12 +686,12 @@ func (b *builder) number(v cue.Value) {
681686
name := fmt.Sprint(a[0])
682687
switch name {
683688
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)
687690
b.setFilter("Schema", "multipleOf", b.int(a[1]))
691+
688692
default:
689-
b.failf(v, "builtin %v not supported in OpenAPI", name)
693+
b.unsupported(a[0])
694+
return
690695
}
691696

692697
case cue.NoOp:
@@ -754,28 +759,26 @@ func (b *builder) string(v cue.Value) {
754759
b.setNot("pattern", s)
755760
}
756761

757-
// TODO: support the following JSON schema constraints
758-
// - maxLength
759-
// - minLength
760-
761762
case cue.NoOp, cue.SelectorOp:
762763
// TODO: determine formats from specific types.
763764

764765
case cue.CallOp:
765766
name := fmt.Sprint(a[0])
766-
field := ""
767767
switch name {
768768
case "strings.MinRunes":
769-
field = "minLength"
769+
b.checkArgs(a, 1)
770+
b.setFilter("Schema", "minLength", b.int(a[1]))
771+
return
772+
770773
case "strings.MaxRunes":
771-
field = "maxLength"
774+
b.checkArgs(a, 1)
775+
b.setFilter("Schema", "maxLength", b.int(a[1]))
776+
return
777+
772778
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
777781
}
778-
b.setFilter("Schema", field, b.int(a[1]))
779782

780783
default:
781784
b.failf(v, "unsupported op %v for string type", op)

0 commit comments

Comments
 (0)