Last active
July 3, 2016 04:21
-
-
Save otakustay/353290e47eb0cb34264f2db9155dc9e0 to your computer and use it in GitHub Desktop.
Schema validation merge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let schema = { | |
type: 'object', | |
description: '推广单元表单', | |
properties: { | |
campaignId: { | |
type: 'string', | |
description: '营销活动' | |
}, | |
platformId: { | |
type: 'string', | |
description: '投放平台' | |
}, | |
names: { | |
type: 'array', | |
description: '推广单元名称', | |
minItems: 1, | |
uniqueItems: true, | |
items: { | |
type: 'string', | |
maxLength: 30 | |
} | |
}, | |
adUnitGroup: { | |
type: 'object', | |
properties: { | |
id: { | |
type: 'string', | |
description: '单元分类ID' | |
}, | |
name: { | |
type: 'string', | |
description: '单元分类名称', | |
maxLength: 30 | |
} | |
} | |
} | |
}, | |
required: ['campaignId', 'platformId', 'names', 'adUnitGroup'] | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let messages = { | |
type: '类型不正确', | |
invalidMessage: '验证${description}时出现未知错误', | |
campaignId: { | |
type: '类型不正确' | |
}, | |
names: { | |
minItems: '请至少填写一条${description}', | |
uniqueItems: '${description}不能重复', | |
items: { | |
maxLength: '${description}不能超过${maxLength}个字符' | |
} | |
}, | |
adUnitGroup: { | |
name: { | |
maxLength: '${description}不能超过${maxLength}个字符' | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"type": "object", | |
"description": "推广单元表单", | |
"properties": { | |
"campaignId": { | |
"type": "string", | |
"description": "营销活动", | |
"messages": { | |
"type": "类型不正确" | |
} | |
}, | |
"platformId": { | |
"type": "string", | |
"description": "投放平台" | |
}, | |
"names": { | |
"type": "array", | |
"description": "推广单元名称", | |
"minItems": 1, | |
"uniqueItems": true, | |
"items": { | |
"description": "单个推广单元名称", | |
"type": "string", | |
"maxLength": 30, | |
"messages": { | |
"maxLength": "单个推广单元名称不能超过30个字符" | |
} | |
}, | |
"messages": { | |
"minItems": "请至少填写一条推广单元名称", | |
"uniqueItems": "推广单元名称不能重复" | |
} | |
}, | |
"adUnitGroup": { | |
"type": "object", | |
"properties": { | |
"id": { | |
"type": "string", | |
"description": "单元分类ID" | |
}, | |
"name": { | |
"type": "string", | |
"description": "单元分类名称", | |
"maxLength": 30, | |
"messages": { | |
"maxLength": "单元分类名称不能超过30个字符" | |
} | |
} | |
} | |
} | |
}, | |
"required": [ | |
"campaignId", | |
"platformId", | |
"names", | |
"adUnitGroup" | |
], | |
"invalidMessage": "验证推广单元表单时出现未知错误", | |
"messages": { | |
"type": "类型不正确" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let update = require('diffy-update').default; | |
let u = require('underscore'); | |
u.templateSettings = { | |
interpolate: /\$\{(.+?)\}/g, // `${name}`直接输出 | |
escape: /\$\{\:(.+?)\}/g // `${:name}`提供HTML转义 | |
}; | |
let buildCommands = (schemaNode, messageNode) => { | |
let commands = u.mapObject( | |
u.pick(messageNode, 'invalidMessage', 'requiredMessage'), | |
value => { | |
return {$set: u.template(value)(schemaNode)} | |
} | |
); | |
let [messageProperties, otherProperties] = Object.entries(messageNode).reduce( | |
([messageProperties, otherProperties], [key, value]) => { | |
if (key === 'invalidMessage' || key === 'requiredMessage') { | |
return [messageProperties, otherProperties]; | |
} | |
if (typeof value === 'string') { | |
messageProperties[key] = value; | |
} | |
else { | |
otherProperties[key] = value; | |
} | |
return [messageProperties, otherProperties]; | |
}, | |
[{}, {}] | |
); | |
if (!u.isEmpty(messageProperties)) { | |
commands.messages = {$merge: u.mapObject(messageProperties, value => u.template(value)(schemaNode))}; | |
} | |
if (u.isEmpty(otherProperties)) { | |
return commands; | |
} | |
if (schemaNode.type === 'object') { | |
commands.properties = u.mapObject( | |
otherProperties, | |
(value, key) => buildCommands(schemaNode.properties[key], value) | |
); | |
} | |
else if (schemaNode.type === 'array' && otherProperties.items) { | |
commands.items = buildCommands(schemaNode.items, otherProperties.items); | |
} | |
return commands; | |
}; | |
let commandsForMergeMessages = buildCommands(schema, messages); | |
let validationSchema = update(schema, commandsForMergeMessages); | |
console.log(JSON.stringify(validationSchema, null, ' ')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment