-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathpattern.go
More file actions
28 lines (23 loc) · 739 Bytes
/
pattern.go
File metadata and controls
28 lines (23 loc) · 739 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package jsonschema
import "regexp"
func evaluatePattern(schema *Schema, instance string) *EvaluationError {
if schema.Pattern == nil {
return nil
}
if schema.compiledStringPattern == nil {
regExp, err := regexp.Compile(*schema.Pattern)
if err != nil {
return NewEvaluationError("pattern", "invalid_pattern", "Invalid regular expression pattern {pattern}", map[string]any{
"pattern": *schema.Pattern,
})
}
schema.compiledStringPattern = regExp
}
if !schema.compiledStringPattern.MatchString(instance) {
return NewEvaluationError("pattern", "pattern_mismatch", "Value does not match the required pattern {pattern}", map[string]any{
"pattern": *schema.Pattern,
"value": instance,
})
}
return nil
}