From 8fbccf4fc597b8cabe1cc7d4326e12ea4998dbd8 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Thu, 12 Mar 2026 17:36:57 +0100 Subject: [PATCH] Keep the parsed pattern property keys --- schema.go | 4 ++-- subSchema.go | 1 + validation.go | 5 ++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/schema.go b/schema.go index 5a2599e9..05e7ddf3 100644 --- a/schema.go +++ b/schema.go @@ -321,14 +321,14 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) if len(patternPropertiesMap) > 0 { currentSchema.patternProperties = make(map[string]*subSchema) for k, v := range patternPropertiesMap { - _, err := regexp.MatchString(k, "") + re, err := regexp.Compile(k) if err != nil { return errors.New(formatErrorDescription( Locale.RegexPattern(), ErrorDetails{"pattern": k}, )) } - newSchema := &subSchema{property: k, parent: currentSchema, ref: currentSchema.ref} + newSchema := &subSchema{property: k, parent: currentSchema, ref: currentSchema.ref, patternPropertyKey: re} err = d.parseSchema(v, newSchema) if err != nil { return errors.New(err.Error()) diff --git a/subSchema.go b/subSchema.go index ec779812..51ea42bd 100644 --- a/subSchema.go +++ b/subSchema.go @@ -124,6 +124,7 @@ type subSchema struct { dependencies map[string]interface{} additionalProperties interface{} patternProperties map[string]*subSchema + patternPropertyKey *regexp.Regexp propertyNames *subSchema // validation : array diff --git a/validation.go b/validation.go index 9081bd92..0c9c3e66 100644 --- a/validation.go +++ b/validation.go @@ -29,7 +29,6 @@ import ( "encoding/json" "math/big" "reflect" - "regexp" "strconv" "strings" "unicode/utf8" @@ -687,8 +686,8 @@ func (v *subSchema) validatePatternProperty(currentSubSchema *subSchema, key str validated := false - for pk, pv := range currentSubSchema.patternProperties { - if matches, _ := regexp.MatchString(pk, key); matches { + for _, pv := range currentSubSchema.patternProperties { + if pv.patternPropertyKey != nil && pv.patternPropertyKey.MatchString(key) { validated = true subContext := NewJsonContext(key, context) validationResult := pv.subValidateWithContext(value, subContext)