Skip to content

Instantly share code, notes, and snippets.

@einarwh
einarwh / customer-schema-20260101.json
Created April 8, 2026 10:15
Customer Schema (first version), published 01.01.2026.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "/hellish-enterprise/customer-schema-20260101.json",
"title": "Customer",
"description": "Validation schema for customers.",
"type": "object",
"required": ["userId", "email", "birthDate"],
"properties": {
"userId": {
"type": "string",
@einarwh
einarwh / customer-schema-20260202.json
Created April 8, 2026 10:13
Customer Schema (second version), published 02.02.2026. Changed type of 'birthDate' from datetime to date. Added 'displayName'.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "/hellish-enterprise/customer-schema-20260202.json",
"title": "Customer",
"description": "Validation schema for customers.",
"type": "object",
"required": ["userId", "email", "birthDate", "displayName"],
"properties": {
"userId": {
"type": "string",
@einarwh
einarwh / customer-schema-20260303.json
Last active April 8, 2026 10:07
Customer Schema (third version), published 03.03.2026. Renamed 'userId' to 'userName' and added 'items' property.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "/hellish-enterprise/customer-schema-20260303.json",
"title": "Customer",
"description": "Validation schema for customers.",
"type": "object",
"required": ["userName", "email", "birthDate", "displayName", "items"],
"properties": {
"userName": {
"type": "string",
@einarwh
einarwh / JsonMorgue.linq
Last active April 8, 2026 10:00
Life in the JSON morgue.
void Main()
{
var john1 = new CustomerDto20260101
{
UserId = "john-1",
Email = "john.fst@foomail.com",
BirthDate = new DateTime(2001, 01, 01),
};
var john2 = new CustomerDto20260202
@einarwh
einarwh / Serialize.cs
Created March 20, 2026 13:56
Serialization with validation.
public string Serialize(Customer customer, string schemaName, JsonSchema schema)
{
var jsonDoc = JsonSerializer.SerializeToDocument(customer, GetSerializerOptions());
var evaluation = schema.Evaluate(jsonDoc.RootElement, GetEvaluationOptions());
var assembly = Assembly.GetAssembly(typeof(JsonSchema));
var fileVersion =
assembly
.GetCustomAttribute<AssemblyFileVersionAttribute>()?
.Version;
var validatorName = $"{assembly.GetName().Name}-{fileVersion}";
@einarwh
einarwh / pellegriff.json
Last active March 17, 2026 14:16
Valid cat
{
"name": "Pellegriff",
"age": 1,
"miceCaught": 0,
"espressosSpilled": 2,
"$schema": "/cat/schema-20260310.json",
"$validation": {
"tool": "JsonSchema.Net-9.1.3",
"timestamp": "2026-03-17T07:52:34.837129Z",
"result": true
@einarwh
einarwh / toValidatedJson.fs
Created March 16, 2026 12:25
Tagging JSON with schema.
let toValidatedJson (schemaText: string) (schemaName: string) (o: obj) =
let jsonDoc = JsonSerializer.SerializeToDocument(o, serializerOptions)
let schema = JsonSchema.FromText(schemaText)
let evalOptions = EvaluationOptions()
evalOptions.OutputFormat <- OutputFormat.Flag
evalOptions.RequireFormatValidation <- true
let results = schema.Evaluate(jsonDoc.RootElement, evalOptions)
if results.IsValid then
let jsonObj = JsonObject.Create(jsonDoc.RootElement)
@einarwh
einarwh / SelectNotNull.cs
Last active August 25, 2025 11:14
SelectNotNull
public static class Ext
{
public static IEnumerable<TR> SelectNotNull<T, TR>(this IEnumerable<T> source, Func<T, TR?> fn) where TR : struct
{
return source.Select(fn).Where(it => it != null).Cast<TR>();
}
public static IEnumerable<TR> SelectNotNull<T, TR>(this IEnumerable<T> source, Func<T, TR?> fn) where TR : class
{
return source.Select(fn).Where(it => it != null).Cast<TR>();
@einarwh
einarwh / Program.cs
Created May 5, 2025 09:52
FizzBuzz without conditionals.
namespace FizzBuzzEnumerable;
using System.Collections;
using System.Collections.Generic;
class Program
{
public class FizzBuzzEnumerable : IEnumerable<string>
{
public IEnumerator<string> GetEnumerator()
@einarwh
einarwh / Program.fs
Last active April 30, 2025 16:03
GET out of the way ASP.NET Core.
open System
open System.Threading.Tasks
open Microsoft.AspNetCore.Builder
open Microsoft.Extensions.Hosting
open Microsoft.AspNetCore.Http
let getHandler (ctx : HttpContext) : Task =
let routePath = ctx.Request.RouteValues["path"] :?> string
let nonNullPath = if routePath = null then "" else routePath
ctx.Response.WriteAsync(sprintf "Hello %s" nonNullPath)