Search code examples
javascriptnode.js

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


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.

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

Solution

  • which version of AutoMapperTS do you currently have in use? Version 1.6.1 should seamlessly support the kind of operations you are trying to implement.

    Please notice the library name has been changed lately, you should install the automapper-ts NPM package. The old package is marked obsolete. It is the same package, therefore your code using it should be totally fine ;). BTW, here is the NPM url: https://www.npmjs.com/package/automapper-ts

    Cheers, Bert