diff --git a/ast/compile_test.go b/ast/compile_test.go index d65be7718d..2614bba562 100644 --- a/ast/compile_test.go +++ b/ast/compile_test.go @@ -909,6 +909,30 @@ func TestCompilerCheckTypesWithSchema(t *testing.T) { assertNotFailed(t, c) } +func TestCompilerCheckTypesWithRegexPatternInSchema(t *testing.T) { + c := NewCompiler() + var schema interface{} + // Negative lookahead is not supported in the Go regex dialect, but this is still a valid + // JSON schema. Since we don't rely on the "pattern" attribute for type checking, ensure + // that this still works (by being ignored) + err := util.Unmarshal([]byte(`{ + "properties": { + "name": { + "pattern": "^(?!testing:.*)[a-z]+$", + "type": "string" + } + } + }`), &schema) + if err != nil { + t.Fatal("Unexpected error:", err) + } + schemaSet := NewSchemaSet() + schemaSet.Put(SchemaRootRef, schema) + c.WithSchemas(schemaSet) + compileStages(c, c.checkTypes) + assertNotFailed(t, c) +} + func TestCompilerCheckTypesWithAllOfSchema(t *testing.T) { tests := []struct { diff --git a/internal/gojsonschema/schema.go b/internal/gojsonschema/schema.go index 3e6f8c0873..6bdf5ebca4 100644 --- a/internal/gojsonschema/schema.go +++ b/internal/gojsonschema/schema.go @@ -517,17 +517,12 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *SubSchema) } } - if pattern, err := getString(m, KeyPattern); err != nil { + // NOTE: Regex compilation step removed as we don't use "pattern" attribute for + // type checking, and this would cause schemas to fail if they included patterns + // that were valid ECMA regex dialect but not known to Go (i.e. the regexp.Compile + // function), such as patterns with negative lookahead + if _, err := getString(m, KeyPattern); err != nil { return err - } else if pattern != nil { - regexpObject, err := regexp.Compile(*pattern) - if err != nil { - return errors.New(formatErrorDescription( - Locale.MustBeValidRegex(), - ErrorDetails{"key": KeyPattern}, - )) - } - currentSchema.pattern = regexpObject } if format, err := getString(m, KeyFormat); err != nil { diff --git a/internal/gojsonschema/testdata/draft4/pattern.json b/internal/gojsonschema/testdata/draft4/pattern.json index 25e7299731..fa8bdb945e 100644 --- a/internal/gojsonschema/testdata/draft4/pattern.json +++ b/internal/gojsonschema/testdata/draft4/pattern.json @@ -9,9 +9,9 @@ "valid": true }, { - "description": "a non-matching pattern is invalid", + "description": "a non-matching pattern is invalid (but ignored)", "data": "abc", - "valid": false + "valid": true }, { "description": "ignores non-strings", diff --git a/internal/gojsonschema/testdata/draft6/pattern.json b/internal/gojsonschema/testdata/draft6/pattern.json index 25e7299731..fa8bdb945e 100644 --- a/internal/gojsonschema/testdata/draft6/pattern.json +++ b/internal/gojsonschema/testdata/draft6/pattern.json @@ -9,9 +9,9 @@ "valid": true }, { - "description": "a non-matching pattern is invalid", + "description": "a non-matching pattern is invalid (but ignored)", "data": "abc", - "valid": false + "valid": true }, { "description": "ignores non-strings", diff --git a/internal/gojsonschema/testdata/draft7/pattern.json b/internal/gojsonschema/testdata/draft7/pattern.json index 25e7299731..fa8bdb945e 100644 --- a/internal/gojsonschema/testdata/draft7/pattern.json +++ b/internal/gojsonschema/testdata/draft7/pattern.json @@ -9,9 +9,9 @@ "valid": true }, { - "description": "a non-matching pattern is invalid", + "description": "a non-matching pattern is invalid (but ignored)", "data": "abc", - "valid": false + "valid": true }, { "description": "ignores non-strings",