Comments (4)
Hi @emyann , thanks for the response and it looks good.
I have a query here that, in case of" interface keyA" if I want to give some other interface as a value to it's "key3" field then how can i achieve mapping at that level.
What i want to do is instead of
key3: (source: any) => [
{
key3A: source.somepath,
key3B: source.somepath
}
]
IN
const keyASchema: StrictSchema<KeyA> = {
key1: 'somepath',
key2: 'somepath',
key3: (source: any) => [
{
key3A: source.somepath,
key3B: source.somepath
}
],
key4: 'somepath'
};
I want to add mapping like
const keyASchema: StrictSchema<KeyA> = {
key1: 'somepath',
key2: 'somepath',
key3: someInterface
key4: 'somepath'
};
where
interface someInterface{
key3A: string,
key3B: number
}
from morphism.
Hello @nitin786786, Thank you for your feedback!
The properties values of the schema allow you to build complex structures but not in this way.
There are 4 possible values for the fields of your schema https://github.com/nobrainr/morphism#1-the-schema:
- ActionString(
field: 'somepath'
): A string that allows to perform a projection from a property - ActionSelector(
field: { path: 'somepath', fn: someCallback }
): An Object that allows to perform a function over a source property's value - ActionFunction(
field: (item)=>{}
): A Function that allows to perform a function over source property - ActionAggregator(
field: ['path', 'path2']
): An Array of Strings that allows to perform a function over source property
By using the ActionFunction
You can pretty much achieve what you want. However you'll need to break your schema in smaller parts.
Here is an example: https://stackblitz.com/edit/morphism-issues-50?file=index.ts
interface IFoo {
keyA: KeyA;
keyB: string;
keyC: number;
keyD: KeyD;
keyE: string;
}
interface KeyA {
key1: number;
key2: number;
key3: [
{
key3A: string;
key3B: number;
}
];
key4: string;
}
interface KeyD {
key1: string;
key2: {
key2A: string;
};
key3: string;
}
const keyASchema: StrictSchema<KeyA> = {
key1: 'somepath',
key2: 'somepath',
key3: (source: any) => [
{
key3A: source.somepath,
key3B: source.somepath
}
],
key4: 'somepath'
};
const keyDSSchema: StrictSchema<KeyD> = {
key1: 'sourcepath',
key2: (source: any) => ({
key2A: source.somepath
}),
key3: 'sourcepath'
};
const schema: StrictSchema<IFoo> = {
keyA: source => morphism(keyASchema, source),
keyB: 'somepath',
keyC: 'somepath',
keyD: source => morphism(keyDSSchema, source),
keyE: 'somepath'
};
const someSourceObject = { somepath: 'value' };
const target = morphism(schema, someSourceObject);
I needed to break down your interface to use StrictSchema<KeyD>
and StrictSchema<KeyA>
to enforce the types, because I found an issue with nested schemas https://stackblitz.com/edit/morphism-issues-50-nested?file=index.ts but they should not be mandatory, the return type of the ActionFunction should be inferred.
Also I might think to implement what you tried as a feature, because I like the idea. But first please let me know what you think about my suggestion above.
from morphism.
At this moment, to achieve that you would need to create a schema for key3
interface someInterface{
key3A: string,
key3B: number
}
cont interfaceSchema: StrictSchema<someInterface> = {
key3A: 'somepath',
key3B: 'somepath'
}
const keyASchema: StrictSchema<KeyA> = {
key1: 'somepath',
key2: 'somepath',
key3: source => morphism(interfaceSchema, source),
key4: 'somepath'
};
The thing is that we can't use a TypeScript type as a value at runtime to figure out what field are available to map to.
This is somewhat possible when you use an ES6 Class, we are able to read the fields from there to compute a schema (see Class Objects
in the documentation). But I don't recommend to use the registry (https://github.com/nobrainr/morphism#registry-api), this is going to be deprecated in v2
from morphism.
@nitin786786 This feature is now available with the latest version of Morphism 1.9.1
. Here's a working playground of your use case: https://repl.it/@yrnd1/Morphism-Complex-Object-using-StrictSchema
Please let me know if you have feedbacks about it :)
from morphism.
Related Issues (20)
- Mocking `morphism` named export HOT 2
- [Question] How to construct an object with projection HOT 3
- [Bug] Inferred types break with certain target types HOT 2
- Spying on morphism with ts-mockito ? HOT 6
- Difference between an object with type and using createSchema HOT 2
- Failing to change types of target schema (TypeScript) HOT 3
- Dealing with undefined source ? HOT 4
- A little help with flattening ? HOT 9
- [Question] Is there an easy way to apply the identity morphism? HOT 4
- A merge method for copying properties with mappings HOT 5
- [Question]: Who is using the Registry to store the mappers ?
- Map array of objects? HOT 1
- Help needed to flatten nest array within a object
- Generics do not work properly with typescript HOT 3
- Morphism playground doesnt work
- Mapping with runtime arguments?
- mapping array without functions
- [Question] Supporting circular references in destination types
- How to map nested types HOT 2
- Still being updated? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from morphism.