简体   繁体   English

如何为有效的 json 和 html5 编码正则表达式模式以进行表单验证?

[英]How to encode regex pattern for valid json and html5 for form validation?

I work on improving the validation of form configurations that are saved in a python flask app.我致力于改进保存在 python flask 应用程序中的表单配置的验证。 The form config comes in via an API in json. A user can save a regex pattern for their form field to have additional validation.表单配置通过 json 中的 API 输入。用户可以为其表单字段保存正则表达式模式以进行额外验证。 Since I also wanted to prevent per default the submission of anything like an url in normal text fields (like First name, last name, etc.) I added a marshmallow regex pattern validation for all text fields: ^((?:\.\/\/).)*$由于我还想防止默认情况下在普通文本字段(如名字、姓氏等)中提交任何类似 url 的内容,我为所有文本字段添加了棉花糖正则表达式模式验证: ^((?:\.\/\/).)*$

I also wanted a matching regex pattern on the frontend, where the form config is rendered into an actual html form.我还希望前端有一个匹配的正则表达式模式,其中表单配置呈现为实际的 html 表单。 This is done by a small petite-vue app that provides the html templates for form fields and automatically adds the pattern from the json config file to the html.这是通过一个小型 petite-vue 应用程序完成的,该应用程序为表单字段提供 html 模板,并自动将模式从 json 配置文件添加到 html。

I noticed:我注意到:

  1. My form config saved in the json file will not be valid.我保存在 json 文件中的表单配置将无效。 The form won't render and I get an error: "Invalid escape character in string."表单不会呈现,并且出现错误: “字符串中的转义字符无效。” . .

Seems the backward slashes \ are a problem for json.似乎反斜杠 \ 是 json 的问题。

  1. The HTML5 pattern validation in my Firefox is not happy with my regex pattern, and gives me this error: "Unable to check input because the pattern is not a valid regexp: invalid identity escape in regular expression" .我的 Firefox 中的 HTML5 模式验证对我的正则表达式模式不满意,并给我这个错误: "Unable to check input because the pattern is not a valid regexp: invalid identity escape in regular expression"

The slashes seem to be an issue here as well.斜线似乎也是这里的一个问题。

I already found out I can:我已经发现我可以:

  • urlencode my regex pattern. urlencode 我的正则表达式模式。
  • escape the back slash for the json file.转义 json 文件的反斜杠。

But now I'm stuck on deciding which potential solution is the more robust choice, and which of the two apps is the place to implement it.但现在我一直在决定哪种潜在解决方案是更可靠的选择,以及这两个应用程序中的哪一个是实施它的地方。


So I need help deciding:所以我需要帮助决定:

  • Should the regex be slash escaped when saving to the form config to provide valid json and the petite-vue app un-escapes and urlencodes the pattern string?在保存到表单配置以提供有效的 json 时是否应该对正则表达式进行斜线转义,而 petite-vue 应用程序是否应该对模式字符串进行转义和 urlencodes? That feels like the right places for the task, but also sounds like a potential error source if encoded/decoding of the slashes is not done right.这感觉就像任务的正确位置,但如果斜杠的编码/解码没有正确完成,这听起来也像是一个潜在的错误源。
  • Should I already urlencode any regex pattern when it gets saved in the python flask app?当它保存在 python flask 应用程序中时,我是否应该已经对任何正则表达式模式进行 urlencode? I would need to validate if it already has been urlencoded.我需要验证它是否已经过 urlencoded。 Maybe by decoding and comparing it to the encoded version?也许通过解码并将其与编码版本进行比较? But I don't mess with the pattern twice.但我不会把图案弄乱两次。
  • Is there a better solution I'm not thinking of?有没有我没有想到的更好的解决方案?

I'll answer my own question.我会回答我自己的问题。 I understand now why it is unclear and not reproducable.我现在明白为什么它不清楚且不可重现。 Hopefully an answer is still valuable.希望答案仍然有价值。

tldr; tldr; The pattern is not right.图案不对。 You don't need to escape the "/" or the ":".您不需要转义“/”或“:”。

Long version长版

In the app flow, the pattern gets saved via python in a json file (ie. it uses json_dumps() and already gets encoded).在应用程序流程中,模式通过 python 保存在 json 文件中(即它使用 json_dumps() 并且已经编码)。 In my test case I just copy/pasted the pattern into the json file, resulting in not valid json. When escaping the \ manually, I either got an error on "invalid identity escape" or the pattern wouldn't match.在我的测试用例中,我只是将模式复制/粘贴到 json 文件中,导致 json 无效。当 escaping 手动为 \ 时,我收到“无效身份转义”错误或模式不匹配。

  • It was helpful to get a solid mental model of the string and its representative states throughout the app.在整个应用程序中获得字符串及其代表状态的坚实心理 model 是有帮助的。
  • It was helpful to research more of the different regex flavors.研究更多不同的正则表达式风格很有帮助。
  • I thought the first comment I found for the error message didn't apply, but it actually did 100%.我以为我找到的第一条错误消息评论不适用,但实际上 100%。 Would have saved me some hours if I had fully embraced it.如果我完全接受它,本可以节省我几个小时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM