From f811ee7df6785c052157d7ab2e6c4969d345b75c Mon Sep 17 00:00:00 2001 From: Roger Peppe Date: Thu, 12 Sep 2024 15:42:15 +0100 Subject: [PATCH] encoding/jsonschema: non-string regular expressions should error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The error from `Value.String` was being ignored. Use `strValue` instead, which gives an error if the value is not a string. Signed-off-by: Roger Peppe Change-Id: If599225bd9f3e2f5db6db3153dd0a40c01393504 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201124 Reviewed-by: Daniel Martí Unity-Result: CUE porcuepine TryBot-Result: CUEcueckoo --- encoding/jsonschema/constraints_string.go | 2 +- .../jsonschema/testdata/txtar/invalid_pattern.txtar | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 encoding/jsonschema/testdata/txtar/invalid_pattern.txtar diff --git a/encoding/jsonschema/constraints_string.go b/encoding/jsonschema/constraints_string.go index b0126d8a9..8cc00f475 100644 --- a/encoding/jsonschema/constraints_string.go +++ b/encoding/jsonschema/constraints_string.go @@ -49,7 +49,7 @@ func constraintMinLength(key string, n cue.Value, s *state) { } func constraintPattern(key string, n cue.Value, s *state) { - str, _ := n.String() + str, _ := s.strValue(n) if _, err := regexp.Compile(str); err != nil { if s.cfg.StrictFeatures { // TODO check if the error is only because of an unsupported diff --git a/encoding/jsonschema/testdata/txtar/invalid_pattern.txtar b/encoding/jsonschema/testdata/txtar/invalid_pattern.txtar new file mode 100644 index 000000000..e5663126b --- /dev/null +++ b/encoding/jsonschema/testdata/txtar/invalid_pattern.txtar @@ -0,0 +1,11 @@ +Invalid patterns should result in failure + +-- schema.json -- +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "pattern": 1234 +} +-- out/decode/extract -- +ERROR: +invalid string: + schema.json:3:5