模式中的转义字符的JSON(模式)验证失败 [英] JSON (schema) validation with escaped characters in patterns fails

查看:152
本文介绍了模式中的转义字符的JSON(模式)验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下JSON对象有效:

The following JSON object is valid:

{
    "foo": "bar",
    "pattern": "^(\/?[-a-zA-Z0-9_.]+)+$"
}

而不是

{
    "foo": "bar",
    "pattern": "^(\/?[-a-zA-Z0-9_.]+)+\.jpg$"
}

这是转义的 \\。),但我看不出为什么这不应该是有效的JSON。我需要在我的真正的JSON模式中包含这样的模式。正则表达式更复杂,没有办法漏掉,特别是点。

It's the escaped dot (\.), but I can't see why this should not be valid JSON. I need to include such patterns in my real JSON schemas. The regexp there are far more complex and there is no way missing out on excaping, especially the dot.

BTW,在字符类中转义 hypens ,例如 [az\- ] 中断验证。

BTW, escaping hypens in character classes such as in [a-z\-] breaks validation as well.

如何解决?

编辑:我使用了 http://jsonlint.com/ http://jsonvalidator.mytechlabs.com/ 和几个节点库。

I used http://jsonlint.com/, http://jsonvalidator.mytechlabs.com/ and a couple of node libraries.

推荐答案

你需要在这里双重逃跑。斜杠是json中的一个转义字符,所以你不能逃避点(因为它看到它),而是需要转义该反斜杠,所以你的正则表达式出来与 \。喜欢它应该(json期待逃脱后的保留字符,即一个报价或另一个斜杠或东西)。

You need to double escape here. The slash is an escape character in json so you can't escape the dot (as it sees it) instead you need to escape that backslash so your regex comes out with \. like it should (json is expecting a reserved character after the escape ie a quote or another slash or something).

// passes validation
{
    "foo": "bar",
    "pattern": "^(/?[-a-zA-Z0-9_.]+)+\\.jpg$"
}

这篇关于模式中的转义字符的JSON(模式)验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆