OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Node.js automapper-ts package not mapping nested object/properties

  • Thread starter Thread starter Ryan Fisch
  • Start date Start date
R

Ryan Fisch

Guest
When I run through the following code the "card" node fails to load in the "source" field as a nested entity. I reviewed the source code which states you can map from 'nested.property' which is supposed to create that object and property in the project destination but it doesn't appear to be working for me. Any help would be much appreciated.

Code:
var jsf = require('json-schema-faker');
var automapper = require('automapper-ts').automapper;

var schema = {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "name": "customer",
    "title": "customer",
    "description": "Canonical Customer",
    "type": "object",
    "properties": {
        "email": {
            "type": "string",
            "format": "email"
        },
        "description": {
            "type": "string"
        },
        "phone": {
            "type": "string"
        },
        "card": {
            "type": "object",
            "properties": {
                "name": {
                    "type": "string"
                },
                "expiryMonth": {
                    "type": "string",
                    "minLength": 2,
                    "maxLength": 2,
                    "pattern": "^(1[0-2])|([0][1-9])$"
                },
                "expiryYear": {
                    "type": "string",
                    "minLength": 4,
                    "maxLength": 4,
                    "pattern": "^20([1-9][5-9])$"
                },
                "number": {
                    "type": "string",
                    "pattern": "^(?:4[0-9]{12}(?:[0-9]{3})?|5[12345][0-9]{14}|3[47][0-9]{13}|3(?:0[012345]|[68][0-9])[0-9]{11}|6(?:011|5[0-9]{2})[0-9]{12}|(?:2131|1800|35[0-9]{3})[0-9]{11})$"
                },
                "cvc": {
                    "type": "integer",
                    "maximum": 9999
                },
                "zip": {
                    "type": "string"
                }
            },
            "required": [
                "name",
                "expiryMonth",
                "expiryYear",
                "number",
                "zip"
            ],
            "additionalProperties": false
        },
        "trialEnd": {
            "type": "number",
            "minimum": 1448420692,
            "maximum": 4098384000
        }
    },
    "required": [
        "email",
        "card"
    ],
    "additionalProperties": false
};

automapper.createMap('a', 'b')
    .forMember('description', function(opts) {opts.mapFrom('description');})
    .forMember('email', function(opts) { opts.mapFrom('email');})
    .forMember('source.name', function(opts) { opts.mapFrom('card.name');})
    .forMember('source.exp_month', function(opts){ opts.mapFrom('card.expiryMonth');})
    .forMember('source.exp_year', function(opts) { opts.mapFrom('card.expiryYear');})
    .forMember('source.number', function(opts){ opts.mapFrom('card.number');})
    .forMember('source.address_zip', function(opts) { opts.mapFrom('card.zip');})
    .forMember('source.cvc', function(opts) { opts.mapFrom('card.cvc');})
    .forMember('source.object', function(){return 'card'})
    .forMember('trial_end', function(opts) { opts.mapFrom('trialEnd')})

var cn = jsf(schema);

var cn2= automapper.map('a', 'b', cn);

<p>When I run through the following code the "card" node fails to load in the "source" field as a nested entity. I reviewed the source code which states you can map from 'nested.property' which is supposed to create that object and property in the project destination but it doesn't appear to be working for me. Any help would be much appreciated.</p>

<pre><code>var jsf = require('json-schema-faker');
var automapper = require('automapper-ts').automapper;

var schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"name": "customer",
"title": "customer",
"description": "Canonical Customer",
"type": "object",
"properties": {
"email": {
"type": "string",
"format": "email"
},
"description": {
"type": "string"
},
"phone": {
"type": "string"
},
"card": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"expiryMonth": {
"type": "string",
"minLength": 2,
"maxLength": 2,
"pattern": "^(1[0-2])|([0][1-9])$"
},
"expiryYear": {
"type": "string",
"minLength": 4,
"maxLength": 4,
"pattern": "^20([1-9][5-9])$"
},
"number": {
"type": "string",
"pattern": "^(?:4[0-9]{12}(?:[0-9]{3})?|5[12345][0-9]{14}|3[47][0-9]{13}|3(?:0[012345]|[68][0-9])[0-9]{11}|6(?:011|5[0-9]{2})[0-9]{12}|(?:2131|1800|35[0-9]{3})[0-9]{11})$"
},
"cvc": {
"type": "integer",
"maximum": 9999
},
"zip": {
"type": "string"
}
},
"required": [
"name",
"expiryMonth",
"expiryYear",
"number",
"zip"
],
"additionalProperties": false
},
"trialEnd": {
"type": "number",
"minimum": 1448420692,
"maximum": 4098384000
}
},
"required": [
"email",
"card"
],
"additionalProperties": false
};

automapper.createMap('a', 'b')
.forMember('description', function(opts) {opts.mapFrom('description');})
.forMember('email', function(opts) { opts.mapFrom('email');})
.forMember('source.name', function(opts) { opts.mapFrom('card.name');})
.forMember('source.exp_month', function(opts){ opts.mapFrom('card.expiryMonth');})
.forMember('source.exp_year', function(opts) { opts.mapFrom('card.expiryYear');})
.forMember('source.number', function(opts){ opts.mapFrom('card.number');})
.forMember('source.address_zip', function(opts) { opts.mapFrom('card.zip');})
.forMember('source.cvc', function(opts) { opts.mapFrom('card.cvc');})
.forMember('source.object', function(){return 'card'})
.forMember('trial_end', function(opts) { opts.mapFrom('trialEnd')})

var cn = jsf(schema);

var cn2= automapper.map('a', 'b', cn);
</code></pre>
 
Top