如何验证字符串 属性 在 Logic Apps Liquid 模板上是否为数字?
How to validate if a string property is numeric on a Logic Apps Liquid template?
我正在创建一个 Liquid 模板,我需要验证 JSON 有效负载中的字符串 属性 是否为数字(仅包含数字)。
我已经尝试实施建议的提示 here
{% assign test = "A string" | plus: 0 %}
{{ test }}
{% assign test_d = "123456" | plus: 0 %}
{{ test_d }}
{{ test }} will print 1 while {{ test_d }} will print 123456.
So you could do a check:
{% if test_d != 0 %}
{{ "I'm a Int!"}}
{% else %}
{{ "I'm a String!"}}
{% endif %}
和here。
When using assign, either of these options should give you a number:
{% assign var1 = var1 | plus: 0 %}
{% assign var2 = var2 | times: 1 %}
但是,它们在 DotLiquid 实现中的工作方式似乎不同。
这是我使用这些技巧创建的模板。
{
{% assign numValue1 = content.myDoc.myProperty | Plus: 0 %}
{% assign numValue2 = content.myDoc.myProperty | Times: 1 %}
{% assign numValue3 = content.myDoc.myProperty | Times: 2 %}
{% if numValue1 != 0 %}
"validation1": true,
{% else %}
"validation1": false,
{% endif %}
"numValue1": "{{numValue1}}",
"numValue2": "{{numValue2}}",
"numValue3": "{{numValue3}}"
}
但是,过滤器 Plus: 0
将字符“0”连接到字符串,而不是像 Ruby 实现中描述的那样表现。 Times
重复字符串,而不是按照建议返回数字。
这是我的 属性 为 12345
时的输出
{
"validation1": true,
"numValue1": "123450",
"numValue2": "12345",
"numValue3": "1234512345"
}
这是我的 属性 为 ABC123
时的输出
{
"validation1": true,
"numValue1": "ABC1230",
"numValue2": "ABC123",
"numValue3": "ABC123ABC123"
}
我知道 DotLiquid 实现与 Ruby 不完全相同。我已经检查了 DotLiquid 的源代码,并且这些过滤器按照它们在我的测试中的行为进行了编码。
有什么建议吗?
在查看 DotLiquid 代码并检查所有过滤器后,我想出了这个解决方案。它不是很优雅,但它完成了工作:)
{
{% assign nonNumericCharsInMyProperty = content.myDoc.myProperty | Remove: "0" | Remove: "1" | Remove: "2" | Remove: "3" | Remove: "4" | Remove: "5" | Remove: "6" | Remove: "7" | Remove: "8" | Remove: "9" %}
{%- if content.myDoc.myProperty == '' -%}
"isValid": false,
"message": "Invalid Message. myProperty cannot be empty."
{%- elseif nonNumericCharsInCardNumber != '' -%}
"isValid": false,
"message": "Invalid Message. myProperty must be numeric."
{%- else -%}
"isValid": true,
"message": ""
{%- endif -%}
}
我正在创建一个 Liquid 模板,我需要验证 JSON 有效负载中的字符串 属性 是否为数字(仅包含数字)。
我已经尝试实施建议的提示 here
{% assign test = "A string" | plus: 0 %}
{{ test }}
{% assign test_d = "123456" | plus: 0 %}
{{ test_d }}
{{ test }} will print 1 while {{ test_d }} will print 123456. So you could do a check:
{% if test_d != 0 %}
{{ "I'm a Int!"}}
{% else %}
{{ "I'm a String!"}}
{% endif %}
和here。
When using assign, either of these options should give you a number:
{% assign var1 = var1 | plus: 0 %}
{% assign var2 = var2 | times: 1 %}
但是,它们在 DotLiquid 实现中的工作方式似乎不同。
这是我使用这些技巧创建的模板。
{
{% assign numValue1 = content.myDoc.myProperty | Plus: 0 %}
{% assign numValue2 = content.myDoc.myProperty | Times: 1 %}
{% assign numValue3 = content.myDoc.myProperty | Times: 2 %}
{% if numValue1 != 0 %}
"validation1": true,
{% else %}
"validation1": false,
{% endif %}
"numValue1": "{{numValue1}}",
"numValue2": "{{numValue2}}",
"numValue3": "{{numValue3}}"
}
但是,过滤器 Plus: 0
将字符“0”连接到字符串,而不是像 Ruby 实现中描述的那样表现。 Times
重复字符串,而不是按照建议返回数字。
这是我的 属性 为 12345
{
"validation1": true,
"numValue1": "123450",
"numValue2": "12345",
"numValue3": "1234512345"
}
这是我的 属性 为 ABC123
{
"validation1": true,
"numValue1": "ABC1230",
"numValue2": "ABC123",
"numValue3": "ABC123ABC123"
}
我知道 DotLiquid 实现与 Ruby 不完全相同。我已经检查了 DotLiquid 的源代码,并且这些过滤器按照它们在我的测试中的行为进行了编码。
有什么建议吗?
在查看 DotLiquid 代码并检查所有过滤器后,我想出了这个解决方案。它不是很优雅,但它完成了工作:)
{
{% assign nonNumericCharsInMyProperty = content.myDoc.myProperty | Remove: "0" | Remove: "1" | Remove: "2" | Remove: "3" | Remove: "4" | Remove: "5" | Remove: "6" | Remove: "7" | Remove: "8" | Remove: "9" %}
{%- if content.myDoc.myProperty == '' -%}
"isValid": false,
"message": "Invalid Message. myProperty cannot be empty."
{%- elseif nonNumericCharsInCardNumber != '' -%}
"isValid": false,
"message": "Invalid Message. myProperty must be numeric."
{%- else -%}
"isValid": true,
"message": ""
{%- endif -%}
}