fix(server): require at least one field to be set when updating memory (#27842)

* add zod util to require one field is set in some schemas. appy to update memory endpoint

* add test
This commit is contained in:
Freddie Floydd
2026-04-17 21:18:48 +01:00
committed by GitHub
parent 9d33853544
commit 6798d5df32
3 changed files with 28 additions and 8 deletions
+16
View File
@@ -32,6 +32,22 @@ export function IsIPRange(options?: IsIPRangeOptions) {
.refine((arr) => arr.every((item) => isIPOrRange(item, options)), 'Must be an ip address or ip address range');
}
/**
* Like z.object().partial(), but rejects objects where every field is undefined.
* Use for update/patch DTOs where at least one field must be provided.
*
* @example
* nonEmptyPartial({ name: z.string(), bio: z.string() }).meta({ id: 'UpdateDto' });
*/
export function nonEmptyPartial<T extends z.ZodRawShape>(shape: T) {
return z
.object(shape)
.partial()
.refine((data) => Object.values(data as Record<string, unknown>).some((value) => value !== undefined), {
message: 'At least one field must be provided',
});
}
/**
* Zod schema that validates sibling-exclusion for object schemas.
* Validation passes when the target property is missing, or when none of the sibling properties are present.