Skip to content

Commit

Permalink
customRegex: fix missing places
Browse files Browse the repository at this point in the history
  • Loading branch information
santhosh-tekuri committed Feb 8, 2024
1 parent b76a0ca commit d33a6df
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
36 changes: 23 additions & 13 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,9 @@ func MustCompileString(url, schema string) *Schema {
// To change this behavior change Compiler.Draft value
func NewCompiler() *Compiler {
return &Compiler{
Draft: latest,
resources: make(map[string]*resource),
Formats: make(map[string]func(interface{}) bool),
CompileRegex: func(s string) (Regexp, error) {
re, err := regexp.Compile(s)
return (*goRegexp)(re), err
},
Draft: latest,
resources: make(map[string]*resource),
Formats: make(map[string]func(interface{}) bool),
Decoders: make(map[string]func(string) ([]byte, error)),
MediaTypes: make(map[string]func([]byte) error),
extensions: make(map[string]extension),
Expand Down Expand Up @@ -498,11 +494,7 @@ func (c *Compiler) compileMap(r *resource, stack []schemaRef, sref schemaRef, re
s.MinLength, s.MaxLength = loadInt("minLength"), loadInt("maxLength")

if pattern, ok := m["pattern"]; ok {
var err error
s.Pattern, err = c.CompileRegex(pattern.(string))
if err != nil {
panic("regex Format and compiler.CompileRegex are incompatible")
}
s.Pattern = c.compileRegex(pattern.(string))
}

if r.draft.version >= 2019 {
Expand Down Expand Up @@ -575,13 +567,19 @@ func (c *Compiler) compileMap(r *resource, stack []schemaRef, sref schemaRef, re

if regexProps, ok := m["regexProperties"]; ok {
s.RegexProperties = regexProps.(bool)
if s.RegexProperties {
s.regexPropertiesFormat = isRegex
if fmt, ok := c.Formats["regex"]; ok {
s.regexPropertiesFormat = fmt
}
}
}

if patternProps, ok := m["patternProperties"]; ok {
patternProps := patternProps.(map[string]interface{})
s.PatternProperties = make(map[Regexp]*Schema, len(patternProps))
for pattern := range patternProps {
s.PatternProperties[regexp.MustCompile(pattern)], err = compile(nil, "patternProperties/"+escape(pattern))
s.PatternProperties[c.compileRegex(pattern)], err = compile(nil, "patternProperties/"+escape(pattern))
if err != nil {
return err
}
Expand Down Expand Up @@ -813,6 +811,18 @@ func (c *Compiler) validateSchema(r *resource, v interface{}, vloc string) error
return nil
}

func (c *Compiler) compileRegex(s string) Regexp {
if c.CompileRegex != nil {
re, err := c.CompileRegex(s)
if err != nil {
panic("regex Format and compiler.CompileRegex are incompatible")
}
return re
}
re := regexp.MustCompile(s)
return (*goRegexp)(re)
}

func toStrings(arr []interface{}) []string {
s := make([]string, len(arr))
for i, v := range arr {
Expand Down
3 changes: 2 additions & 1 deletion schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Schema struct {
Properties map[string]*Schema
PropertyNames *Schema
RegexProperties bool // property names must be valid regex. used only in draft4 as workaround in metaschema.
regexPropertiesFormat func(interface{}) bool
PatternProperties map[Regexp]*Schema
AdditionalProperties interface{} // nil or bool or *Schema.
Dependencies map[string]interface{} // map value is *Schema or []string.
Expand Down Expand Up @@ -349,7 +350,7 @@ func (s *Schema) validate(scope []schemaRef, vscope int, spath string, v interfa

if s.RegexProperties {
for pname := range v {
if !isRegex(pname) {
if !s.regexPropertiesFormat(pname) {
errors = append(errors, validationError("", "patternProperty %s is not valid regex", quote(pname)))
}
}
Expand Down

0 comments on commit d33a6df

Please sign in to comment.