a fp-inspired utility for generating schema estimates with a bunch of sample input data. useful for generating zod (or similar) schemas.
say some api (of Star-Trek characters, for example) returns data like:
[
{ "name": "Geordi", "position": 3 },
{ "name": "Tasha", "position": 4 },
{ "name": "Q" }
]run node dist/cli.cjs example/trek.json and then it will output an ideal object:
{
"name": {
"__stats": {
"sum": 3,
"types": ["string"],
"enums": { "values": ["Geordi", "Tasha", "Q"], "sum_uniq": 3 },
"freq": 100
}
},
"position": {
"__stats": {
"sum": 2,
"types": ["number", "undefined"],
"enums": { "values": [3, 4, null], "sum_uniq": 3 },
"freq": 66
}
}
}this ideal object would be a single object that has all the object keys as the input objects plus a __stats property with some helpful information that will guide the creation of a schema.
in the case here we see:
- a
nameproperty that has"freq": 100which means it appeared in all example input, so it's not optional! it also has a single type"types": ["string"],of string. - a
positionproperty that has"freq": 66, so some objects from the api are missing this, so it's optional. it also appears to be a number type"types": ["number", "undefined"],.
so then a zod schema like this could be constructed:
import { z } from "zod";
const trekSchema = z.array(
z.object({
name: z.string(),
position: z.number().optional(),
})
);