Giter VIP home page Giter VIP logo

Comments (4)

nitin786786 avatar nitin786786 commented on August 26, 2024 1

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.

emyann avatar emyann commented on August 26, 2024

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.

emyann avatar emyann commented on August 26, 2024

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.

emyann avatar emyann commented on August 26, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.