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

Commit

Permalink
encoding/jsonschema: add several validators
Browse files Browse the repository at this point in the history
- minLength
- maxLength
- additionalItems

Change-Id: I09d9e353914b0fece70a26d97db5c4665c4e5f8b
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5651
Reviewed-by: roger peppe <rogpeppe@gmail.com>
  • Loading branch information
mpvl committed Apr 15, 2020
1 parent ed4fc4d commit b99d364
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
31 changes: 27 additions & 4 deletions encoding/jsonschema/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,21 @@ var constraints = []*constraint{
s.addConjunct(&ast.UnaryExpr{Op: token.MAT, X: s.string(n)})
}),

p0("minLength", func(n cue.Value, s *state) {
s.usedTypes |= cue.StringKind
min := s.number(n)
strings := s.addImport("strings")
s.addConjunct(ast.NewCall(ast.NewSel(strings, "MinRunes"), min))

}),

p0("maxLength", func(n cue.Value, s *state) {
s.usedTypes |= cue.StringKind
max := s.number(n)
strings := s.addImport("strings")
s.addConjunct(ast.NewCall(ast.NewSel(strings, "MaxRunes"), max))
}),

p0d("contentMediaType", 7, func(n cue.Value, s *state) {
s.usedTypes |= cue.StringKind
}),
Expand Down Expand Up @@ -530,9 +545,6 @@ var constraints = []*constraint{

p0("items", func(n cue.Value, s *state) {
s.usedTypes |= cue.ListKind
if s.list != nil {
s.errf(n, `"items" declared more than once, previous declaration at %s`, s.list.Pos())
}
switch n.Kind() {
case cue.StructKind:
elem := s.schema(n)
Expand All @@ -546,13 +558,22 @@ var constraints = []*constraint{
ast.SetRelPos(v, token.NoRelPos)
a = append(a, v)
}
s.addConjunct(ast.NewList(a...))
s.list = ast.NewList(a...)
s.addConjunct(s.list)

default:
s.errf(n, `value of "items" must be an object or array`)
}
}),

p0("additionalItems", func(n cue.Value, s *state) {
s.usedTypes |= cue.ListKind
if s.list != nil {
elem := s.schema(n)
s.list.Elts = append(s.list.Elts, &ast.Ellipsis{Type: elem})
}
}),

p0("contains", func(n cue.Value, s *state) {
s.usedTypes |= cue.ListKind
list := s.addImport("list")
Expand All @@ -562,6 +583,8 @@ var constraints = []*constraint{
}
}),

// TODO: min/maxContains

p0("minItems", func(n cue.Value, s *state) {
s.usedTypes |= cue.ListKind
list := s.addImport("list")
Expand Down
3 changes: 1 addition & 2 deletions encoding/jsonschema/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,7 @@ func (s *state) hasConstraints() bool {
len(s.patterns) > 0 ||
s.title != "" ||
s.description != "" ||
s.obj != nil ||
s.list != nil
s.obj != nil
}

const allTypes = cue.NullKind | cue.BoolKind | cue.NumberKind | cue.IntKind |
Expand Down
8 changes: 6 additions & 2 deletions encoding/jsonschema/testdata/basic.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"name": { "type": "string" },
"address": {
"description": "where does this person live?",
"type": "string"
"type": "string",
"minLength": 4,
"maxLength": 20
},
"children": {
"description": "A very large comment that will be wrapped after a certain line length. Let's keep on going and see what happens.",
Expand All @@ -33,6 +35,8 @@
}

-- out.cue --
import "strings"

// Main schema
//
// Specify who you are and all.
Expand All @@ -43,7 +47,7 @@ Schema :: {
name: string

// where does this person live?
address?: string
address?: strings.MinRunes(4) & strings.MaxRunes(20)

// A very large comment that will be wrapped after a certain line
// length. Let's keep on going and see what happens.
Expand Down
9 changes: 9 additions & 0 deletions encoding/jsonschema/testdata/list.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ properties:
maxItems: 9
uniqueItems: true

additional:
type: array
items:
- type: integer
- type: integer
additionalItems:
type: string

additionalProperties: false

-- out.cue --
Expand All @@ -35,4 +43,5 @@ Schema :: {
tuple?: [string, int, 2]
has?: list.Contains(3)
size?: list.MinItems(3) & list.MaxItems(9) & list.UniqueItems()
additional?: [int, int, ...string]
}

0 comments on commit b99d364

Please sign in to comment.