Skip to content

Commit

Permalink
encoding/jsonschema: implement regex format
Browse files Browse the repository at this point in the history
As the TODO says, this is a bit stricter than the specification,
but it will probably be OK for now.

For #3479.

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: Icbc9bf6a98058d5f6ca9c0fe87f5e29e4685255b
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202108
Reviewed-by: Daniel Martí <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
rogpeppe committed Oct 7, 2024
1 parent c54ac60 commit 95fd2e9
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 25 deletions.
8 changes: 7 additions & 1 deletion encoding/jsonschema/constraints_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var formatFuncs = sync.OnceValue(func() map[string]formatFuncInfo {
"iri-reference": {vfrom(VersionDraft7), formatURIReference},
"json-pointer": {vfrom(VersionDraft6), formatTODO},
"password": {openAPI, formatTODO},
"regex": {vfrom(VersionDraft7), formatTODO},
"regex": {vfrom(VersionDraft7), formatRegex},
"relative-json-pointer": {vfrom(VersionDraft7), formatTODO},
"time": {vfrom(VersionDraft7), formatTODO},
// TODO we should probably disallow non-ASCII URIs (IRIs) but
Expand Down Expand Up @@ -100,4 +100,10 @@ func formatDateTime(n cue.Value, s *state) {
s.add(n, stringType, ast.NewSel(s.addImport(n, "time"), "Time"))
}

func formatRegex(n cue.Value, s *state) {
// TODO this is a bit stricter than the spec, because the spec
// allows Perl idioms such as back-references.
s.add(n, stringType, ast.NewSel(s.addImport(n, "regexp"), "Valid"))
}

func formatTODO(n cue.Value, s *state) {}
16 changes: 8 additions & 8 deletions encoding/jsonschema/external_teststats.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# Generated by teststats. DO NOT EDIT
v2:
schema extract (pass / total): 993 / 1363 = 72.9%
tests (pass / total): 3513 / 4803 = 73.1%
tests on extracted schemas (pass / total): 3513 / 3801 = 92.4%
tests (pass / total): 3512 / 4803 = 73.1%
tests on extracted schemas (pass / total): 3512 / 3801 = 92.4%

v3:
schema extract (pass / total): 981 / 1363 = 72.0%
tests (pass / total): 3474 / 4803 = 72.3%
tests on extracted schemas (pass / total): 3474 / 3757 = 92.5%
tests (pass / total): 3473 / 4803 = 72.3%
tests on extracted schemas (pass / total): 3473 / 3757 = 92.4%

Optional tests

v2:
schema extract (pass / total): 230 / 274 = 83.9%
tests (pass / total): 1505 / 2372 = 63.4%
tests on extracted schemas (pass / total): 1505 / 2258 = 66.7%
tests (pass / total): 1508 / 2372 = 63.6%
tests on extracted schemas (pass / total): 1508 / 2258 = 66.8%

v3:
schema extract (pass / total): 230 / 274 = 83.9%
tests (pass / total): 1495 / 2372 = 63.0%
tests on extracted schemas (pass / total): 1495 / 2258 = 66.2%
tests (pass / total): 1498 / 2372 = 63.2%
tests on extracted schemas (pass / total): 1498 / 2258 = 66.3%
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@
{
"description": "a regular expression with unclosed parens is invalid",
"data": "^(abc]",
"valid": false,
"skip": {
"v2": "unexpected success",
"v3": "unexpected success"
}
"valid": false
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@
{
"description": "invalid regex string is only an annotation by default",
"data": "^(abc]",
"valid": true
"valid": true,
"skip": {
"v2": "6 errors in empty disjunction:\nconflicting values \"^(abc]\" and [...] (mismatched types string and list):\n generated.cue:4:1\n generated.cue:4:39\n instance.json:1:1\nconflicting values \"^(abc]\" and bool (mismatched types string and bool):\n generated.cue:4:1\n generated.cue:4:8\n instance.json:1:1\nconflicting values \"^(abc]\" and null (mismatched types string and null):\n generated.cue:4:1\n instance.json:1:1\nconflicting values \"^(abc]\" and number (mismatched types string and number):\n generated.cue:4:1\n generated.cue:4:15\n instance.json:1:1\nconflicting values \"^(abc]\" and {...} (mismatched types string and struct):\n generated.cue:4:1\n generated.cue:4:47\n instance.json:1:1\ninvalid value \"^(abc]\" (does not satisfy regexp.Valid): error in call to regexp.Valid: error parsing regexp: missing closing ): `^(abc]`:\n generated.cue:1:1\n instance.json:1:1\n",
"v3": "conflicting values \"^(abc]\" and [...] (mismatched types string and list):\n generated.cue:4:39\n instance.json:1:1\nconflicting values \"^(abc]\" and bool (mismatched types string and bool):\n generated.cue:4:8\n instance.json:1:1\nconflicting values \"^(abc]\" and null (mismatched types string and null):\n generated.cue:4:1\n instance.json:1:1\nconflicting values \"^(abc]\" and number (mismatched types string and number):\n generated.cue:4:15\n instance.json:1:1\nconflicting values \"^(abc]\" and {...} (mismatched types string and struct):\n generated.cue:4:47\n instance.json:1:1\ninvalid value \"^(abc]\" (does not satisfy regexp.Valid): error in call to regexp.Valid: error parsing regexp: missing closing ): `^(abc]`:\n generated.cue:1:1\n instance.json:1:1\n"
}
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@
{
"description": "a regular expression with unclosed parens is invalid",
"data": "^(abc]",
"valid": false,
"skip": {
"v2": "unexpected success",
"v3": "unexpected success"
}
"valid": false
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@
{
"description": "a regular expression with unclosed parens is invalid",
"data": "^(abc]",
"valid": false,
"skip": {
"v2": "unexpected success",
"v3": "unexpected success"
}
"valid": false
}
]
}
Expand Down

0 comments on commit 95fd2e9

Please sign in to comment.