I would like to make use of a external library to parse the core domain entities. Doing this in a naive way it would look like the following:
import { z } from 'zod';
const Audience = z.object({
name: z.string(),
id: z.string(),
});
type Audience = z.infer<typeof Audience>;
const buildMakeAudience = ({ getShortId }: { getShortId: () => string }) => {
return (input: Audience) => {
const { id, name } = Audience.parse(input);
return Object.freeze({
getId: () => id || getShortId(),
getName: () => name,
});
};
};
export { buildMakeAudience };
However this breaks the dependency rule and our entities now depend on the 3rd party lib directly. I am interested to know if it is possible to use a 3rd party lib when defining the schema and types of your core domain entities, or if its best to just validate them by hand?
I could of course inject the parse()
method, but to me this still feels heavily coupled to the external lib.