Zod

Zod #

Define a schema for any non-primitive object #

import { z } from 'zod';

const mySchema = z.record(z.unknown());

Defining required attributes while allowing additional unknown properties #

import { z } from 'zod';

const mySchema = z.object({
    requiredAttribute: z.string(), 
    anotherRequiredAttribute: z.number()
}).passthrough();

Attribute should be an empty object and not a typescript {} which allows any object #

import { z } from 'zod';

const mySchema = z.object({
    myObj: z.record(z.never())  
})

Attributte should be optional even when default is used. Use z.input instead of z.infer #

import { z } from 'zod';

const mySchema = z.object({
    color: z.string().default('red').optional();
})

export type MySchemaInput = z.input<typeof mySchema>;