简体   繁体   English

正则表达式不适用于 Wordpress REST API

[英]regex not working with Wordpress REST API

The Wordpress REST API lets you validate a parameter against a regex if you provide one in the API route's schema:如果您在 API 路由的架构中提供参数,Wordpress REST API 允许您针对正则表达式验证参数:

register_rest_route( $this->namespace, "/posts", [
   "methods" => "POST",
   "permission_callback" => "__return_true",
   "callback" => "create_post",
   "args" => [       
      "title" => [
         "type" => "string",
         "description" => "The posts's title",
         "required" => true,
         "pattern" => '/^[A-Za-z0-9 ]+$/' // <-- Here's the pattern!
      ]
   ]
]);

The pattern I provided should accept all alphanumeric values and single spaces.我提供的模式应该接受所有字母数字值和单个空格。 Indeed, the pattern works if I test it in isolation:确实,如果我单独测试该模式,则该模式有效:

preg_match( '/^[A-Za-z0-9 ]+$/', 'Title'); // Returns 1 as expected
preg_match( '/^[A-Za-z0-9 ]+$/', 'Test Title'); // Returns 1 as expected
preg_match( '/^[A-Za-z0-9 ]+$/', '123'); // Returns 1 as expected
preg_match( '/^[A-Za-z0-9 ]+$/', '&Title&'); // Returns 0 as expected

However, the pattern does not seem to work in the context of the Wordpress API!但是,该模式似乎不适用于 Wordpress API 的上下文! If I make a request to the /post route passing any of the above test values as the title, I always get the following error:如果我通过任何上述测试值作为标题向 /post 路由发出请求,我总是会收到以下错误:

{
    "code": "rest_invalid_param",
    "message": "Invalid parameter(s): title",
    "data": {
        "status": 400,
        "params": {
            "title": "title does not match pattern /^[A-Za-z0-9 ]+$/."
        },
        "details": {
            "title": {
                "code": "rest_invalid_pattern",
                "message": "title does not match pattern /^[A-Za-z0-9 ]+$/.",
                "data": null
            }
        }
    }
}

I've dug a bit deeper and discovered that internally Wordpress uses this function:我挖得更深了,发现内部 Wordpress 使用这个函数:

function rest_validate_json_schema_pattern( $pattern, $value ) {
    $escaped_pattern = str_replace( '#', '\\#', $pattern );
   
    return 1 === preg_match( '#' . $escaped_pattern . '#u', $value );
}

If I run my test values against this function, preg_match always returns false.如果我针对这个函数运行我的测试值, preg_match总是返回 false。 Unfortunately I don't know the first thing about regexes so I don't understand what this function is doing.不幸的是,我不知道关于正则表达式的第一件事,所以我不明白这个函数在做什么。 I've read the #u bit should be about unicode characters, but can't figure it out besides that.我读过#u位应该是关于 unicode 字符的,但除此之外无法弄清楚。 Can somebody please help?有人可以帮忙吗?

(The Wordpress docs for using a pattern are here , but I can't work anything out of that either). (使用模式的 Wordpress 文档在此处,但我也无能为力)。

Got it.知道了。 So apparently what's happening is that the rest_validate_json_schema_pattern function is adding the regex's delimiting characters, and since my regex already had delimiting characters, things were not working, so the solution is to provide the regex without any delimiting characters to begin with:显然,发生的事情是rest_validate_json_schema_pattern函数正在添加正则表达式的分隔字符,并且由于我的正则表达式已经有分隔字符,所以事情不工作,所以解决方案是提供没有任何分隔字符的正则表达式:

register_rest_route( $this->namespace, "/posts", [
   "methods" => "POST",
   "permission_callback" => "__return_true",
   "callback" => "create_post",
   "args" => [       
      "title" => [
         "type" => "string",
         "description" => "The posts's title",
         "required" => true,
         "pattern" => '^[A-Za-z0-9 ]+$' // <-- Regex without delimiting characters
      ]
   ]
]);

Confusing if you ask me, but it works!如果你问我会感到困惑,但它有效!

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

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