zenstack icon indicating copy to clipboard operation
zenstack copied to clipboard

expose omitted fields in the zod schemas

Open svengau opened this issue 5 months ago • 3 comments

Thanks for this awesome library, I've got a huge schema, and it saved me hours of dev.

I would like to propose a small feature request to have a more flexible way to handle omitted fields.

When a field is omitted in the schema, it's only omitted in the MyModelScalarSchema, not in the MyModelCreateScalarSchema, or MyModelUpdateScalarSchema:

const baseSchema = z.object({ ...all fields...}).strict();
const relationSchema = z.object({ ...relations...});
const fkSchema = z.object({ ...fk fields...});
export const MyModelScalarSchema = baseSchema.omit({
  omittedField: true,
  ....
});
export const MyModelCreateScalarSchema = baseSchema.partial({
  ...fields with defaults...
});
export const MyModelCreateSchema =  MyModelCreateScalarSchema.merge(fkSchema);
export const MyModelUpdateScalarSchema = baseSchema.partial();
export const MyModelUpdateSchema = MyModelUpdateScalarSchema.merge(fkSchema.partial());

IMHO, it would make sense to also exclude them from the MyModelCreateSchema, and MyModelUpdateSchema.

But I understand it's a major change, so should it be possible to expose a variable named myModelOmittedFields or something like that, so we can call it anywhere we need it ?

export const myModelOmittedFields = {
  omittedField: true,
  ....
};

export const MyModelScalarSchema = baseSchema.omit(myModelOmittedFields);

svengau avatar Jul 25 '25 04:07 svengau

Hi @svengau , thank for filing this. The inconsistency feels like a bug to me and not by intention. I'll check why it's the case. Is it urgent for you?

ymc9 avatar Jul 27 '25 01:07 ymc9

@ymc9 thanks for your feedback. And for now I handle it manually so not urgent.

svengau avatar Jul 27 '25 05:07 svengau

@ymc9 I'm wondering if we don't have also an issue with fkSchema: an omitted field will finally be present in MyModelCreateSchema if it's a foreign key.

svengau avatar Jul 29 '25 11:07 svengau