在Bicep或ARM中,如何检测策略规则(policyRule)部分中只包含空格的参数值?

huangapple go评论53阅读模式
英文:

In Bicep or ARM, how do I detect parameter values that are just whitespace in the policyRule section?

问题

我正在使用Bicep,但我理解旧的ARM JSON语法足够适应解决方案,如果有人了解其中一个但不了解另一个。无论如何,我可以轻松地通过在我的policyRule对象中执行以下操作来判断参数值是否为空字符串:

policyRule: {
  if: {
    {
        field: '[concat('tags[', parameters('tagName'), ']')]'
        equals: ''
    }
  }
...其余的规则和语句
}

或者使用 exists: false 来测试存在性。

但如何检查值是否只是一堆空格,例如 " "?我尝试将field的值修改为这样:

[trim(concat('tags[', parameters('tagName'), ']'))]

但似乎不起作用。我仍然可以输入一堆空格并使其通过验证。

英文:

I'm using Bicep, but I understand the older ARM JSON syntax enough to adapt a solution if someone knows one but not the other. Anyway, I can easily tell if a parameter value is an empty string by just doing this inside of one of my policyRule objects:

policyRule: {
  if: {
    {
	    field: '[concat(\'tags[\', parameters(\'tagName\'), \']\')]'
        equals: ''
    }
  }
...rest of rule and then statement
}

or testing existence with exists: false

But how would I check if the value is just a bunch of whitespaces like " "? I tried modifying the field: value to be this:

[trim(concat(\'tags[\', parameters(\'tagName\'), \']\'))]

But it doesn't appear to work. I can still enter a bunch of spaces and have it pass verification.

答案1

得分: 1

Trim 仅删除前导和尾随空白,我也给出了与您使用的相同的内容。

这将在检查参数值是否为空之前删除参数值中的任何前导或尾随空格。如果参数值仅包含空白字符,则 trim 函数将将其转换为空字符串,触发 equals: " 条件并导致策略检查失败。

policyRule: { 
if: { 
{
 field: '[trim(concat(\'tags[\', parameters(\'tagName\'), \']\'))]' 
 equals: ''
  } 
 }
 }

但是,您需要使用 replace 函数与正则表达式从参数值中删除所有空格字符。

您可以按以下方式修改策略规则:

policyRule: {
 if: {
  {
   field: '[replace(concat(\'tags[\', parameters(\'tagName\'), \']\'), \'\\s\', \'\')]' 
    equals: ''
    } 
  }
  }
英文:

Trim removes only leading and trailing whitespace and I also gave the same one as you've used.

This will remove any leading or trailing whitespace from the parameter value before checking to see if it is empty. If the parameter value only contains whitespace characters, the trim function will convert it to an empty string, triggering the equals: " condition and failing the policy check.

policyRule: { 
if: { 
{
 field: '[trim(concat(\'tags[\', parameters(\'tagName\'), \']\'))]' 
 equals: ''
  } 
 }
 }

But, you need to use the replace function with a regular expression to remove all whitespace characters from the parameter value.

You can modify the policy rule as shown below:

policyRule: {
 if: {
  {
   field: '[replace(concat(\'tags[\', parameters(\'tagName\'), \']\'), \'\\s\', \'\')]' 
    equals: ''
    } 
  }
  }

huangapple
  • 本文由 发表于 2023年3月15日 20:30:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定