- Published on
Reading a Schema You Do Not Own
- Authors

- Name
- Mathias Hove
- @mathias_hove
If you take a product feed with a field available_from. It tells us when a product goes on sale. The value looks something like this: 2026-11-20T09:00:00+01:00.
Suddenly one day the source starts sending something like this: 2026-11-20T09:00:00. No offset?! DateTime.Parse normally just use the local timezone on the machine it is hosted on. In this case Denmark is an hour ahead of UTC in winter and it ends up going on sale at 10 instead of 9.
There are no errors and nothing fails. The import runs and thinks everything works as expected. But the error is on the product on the site. It is an hour late and no one has noticed before a customer asks.
Often we do not have control of these kinds of feeds, and we cannot stop it from changing. However we can write some assumptions that has to be satisfied. In this case a offset of the timezone.
The reader knew something it never said
The way we can do this is write a rule down and let the reader check every record:
| Source path | Target | Transform | Required |
|---|---|---|---|
| product.id | ProductId | text | yes |
| product.available_from | AvailableFrom | instant | yes |
| product.brand | Brand | text | no |
Each row states where the value is and how we can read it. Also it dictates if it is required or not. Instant is in this case just a name of a transform, which accepts a date, time and a offset. If the offset is not there, then the transform will fail and the record is rejected before it gets imported. We are no longer going to guess anything.
The code
public sealed record FieldMapping(string Path, string Target, string Transform, bool Required);
public sealed record Problem(string Path, string Detail);
public sealed record MappedRecord(
IReadOnlyDictionary<string, object> Values,
IReadOnlyList<Problem> Rejections) // required and missing, or any value that cannot be read
{
public bool Accepted => Rejections.Count == 0;
}
public static class Mapper
{
static readonly Dictionary<string, Func<JsonElement, object>> Transforms = new()
{
["text"] = e => e.GetString()!.Trim(),
["instant"] = e => DateTimeOffset.ParseExact(
e.GetString()!, "yyyy-MM-dd'T'HH:mm:sszzz", CultureInfo.InvariantCulture),
};
public static MappedRecord Apply(JsonElement source, IReadOnlyList<FieldMapping> mappings)
{
var values = new Dictionary<string, object>();
var rejections = new List<Problem>();
foreach (var m in mappings)
{
if (!TryFind(source, m.Path, out var raw) || raw.ValueKind == JsonValueKind.Null)
{
if (m.Required) rejections.Add(new(m.Path, "missing"));
continue;
}
try { values[m.Target] = Transforms[m.Transform](raw); }
catch (Exception e) when (e is FormatException or InvalidOperationException)
{
rejections.Add(new(m.Path, e.Message));
}
}
return new(values, rejections);
}
static bool TryFind(JsonElement node, string path, out JsonElement found)
{
found = node;
foreach (var part in path.Split('.'))
{
if (found.ValueKind != JsonValueKind.Object || !found.TryGetProperty(part, out found))
return false;
}
return true;
}
}
If a required value is missing, a record is rejected. Each rejection shows details information of the path and what is wrong with it.
If we run the date through it, 2026-11-20T09:00:00+01:00 is accepted and 2026-11-20T09:00:00 is rejected. The message names product.available_from. 2026-11-20T08:00:00Z is rejected.
Why this is worth doing
It is cheap and it's written once. I use them for important transforms when i depend on a specific structure in a value.
It finds the problem at the right time and it's much easier to track and actually do something about it.
It keeps the list of what I depend on true. A complete list of the fields I use and what I expect from each. It won't go stale, since the reader runs it on every record. When the supplier announces a change, I can open it and I would know in a minute whether it concerns me.