VonaJS: 序列化/数据脱敏(下)

VonaJS 提供了序列化能力,可以对 API 的响应数据进行转换,比如:排除密码字段,对 Email 和 Mobile 进行脱敏处理,等等

前文介绍了序列化的一般用法。这里再介绍一组工具函数。通过工具函数可以更加便利的使用序列化能力

工具清单

工具: @Serializer 工具: v 说明
@Serializer.transform v.serializerTransform 使用Serializer Transform,参见: 序列化
@Serializer.exclude v.serializerExclude 排除字段
@Serializer.replace v.serializerReplace 对字段值进行脱敏处理
@Serializer.getter v.serializerGetter 采用getter机制生成新的字段值
@Serializer.custom v.serializerCustom 使用自定义函数对字段值进行处理
bash 复制代码
既然`@Serializer`工具非常简洁,直观,为什么还要提供`v`工具?

1. `v`工具可以实现通过 App Config 修改配置
2. `v`工具和`@Serializer`工具底层逻辑是一致的

@Serializer.transform/v.serializerTransform

比如,将EntityStudent中的name字段值转化为大写

我们直接使用序列化中创建的 Serializer Transform upper

1. @Serializer.transform

diff 复制代码
class EntityStudent {
+ @Serializer.transform('demo-student:upper')
  @Api.field(v.title($locale('Name')))
  name: string;
}

2. v.serializerTransform

diff 复制代码
class EntityStudent {
+ @Api.field(v.serializerTransform('demo-student:upper'), v.title($locale('Name')))
  name: string;
}

3. App Config

可以在 App Config 中修改配置

src/backend/config/config/config.ts

  • 方法 1: 直接修改 Openapi 参数
typescript 复制代码
// onions
config.onions = {
  entity: {
    'demo-student:student': {
      fields: {
        name: {
          serializerTransforms: {
            'demo-student:upper': {},
          },
        },
      },
    },
  },
};
  • 方法 2: 构造一个新的 schema
typescript 复制代码
import { $makeSchema, v } from 'vona-module-a-openapi';

// onions
config.onions = {
  entity: {
    'demo-student:student': {
      fields: {
        name: $makeSchema(v.serializerTransform('demo-student:upper'), z.string()),
      },
    },
  },
};

@Serializer.exclude/v.serializerExclude

比如,排除EntityStudent中的name字段

1. @Serializer.exclude

diff 复制代码
class EntityStudent {
+ @Serializer.exclude()
  @Api.field(v.title($locale('Name')))
  name: string;
}

2. v.serializerExclude

diff 复制代码
class EntityStudent {
+ @Api.field(v.serializerExclude(), v.title($locale('Name')))
  name: string;
}

3. App Config

可以在 App Config 中修改配置

src/backend/config/config/config.ts

  • 方法 1: 直接修改 Openapi 参数
typescript 复制代码
// onions
config.onions = {
  entity: {
    'demo-student:student': {
      fields: {
        name: { exclude: false },
      },
    },
  },
};
  • 方法 2: 构造一个新的 schema
typescript 复制代码
import { $makeSchema, v } from 'vona-module-a-openapi';

// onions
config.onions = {
  entity: {
    'demo-student:student': {
      fields: {
        name: $makeSchema(v.serializerExclude(), z.string()),
      },
    },
  },
};

@Serializer.replace/v.serializerReplace

比如,将EntityStudent中的name字段值进行脱敏处理

比如,name 原始值为tom,脱敏之后为t***m

1. @Serializer.replace

diff 复制代码
class EntityStudent {
+ @Serializer.replace({ patternFrom: /(\w)(\w+)(\w)/, patternTo: '$1***$3' })
  @Api.field(v.title($locale('Name')))
  name: string;
}

2. v.serializerReplace

diff 复制代码
class EntityStudent {
  @Api.field(
+   v.serializerReplace({ patternFrom: /(\w)(\w+)(\w)/, patternTo: '$1***$3' }),
    v.title($locale('Name')),
  )
  name: string;
}

3. App Config

可以在 App Config 中修改配置

src/backend/config/config/config.ts

  • 方法 1: 直接修改 Openapi 参数
typescript 复制代码
// onions
config.onions = {
  entity: {
    'demo-student:student': {
      fields: {
        name: {
          serializerTransforms: {
            'a-serialization:replace': {
              patternFrom: /(\w)(\w+)(\w)/,
              patternTo: '$1***$3',
            },
          },
        },
      },
    },
  },
};
  • a-serialization:replace: a-serialization模块提供的 Serializer Transform
  • 方法 2: 构造一个新的 schema
typescript 复制代码
import { $makeSchema, v } from 'vona-module-a-openapi';

// onions
config.onions = {
  entity: {
    'demo-student:student': {
      fields: {
        name: $makeSchema(
          v.serializerReplace({ patternFrom: /(\w)(\w+)(\w)/, patternTo: '$1***$3' }),
          z.string(),
        ),
      },
    },
  },
};

@Serializer.getter/v.serializerGetter

比如,EntityStudent中的fullName字段由firstName和lastName字段组合而成

1. getter

diff 复制代码
class EntityStudent {
  @Api.field()
  firstName: string;

  @Api.field()
  lastName: string;

  @Api.field()
+ get fullName(): string | undefined {
+   return `${this.firstName} ${this.lastName}`;
+ }
}

2. @Serializer.getter

diff 复制代码
class EntityStudent {
+ @Serializer.getter((data: EntityStudent) => {
+   return `${data.firstName} ${data.lastName}`;
+ })
  @Api.field()
  fullName: string;
}

3. v.serializerGetter

diff 复制代码
class EntityStudent {
+ @Api.field(v.serializerGetter((data: EntityStudent) => {
+   return `${data.firstName} ${data.lastName}`;
+ }))
  fullName: string;
}

4. App Config

可以在 App Config 中修改配置

src/backend/config/config/config.ts

  • 方法 1: 直接修改 Openapi 参数
typescript 复制代码
// onions
config.onions = {
  entity: {
    'demo-student:student': {
      fields: {
        fullName: {
          serializerTransforms: {
            'a-serialization:getter': {
              getter: (data: EntityStudent) => {
                return `${data.firstName} ${data.lastName}`;
              },
            },
          },
        },
      },
    },
  },
};
  • a-serialization:getter: a-serialization模块提供的 Serializer Transform
  • 方法 2: 构造一个新的 schema
typescript 复制代码
import { $makeSchema, v } from 'vona-module-a-openapi';

// onions
config.onions = {
  entity: {
    'demo-student:student': {
      fields: {
        fullName: $makeSchema(
          v.serializerGetter((data: EntityStudent) => {
            return `${data.firstName} ${data.lastName}`;
          }),
          z.string(),
        ),
      },
    },
  },
};

@Serializer.custom/v.serializerCustom

比如,将EntityStudent中的name字段值转换为大写

1. @Serializer.custom

diff 复制代码
class EntityStudent {
+ @Serializer.custom((value: string) => {
+   return value.toUpperCase();
+ })
  @Api.field(v.title($locale('Name')))
  name: string;
}

2. v.serializerCustom

diff 复制代码
class EntityStudent {
  @Api.field(
+   v.serializerCustom((value: string) => {
+     return value.toUpperCase();
+   }),
    v.title($locale('Name')),
  )
  name: string;
}

3. App Config

可以在 App Config 中修改配置

src/backend/config/config/config.ts

  • 方法 1: 直接修改 Openapi 参数
typescript 复制代码
// onions
config.onions = {
  entity: {
    'demo-student:student': {
      fields: {
        name: {
          serializerTransforms: {
            'a-serialization:custom': {
              custom: (value: string) => {
                return value.toUpperCase();
              },
            },
          },
        },
      },
    },
  },
};
  • a-serialization:custom: a-serialization模块提供的 Serializer Transform
  • 方法 2: 构造一个新的 schema
typescript 复制代码
import { $makeSchema, v } from 'vona-module-a-openapi';

// onions
config.onions = {
  entity: {
    'demo-student:student': {
      fields: {
        name: $makeSchema(
          v.serializerCustom((value: string) => {
            return value.toUpperCase();
          }),
          z.string(),
        ),
      },
    },
  },
};

资源

相关推荐
miss2 小时前
从零做一个可视化规则引擎:Vue3 递归条件树 + 双引擎结果对比
前端·vue.js·typescript
seowmt1 天前
结构化数据和自然语言描述哪个更管用?答案分场景
人工智能·google·typescript·go语言·schema
进击的明明1 天前
TypeScript速通笔记(上)
前端·面试·typescript
Orange_sparkle1 天前
langgraph快速入门
学习·ai·typescript·deepseek·claude code
FYKJ_20102 天前
springboot刑事案件管理系统03047-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·typescript·spark·django
用户0934077735142 天前
HarmonyOS WPS Open SDK 实践:从 HAR 集成到 OpenFileRequest 最小闭环
typescript·harmonyos
FYKJ_20102 天前
springboot雅集社区养老管理系统04456-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·typescript·spark·django
FYKJ_20102 天前
express皖美特色农产品网售系统53118-计算机课程设计、毕业设计
javascript·vue.js·spring boot·mysql·typescript·spark·express
FYKJ_20102 天前
springboot羽毛球场地管理系统00626-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·typescript·spark·django
FYKJ_20102 天前
django药饮个性化推荐系统54839-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·typescript·spark·django