-
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
Morphism over a array ignore element #115
Comments
Hi @SamuelColacchia, so happy you're having a good time using Do you want to strip a specific property, or the entire object ? As you already mentioned this is not actually possible to skip a complete object during the transformation. #85 will be about to strip a specific key of an object but your suggestion looks a lot like #35 (comment). I was thinking about some workarounds and ended up with this solution https://repl.it/@yrnd1/morphism-strip-key interface Source {
sku: string
}
interface Target {
sku?: string | null
}
const schema = createSchema<Target, Source>({
sku: orderItem => {
if (orderItem.sku === 'good') {
return orderItem.sku;
} else if (orderItem.sku === 'bad') {
return; // returning undefined will skip this property from the final object
} else {
return null
}
}
}, { undefinedValues: { strip: true } });
const result = morphism(schema, [{ sku: 'good' }, { sku: 'bad' }])
// => [ { sku: 'good' }, {} ] Using the options of the schema to strip the I guess your approach is actually to apply a filter after the transformation has been done in order to filter out the objects you don't want ? If the workaround above along with a filter does not answer to your question, I could think about 2 solutions: 1. Using a constant signaling the object / key should be stripped
2. Using a post processing predicate
I kinda feel the second solution is more flexible since it gives the consumer the ability to define on which criteria an object is stripped, but I'm curious about the solution you would pick ? 🙂 |
Hi @emyann, Thanks for the detailed post and examples!
Remove the entire object
Yes, exactly! I have been thinking of helper functions to write to each this goal. My current idea is fairly simple and i think very crude, a recursive function which loops through a given object and if it encounters a REMOVE_OBJECT const value then it removes the current object. If it is in a array it is removed from the array, if it encounters REMOVE_OBJECT in a root object it returns undefined.
Your 2nd ideal seems brilliant! It provides the most flexibility and it overall very robust. I may have to write a helper function to use with my schemas following that idea. So to talk about your 2nd idea in practice. It would be a search predicate which would? Loop through all values of a given object, going how deep? (First layer, deep, configurable depth.) If the predicate returns true then remove the whole object, if in array then removes from array, if in root object does what? (returns undefined, returns configurable value, throws error.) Side Thought This maybe something that is better suited to a seperate issue but what are your thoughts on morphism actions effect other morphism actions or to say it another way action dependency? For example: createSchema({
group: async target => {
const isGroupSpecial: boolean = await checkIfGroupIsSpecial(target.group);
if (isGroupSpecial) {
// group is special 😄
return target.group;
} else {
// group is no special 😦
return 'notspecial group';
}
},
dependsOnGroupValue: target => {
/**
* I depend on the result of group I have a few options:
* 1: I repeat the code that ran in group so i get the same result
* - Duplicate code, make function instead
* - multiple async calls, maybe cache the data some how?
* 2: I somehow access the value of group, is this even possible?
* 3: My value gets set in a 2nd call to morphism
*/
};
}); Possible solutions
Solution one and two would require no change to morphism just a maybe creating a examples folder saying hey here is how you do dependent actions. Solution 3 seems to be most robust but also may require alot a work to implement. What do you think @emyann? |
Question
I have been messing around with morphism for the past few weeks and thus far it has been a delight to use!
While using it to morph a array of object I encountered a scenario where I would like to remove a object from the array. Is this possible to do in a schema ?
Information
Schema
Data
Current Result
We set the value of sku to something like null and then loop through the array and remove all object that contain a sku: null
Current Result Data
Ideal Result
Perhaps export a enum from morphism that signals this object should be removed. Perhaps this could be apart of #85?
Ideal Result Data
Version
morphism: 1.12.0
The text was updated successfully, but these errors were encountered: