0

I am trying to validate the smart format object against the token present in the string.

Sample:-

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

Smart.Format("Your Id - {Id} for registration - {RegNo}", obj);

If I do not pass RegNo property/value in the object then smart format throws an error. Instead do we have any proper validation method to validate the tokens required against the object provided. Any help would be appreciated.

samssagar
  • 1
  • 4

1 Answers1

0

SmartFormat provides various extensions which allow to format a string depending on the provided arguments. Here is the example code for a missing "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"
axuno
  • 581
  • 4
  • 15