Skip to content

Instantly share code, notes, and snippets.

@rido-min
Created April 7, 2022 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rido-min/bb2afdd5dbf573100b107bbe0260b29d to your computer and use it in GitHub Desktop.
Save rido-min/bb2afdd5dbf573100b107bbe0260b29d to your computer and use it in GitHub Desktop.
ValidatePayload
{
"@context": [
"dtmi:dtdl:context;2",
"dtmi:iotcentral:context;2"
],
"@id": "dtmi:samplesv2:geoTypes;1",
"@type": "Interface",
"contents": [
{
"@type": "Telemetry",
"name": "myPoint",
"schema": "point"
},
{
"@type": ["Telemetry", "Location"],
"name": "myGeopoint",
"schema": "geopoint"
}
]
}
using Microsoft.Azure.DigitalTwins.Parser;
using Microsoft.Azure.DigitalTwins.Parser.Models;
using System.Text.Json;
string basePath = Path.Join(System.Reflection.Assembly.GetExecutingAssembly().Location + @"./../../../");
string ReadFile(string path) => File.ReadAllText(Path.Join(basePath, path));
string ToJs(object obj) => JsonSerializer.Serialize(obj);
var objectModel = new ModelParser().Parse(new string[] { ReadFile("geotypes-1.json") });
var rootInterface = (DTInterfaceInfo)objectModel[new Dtmi("dtmi:samplesv2:geoTypes;1")];
var myPoint = (DTTelemetryInfo)rootInterface.Contents["myPoint"];
var validPayload = myPoint.Schema.ValidateInstance(ToJs(new { type = "Point", coordinates = new double[] { 12.35, 23.56} }));
Console.WriteLine(validPayload);
var myGeopoint = (DTTelemetryInfo)rootInterface.Contents["myGeopoint"];
var validPayload2 = myGeopoint.Schema.ValidateInstance(ToJs(new { lat =12.35, lon = 23.56 }));
Console.WriteLine(validPayload2);
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>validate_payload</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.DigitalTwins.Parser" Version="6.1.0-preview" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment