Skip to content

Latest commit

 

History

History
29 lines (21 loc) · 828 Bytes

check-if-an-object-is-empty-with-zod.md

File metadata and controls

29 lines (21 loc) · 828 Bytes

Check If An Object Is Empty With Zod

Zod is a schema validation library. It can be used to check all sorts of properties about the data moving through our system.

Let's look at how to implement a common type of check -- is this object empty?

import {z} from 'zod';

const emptyObjectSchema = z.object({}).strict();
const isEmpty = (obj: object): boolean => {
  const result = emptyObjectSchema.safeParse(obj);
  return result.success;
}

isEmpty({});
//=> true

isEmpty({ hello: 'world' });
//=> false

This emptyObjectSchema strictly defines the schema as an empty object ({}). Without the strict() part, we'd be allowing an object with key-value pairs to quietly pass the validation.

source