Skip to content

Commit

Permalink
encoding/jsonschema: implement "not"
Browse files Browse the repository at this point in the history
This change implements the "not" keyword using `matchN`.
No test regressions were encountered when updating the external test data.

One regression was encountered when updating the regular
test data, and filed as a comment under issue #3422.
This required updating the test data to get the test to pass.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: Ib2537af7424895843eebc1b915bc643894637afa
  • Loading branch information
rogpeppe committed Sep 3, 2024
1 parent 000b8f5 commit 17d8cce
Show file tree
Hide file tree
Showing 21 changed files with 202 additions and 926 deletions.
1 change: 1 addition & 0 deletions encoding/jsonschema/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ var constraints = []*constraint{
p2("minLength", constraintMinLength),
p3("minimum", constraintMinimum),
p2("multipleOf", constraintMultipleOf),
p3("not", constraintNot),
p3("oneOf", constraintOneOf),
p2("nullable", constraintNullable),
p2("pattern", constraintPattern),
Expand Down
12 changes: 12 additions & 0 deletions encoding/jsonschema/constraints_combinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,15 @@ func constraintOneOf(key string, n cue.Value, s *state) {
// TODO: oneOf({a:x}, {b:y}, ..., not(anyOf({a:x}, {b:y}, ...))),
// can be translated to {} | {a:x}, {b:y}, ...
}

func constraintNot(key string, n cue.Value, s *state) {
subSchema := s.schema(n)
s.all.add(n, ast.NewCall(
ast.NewIdent("matchN"),
&ast.BasicLit{
Kind: token.INT,
Value: "0",
},
ast.NewList(subSchema),
))
}
12 changes: 6 additions & 6 deletions encoding/jsonschema/external_teststats.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Generated by teststats. DO NOT EDIT
v2:
schema extract (pass / total): 975 / 1637 = 59.6%
tests (pass / total): 3140 / 7175 = 43.8%
tests on extracted schemas (pass / total): 3140 / 3546 = 88.6%
schema extract (pass / total): 1015 / 1637 = 62.0%
tests (pass / total): 3308 / 7175 = 46.1%
tests on extracted schemas (pass / total): 3308 / 3722 = 88.9%

v3:
schema extract (pass / total): 967 / 1637 = 59.1%
tests (pass / total): 3074 / 7175 = 42.8%
tests on extracted schemas (pass / total): 3074 / 3538 = 86.9%
schema extract (pass / total): 1003 / 1637 = 61.3%
tests (pass / total): 3214 / 7175 = 44.8%
tests on extracted schemas (pass / total): 3214 / 3678 = 87.4%
Loading

0 comments on commit 17d8cce

Please sign in to comment.