The JSON Schema to normalize
Filter options for selecting/including/omitting properties
A normalized schema with all refs resolved and filters applied
// Without type parameter (backward compatible)
const schema = {
type: "object",
properties: {
name: { type: "string" },
tags: {
type: "array",
items: { $ref: "#/$defs/Tag" }
}
},
$defs: {
Tag: {
type: "object",
properties: {
label: { type: "string" },
"@id": { type: "string" }
}
}
}
};
const normalized = normalizeSchema(schema, {
include: { tags: { take: 10 } },
includeRelationsByDefault: false
});
// With Zod type inference for type safety
import { z } from 'zod';
const zodSchema = z.object({
name: z.string(),
tags: z.array(z.object({ label: z.string() }))
});
type MyType = z.infer<typeof zodSchema>;
const normalized2 = normalizeSchema<MyType>(schema, {
include: { tags: { take: 10 } } // Type-safe: only valid keys allowed
});
Normalizes a JSON Schema by resolving all $refs and applying filter options
This is a two-phase process: