0

I'm looking for a way to validate dynamic objects. The dynamic objects are used in an internal API. I know that I can

  1. convert to XML and use XSD for validation
  2. convert to JSON and use Json.NET .IsValid(schema) or the Json.NET Schema package
  3. validate each property in code like HasProperty as shown here

The XML and JSON based approaches requires serialization and deserialization and this will impact performance when used frequently. The code based validation is difficult to maintain (in particular for hierarchical objects) and can't be used easily by API consumers.

I was looking to find something similar to XSD validation but natively for dynamic objects but had no luck. I'm fine if it is limited to ExpandoObjects.

My minimum requirements:

  1. identify missing required properties
  2. identify properties which are not specified
  3. allow optional properties
  4. allow nested objects
Klaus P.
  • 43
  • 8
  • `XmlSerializer` does not support `ExpandoObject` -- because it does not support dictionaries. Only option is to implement `IXmlSerializable` which is burdensome. `DataContractSerializer` can be made to serialize expandos but apparently the output format is not ideal, see e.g. [XML Serialize dynamic object](https://stackoverflow.com/q/7501846/3744182). All that argues against validating an expando by transforming to XML. – dbc Aug 11 '21 at 18:30
  • On the other hand Json.NET serializes and deserializes `ExpandoObject` quite readily, so a [tag:jsonschema] schema may work for you depending on your requirements. Not sure that [JSON Schema](https://json-schema.org/) has all the functionality of an XSD schema though, you will have to test to find out. – dbc Aug 11 '21 at 18:35
  • Also, by default XML schemas are order-sensitive (and require extra work to make them unordered) while JSON objects are defined to be unordered and so JSON object schemas are also. When working with expandos you probably want unordered validation, but have you defined your requirements here? – dbc Aug 11 '21 at 18:38
  • Order-sensitive validation is not an issue, if the validation is easily available to the consumer of the API. I looked already at JSON schema and the output of the DataContractSerializer example and didn't find these ideal, but I am more concerned about the impact on performance of serialization and deserialisation. – Klaus P. Aug 11 '21 at 19:10
  • Then take a look at https://ericlippert.com/2012/12/17/performance-rant/. Long and short: try it yourself to see if the performance is good enough. Also, [questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow](https://stackoverflow.com/help/on-topic) so a general question as to which approach is best may get closed as off topic. – dbc Aug 11 '21 at 19:27
  • This is why I wrote that I'm "looking to find something similar to XSD validation but natively for dynamic objects" - that means without serialization / deserialization. If there ain't such thing, that's ok - in this case JSON Schema might be best. Appreciate your help. – Klaus P. Aug 11 '21 at 19:34
  • An expando object is basically a `Dictionary under the covers. Build a little tool that walks through the properties once, checking off all of your checks. It shouldn't be too hard. – Flydog57 Aug 11 '21 at 20:15

0 Answers0