Merging Object & Record #3723
Unanswered
jonathanstanley
asked this question in
Q&A
Replies: 1 comment 1 reply
-
The best I can think of: let staticColSchema = z.object({
Title: z.string(),
Price: z.number(),
Published: z.boolean()
});
let uploadSchema = staticColSchema
.passthrough()
.transform((row) => {
return Object.fromEntries(
// use filter to strip disallowed properties
Object.entries(row).filter(
([key, value]) =>
// allow all statically defined elements
Object.keys(staticColSchema.shape).includes(key) ||
// allow properties that have valid title and value type
(key.startsWith('Attribute') && z.string().safeParse(value).success)
)
);
})
.array(); ... but the parsed type is bad |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'd like to parse a spreadsheet provided by users. A product spreadsheet could look like this:
I want to allow users to upload any number of attributes (ex: Attribute3, Attribute4, ...Attribute999).
individually, the schemas are simple enough
however, I need a schema for the entire spreadsheet
Doesn't work. For example,
record
will not strip out invalid keys; instead if fails.catchall
cannot specify keysintersection
doesn't work because not all columns have that same record typePossible solutions I've thought about:
object()
andrecord()
parsing fails #2195z.object
to be mergeable withz.record
, or ability to define keys by a custom type #2200 (comment)Beta Was this translation helpful? Give feedback.
All reactions