- Do you want to request a feature or report a bug?
Bug
- What is the current behavior?
Using a regular expression as outlined in documentation for pattern matching validation causes config.yml parsing to fail when special characters are used.
The documentation shows a very simple case without any special characters.
- If the current behavior is a bug, please provide the steps to reproduce.
Add a field to config.yml with a regex pattern that contains special characters (w, b, etc).
example
- {label: URL Path, name: path, widget: string, pattern: ["\/news\/\S+", "Expects the form /news/your-path"]}
Returns the following error:
The config.yml file could not be loaded or failed to parse properly.
Error message: YAMLException: unknown escape sequence at line 23, column 29: pattern: ["\/news\/\S+", "Expects the form /news/you ... ^
To elaborate, it's currently not consistent with how to handle regex options:
This parses:
\/news\/\S+
This does not:
"\/news\/\S+"
This parses:
".{10,}"
This does not:
.{10,}
- What is the expected behavior?
Parsing of config.yml should not fail for any valid regex pattern.
JS-Yaml includes a type for javascript regex, but I'm not sure if it's implemented for this project.
https://github.com/nodeca/js-yaml#supported-yaml-types
- Please mention your CMS, node.js, and operating system version.
Netlify CMS version 0.4.6
This doesn't look to me like a problem with the CMS code - it looks like you're running into issues where your regex syntax is not being escaped correctly or is colliding with YAML syntax. First, when you put a \ in a string in YAML, you need to escape it or YAML will try to interpret it as a _string_ escape, not an escape in the regex you're trying to make. Does "\\/news\\/\\S+" or '\/news\/\S+' (YAML ignores backslash escapes in single-quote strings) parse correctly? If so, that's likely what you're running into.
As for the second example,{ and } have special meaning in YAML, so those need to be stored as a delimited string, not an implicit one. Again, the single-quote version ('.{10,}') is probably the best option.
Please let me know if these don't work.
Yes, using the "\\/news\\/\\S+" parses correctly.
Since escapes are ignored in single quotes, maybe it would be a good idea to update the example in the docs to use single quotes?
@dardub that's a great idea; thanks for the PR!
This tripped me up even with the updated documentation, basically just use single quotes around complex regex patterns you get from regexr.com
Most helpful comment
This tripped me up even with the updated documentation, basically just use single quotes around complex regex patterns you get from regexr.com