You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If the regex pattern specified for a field in the JSON schema is invalid, the MatchString function fails. To prevent this, the pattern should be validated in advance, and MatchString code should only be generated for valid patterns. For example, the pattern "^(?!\s*$)[a-zA-Z0-9\.]*$" causes MatchString to fail because the Go regexp package does not support lookaheads. Implementing a validation step to check for such unsupported patterns before code generation is a suggested improvement.Attached is a potential solution implemented in the code:
if len(v.pattern) != 0 {
if v.isNillable {
out.Printlnf("if %s != nil {", value)
out.Indent(1)
}
if _, err := regexp.Compile(v.pattern); err != nil {
fmt.Println("WARNING: regex pattern not compatible with go regexp library, ignoring ...")
} else {
out.Printlnf(
`if matched, _ := regexp.MatchString(`+"`%s`"+`, string(%s%s)); !matched {`,
v.pattern, pointerPrefix, value,
)
out.Indent(1)
out.Printlnf(
`return fmt.Errorf("field %%s pattern match: must match %%s", `+"`%s`"+`, "%s")`,
v.pattern, v.fieldName,
)
out.Indent(-1)
out.Printlnf("}")
if v.isNillable {
out.Indent(-1)
out.Printlnf("}")
}
}
}
The text was updated successfully, but these errors were encountered:
If the regex pattern specified for a field in the JSON schema is invalid, the MatchString function fails. To prevent this, the pattern should be validated in advance, and MatchString code should only be generated for valid patterns. For example, the pattern "^(?!\s*$)[a-zA-Z0-9\.]*$" causes MatchString to fail because the Go regexp package does not support lookaheads. Implementing a validation step to check for such unsupported patterns before code generation is a suggested improvement.Attached is a potential solution implemented in the code:
The text was updated successfully, but these errors were encountered: