Open akutruff opened 1 year ago
This is great
Definitely on the list
If the property mappers from z.mapProps
had the signature that closely matched the Array.map
function, we'd have a lot more power. Something like the following.
type PropertyMapper = <
Type extends object,
Key extends keyof Type
>(
value?: Type[Key],
key?: Key,
parent: Type
): any
Person.mapProps({
name: (value) => value.default(''),
title: (value) => value.default(''),
fullSigniture: (, , { name, title }) => [ name, title ].filter(Boolean).join('; ')
})
mapProps
could also receive a PropertyMapper
function directly, where it would iterate over the keys, and call the PropertyMapper
function for each, resulting in an array. I expect this to be more used on z.record()
types.
Also, while I'm at it, can I add optional VSCode dev container support to the project? It really makes contributing to projects so much easier as it isolates your environment. It would be a separate PR.
(Removed previous comment. ) I'm actually now not following what fullSignature would do in this case. I thought I did, but what does the array mean here? Are the decomposed parameters the value to be parsed and not the types? Initially I thought name and title were the types.
Are you mixing transforms with type pipelining?
@Ustice At present, the design of mapProps
function doesn't extend the source object. It restricts it to just existing properties so that you are guaranteed that your property names do not deviate as you make changes.
For input validation, Zod sometimes feels like it's copy/paste error prone and feels "reversed." Often, you validate inputs that are going to be sent to another model type that will be written to an API or database. The types of the properties of the inputs and the destination type usually have the same data type, or only a few properties on the input need to be coerced, refined, or transformed before simply being copied to another type. I would also like the some of the errors on the model type to be surfaced immediately before any other logic takes place.
Also, autocomplete is greatly hindered as making sure the property names and property types stay in sync is difficult.
I based a lib I wrote almost entirely off Zod, and added this functionality and it's been a big help. You can see the documentation which mirrors Zod's docs, here:
https://github.com/kutruff/nimbit#mapprops
I'd love for this functionality to also be in Zod.
Now imagine if we had
mapProps()
that allowed us on a property by property basis to work with the first type:mapPropsPicked()
would do the same but restrict the properties would only allow the properties that were specified in the mapping.