简体   繁体   English

根据字符串中可用的令牌验证智能格式 object

[英]Validate Smart Format object against the token available in the string

I am trying to validate the smart format object against the token present in the string.我正在尝试根据字符串中存在的令牌验证智能格式 object。

Sample:-样本:-

object obj = {"Id": "1", "RegNo": "REG123"} object obj = {"Id": "1", "RegNo": "REG123"}

Smart.Format("Your Id - {Id} for registration - {RegNo}", obj); Smart.Format("你的 Id - {Id} 用于注册 - {RegNo}", obj);

If I do not pass RegNo property/value in the object then smart format throws an error.如果我没有在 object 中传递 RegNo 属性/值,则智能格式会引发错误。 Instead do we have any proper validation method to validate the tokens required against the object provided.相反,我们是否有任何适当的验证方法来根据提供的 object 验证所需的令牌。 Any help would be appreciated.任何帮助,将不胜感激。

SmartFormat provides various extensions which allow to format a string depending on the provided arguments. Here is the example code for a missing "RegNo". SmartFormat提供各种扩展,允许根据提供的 arguments 格式化字符串。以下是缺少“RegNo”的示例代码。 Still, this is not "validation", but rather "conditional formatting".不过,这不是“验证”,而是“条件格式”。

// Create a formatter with necessary extensions, including NewtonsoftJsonSource
var smart = new SmartFormatter(settings ?? new SmartSettings())
    .AddExtensions(new NewtonsoftJsonSource(), new DefaultSource())
    .AddExtensions(new NullFormatter(), new DefaultFormatter());
// Parse the JSON input
var jObject = JObject.Parse(@"{""Id"": ""1"", ""RegNo"": null}");
var result = smart.Format("Id: {Id:isnull:Value is Null|{}} - RegNo: {RegNo:isnull:Value is Null|{}}", jObject);
// Result: "Id: 1 - RegNo: Value is Null"

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

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