当我运行以下代码时,“card”节点无法作为嵌套实体加载到“source”字段中。我查看了源代码,声明你可以从'nested.property'映射,它应该在项目目的地创建该对象和属性,但它似乎不适合我。任何帮助将不胜感激。
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);
答案 0 :(得分:1)
您目前使用的是哪个版本的AutoMapperTS?版本1.6.1应该无缝地支持您尝试实现的操作类型。
请注意最近更改了库名称,您应该安装automapper-ts NPM软件包。旧包装已标记为过时。它是相同的包,因此使用它的代码应该完全没问题;)。顺便说一句,这是NPM网址:https://www.npmjs.com/package/automapper-ts
干杯,伯特