0.0.6 • Published 4 years ago

umo-schema-util v0.0.6

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

schema-util

介绍

闪蝶 schema 解析工具。

schema 支持类型

1 基础类型:

  1. String
Object(banner内容填写) {
    btnText(活动跳转按钮文案): String[minLength: 10, default: "领取折扣"]
}
  1. Image
Object(banner内容填写) {
  image(图片地址[图片要求尺寸为590*250]): Image[width: 590, height: 250]
  name(用户名)
}
  1. Number
Object(券描述) {
  amount(券金额): Number[default: 20]
}
  1. Boolean
Object(活动规则) {
  show(拥有边框): Boolean
}
  1. URL
Array(用户列表) {
  link(目标地址): URL[regExp: "/^https:/gi"]
}
  1. Text
Object(协议描述) {
  desc(描述): Text
}
  1. RichText
Object(协议描述) {
  desc(规则描述): RichText
}
  1. Color
Object(用户列表) {
  bgColor(背景颜色): Color
}
  1. Enum
Object(描述) {
  type(抽奖形式): Enum {
    normal(一键抽奖),
    checksum(校验码)
  }
}
  1. File
Object(描述) {
  desc(上传文件): File
}
  1. Date
Object(用户列表) {
  name(用户名),
  birth(出身日期): Date
}
  1. Box
Object(描述) {
  button(热区按钮位置): Box
}

2 复合对象:

  1. Object:

其中子对象的值为各个子字段的默认值, 如:

Object(主内容) {
  title(标题),
  image(介绍图片): Image,
  desc(描述),
  btnName(按钮名称),
  btnHref(按钮链接): URL
}

对应 JSON 为

{
  "title": "打开支付宝,轻松找到她",
  "image": "https://os.alipayobjects.com/rmsportal/NCdKkRXpcwdUfFr.png",
  "desc": "如果没有找到,可能是你所在的城市还未开通哦。没关系,期待她突然出现的惊喜吧。",
  "btnName": "如何设置所在城市",
  "btnHref": "https://alipay.com"
}
  1. Array
Array(Banner,填单个图片时不轮播) {
  link(跳转链接 [不填时点击 banner 不能转跳]): URL,
  seed(链接埋点),
  image(图片地址): Image[width:750]
}

对应 JSON 为

[
  {
    "link": "alipays://",
    "seed": "alipays://",
    "image": "https://t.alipayobjects.com/images/rmsweb/T1xwNjXctcXXXXXXXX.jpeg_640xQ75"
  }
]

3 自定义类型:

对于如动态请求数据源等含有复杂业务逻辑的模板,morpho-schema-util 提供自定义 schema 来解决此类问题。其使用方式和 Object 类型一致,具体接入可以参考 https://yuque.antfin-inc.com/morpho/guide/zghus5

schema 文件:

Object(生活号城市选择) {
  citys(配置): moduleName.componentName {
    title(按钮正上方的介绍文案),
    moreText(按钮文案),
    moreLink(按钮正下方的文案),
    province(省份),
    city(市),
    district(区)
  },
}

对应 JSON 文件为

{
  citys: {
    title: '展示文案',
    moreText: '更多文案',
    moreLink: '跳转链接',
    province: '浙江',
    city: '杭州',
    district: '西湖'
  }
}

meta

group

作用:用于第一层 schema 节点,声明分组,供编辑器 UI 渲染,无其他功用。 限制条件:目前值仅支持 normaladvancedeveloperoperator,其他值会提示非法

特性

  • 解析 schema
var schema = require("@alipay/morpho-schema-util").schema;

var json = schema(`
  Array(foo) {
    href(href),
    title(title),
    img(image url): Image,
    amount(money amout)
  }
`);
// 输出 json 结构如下:
/**
  {
    type: 'array',
    description: 'foo',
    properties: {
      href: { description: 'href', type: 'string' },
      title: { description: 'title', type: 'string' },
      img: { description: 'image url', type: 'image' },
      amount: { description: 'money amount', type: 'string' }
    }
  }
*/

支持嵌套对象

// 嵌套对象深度, 如果超过 3 层则检验不通过
var nestLevel = 3;
var json = schema(`
  Object(abc) {
    title(title),
    user(user): Object {
      name(user name),
      age(user age): Number
    }
  }
`, nestLevel);
  • 根据 schema 生产对应 mock 数据
mock(schema, config)

config 用于设置默认值, 比如, 设置 config={ image: 'xx.png' }, 则 image 的默认值即为 xx.png. config 也可以是一个函数,其返回值用于设置默认值

  • 不同 schema 的合并
var array_origin = [{
  href: '店铺连接origin1',
  title: '标题origin1'
}, {
  href: '店铺连接origin2',
  title: '标题origin2'
}];

var array_mock_type_change = [{
  href: '店铺连接mock1',
  title: {
    image: '图片链接mock1'
  }
}, {
  href: '店铺连接mock2',
  title: {
    image: '图片链接mock2'
  }
}];
var result = merge(array_mock_type_change, array_origin);

// result的值为:
/**
 [{
   href: array_origin[0].href,
   title: array_mock_type_change[0].title
 }, {
   href: array_origin[1].href,
   title: array_mock_type_change[1].title
 }]
*/
0.0.5

4 years ago

0.0.4

4 years ago

0.0.6

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago