I’d like to validate untrusted config JSON submitted to my application against a TypeScript interface. If it’s bad, I’d like to serialize a TypeScript object with default values, and suggest it as a pattern.

Last time I looked, TypeScript didn’t provide runtime access to types and interfaces, so I’m not clear if that’s possible without build-time tomfoolery.

I’d prefer to avoid JSON schemas if I can, but I guess that’s an option too.

Are there libraries or new-ish language features that I could use?

  • Foster Hangdaan@lemmy.hangdaan.com
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    3 days ago

    Try Valibot. It is inspired by Zod.

    According to the Valibot FAQ, the main difference with Zod is:

    The functionality of Valibot is very similar to Zod. The biggest difference is the modular design of our API and the ability to reduce the bundle size to a minimum through tree shaking and code splitting. Depending on the schema, Valibot can reduce the bundle size up to 95% compared to Zod. Especially for client-side validation of forms and serverless environments this can be a big advantage.