-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Missing attributes during conversion using an ActionFunction with morphism() #52
Comments
Hello @terlar, Thank you for the feedback. By design Without any source, keep the mapper to be reused later
Straight map when a schema and a source are provided
If you are not sure of the value of
Also this brings the opportunity for you to normalize the data you want to expose. You can force the mapping by spreading the input: https://stackblitz.com/edit/typescript-nxc4hj
What would you expect as a return value if |
I think if the type declaration is optional it should act as if the value was not provided and omit the key completely, e.g.:
However it would be nice if you could enforce the types if they are not optional, e.g.:
In the cases of non-optional types I do agree that there is not default fallback and should probably error out instead, or force the transformation to specify how to handle that. The problem with the functions although i understand from an implementation behavior how that is neat. But in the final transformation if you did some mistake or whatever, a function is never the correct value? At least not when working with plain data. |
Yes I do agree with this one. I won't be able to know at runtime if the field is optional from TypeScript , but I can definitely omit the key if the transformation returns an undefined value. I will create a PR for that 👍
Do you use the last version of |
I installed it via NPM this weekend, so it should have been the latest published at that point. I saw your road-map and it looks promising. Looking forward to use this more, all really good ideas. I think this has the best TypeScript integration what I have seen. I have been running into things complaining due to missmatching types which is good. Perhaps add the possibility to apply some transformation on the final result. I know I can do it on the result from |
Thank you so much for the kind words and the feedback 🙏🏽
I am not sure I understood this correctly but you actually have the ability to use Morphism on Objects and Arrays, the rule is it will always respect the input dimension: e.g.
I will work on stripping the undefined keys starting next week I think, thank you for the suggestion! :) |
You are welcome. Yes, you understood me partly, sorry for being a bit unclear. I see now that it was just a blob of text and not very structured. That is what I meant, since it supports both arrays and direct objects. If you want to apply some function to the transformation you have to be aware of the result being worked on is an array or not and if it is an array you have to manually map as shown. My thought was what the procedure was if you always wanted some post-processing of your morphism transformation, per object level. I am not sure there is a strong use case, but such as thing as removing null values, etc. could be a thing for the post processing step. |
@terlar I made a little fix to avoid getting a function instead of a undefined property in the case of mapping a undefined data source https://repl.it/@yrnd1/Morphism-undefined-data-source This fix is available with version 1.9.2 I will add some options along the schema in order to apply some strategies on null and/or undefined values. I will think about your suggestion on some processing steps as well. |
@emyann Nice! Thanks, this made me be able to replace a pattern where I had a In this case I would still prefer to have the key omitted, but I guess I could have some manual post-processing. So would appreciate some options like that, let me know and I will test it :) |
@terlar Thank you for your feedback !I did release a new version 1.10 with which it is possible to either strip the key when the value is undefined or provide a default value to replace undefined ones. To do so it is now possible to create a schema with options using the Strip undefined valueimport { createSchema, morphism } from 'morphism'
const schema = createSchema<Target>({ keyA: 'somepath' }, { undefinedValues: { strip: true } })
// => {} Default value fallbackimport { createSchema, morphism } from 'morphism'
const schema = createSchema(
{ keyA: 'somepath' },
{
undefinedValues: {
strip: true,
default: () => null
}
}
);
// => { keyA: null } Here's a playground if you want to see it working: https://repl.it/@yrnd1/Morphism-undefined-values Please let me know what you think about it and don't hesitate if you have questions :) |
Sorry for the delay, I didn't have time to update my implementation to use this until now. Just want to return here to say thank you and I am very satisfied with the current behavior. Keep up the good work 👍 |
I really appreciate your feedback @terlar! Thank you and let me know if you need further support :) |
If I have a definition such as:
Then the following function call will give:
I can wrap this in something like:
However it would be nice if the morphism never resulted in a function even with missing values. What is the strategy when working with optional values?
The text was updated successfully, but these errors were encountered: