找回密码
 立即注册
首页 业界区 业界 VonaJS: 序列化/数据脱敏(下)

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

厥轧匠 2025-11-14 12:35:01
VonaJS 提供了序列化能力,可以对 API 的响应数据进行转换,比如:排除密码字段,对 Email 和 Mobile 进行脱敏处理,等等
前文介绍了序列化的一般用法。这里再介绍一组工具函数。通过工具函数可以更加便利的使用序列化能力
工具清单

工具: @Serializer工具: v说明@Serializer.transformv.serializerTransform使用Serializer Transform,参见: 序列化@Serializer.excludev.serializerExclude排除字段@Serializer.replacev.serializerReplace对字段值进行脱敏处理@Serializer.getterv.serializerGetter采用getter机制生成新的字段值@Serializer.customv.serializerCustom使用自定义函数对字段值进行处理
  1. 既然`@Serializer`工具非常简洁,直观,为什么还要提供`v`工具?
  2. 1. `v`工具可以实现通过 App Config 修改配置
  3. 2. `v`工具和`@Serializer`工具底层逻辑是一致的
复制代码
@Serializer.transform/v.serializerTransform

比如,将EntityStudent中的name字段值转化为大写
我们直接使用序列化中创建的 Serializer Transform upper
1. @Serializer.transform
  1. class EntityStudent {
  2. + @Serializer.transform('demo-student:upper')
  3.   @Api.field(v.title($locale('Name')))
  4.   name: string;
  5. }
复制代码
2. v.serializerTransform
  1. class EntityStudent {
  2. + @Api.field(v.serializerTransform('demo-student:upper'), v.title($locale('Name')))
  3.   name: string;
  4. }
复制代码
3. App Config

可以在 App Config 中修改配置
src/backend/config/config/config.ts

  • 方法 1: 直接修改 Openapi 参数
  1. // onions
  2. config.onions = {
  3.   entity: {
  4.     'demo-student:student': {
  5.       fields: {
  6.         name: {
  7.           serializerTransforms: {
  8.             'demo-student:upper': {},
  9.           },
  10.         },
  11.       },
  12.     },
  13.   },
  14. };
复制代码

  • 方法 2: 构造一个新的 schema
  1. import { $makeSchema, v } from 'vona-module-a-openapi';
  2. // onions
  3. config.onions = {
  4.   entity: {
  5.     'demo-student:student': {
  6.       fields: {
  7.         name: $makeSchema(v.serializerTransform('demo-student:upper'), z.string()),
  8.       },
  9.     },
  10.   },
  11. };
复制代码
@Serializer.exclude/v.serializerExclude

比如,排除EntityStudent中的name字段
1. @Serializer.exclude
  1. class EntityStudent {
  2. + @Serializer.exclude()
  3.   @Api.field(v.title($locale('Name')))
  4.   name: string;
  5. }
复制代码
2. v.serializerExclude
  1. class EntityStudent {
  2. + @Api.field(v.serializerExclude(), v.title($locale('Name')))
  3.   name: string;
  4. }
复制代码
3. App Config

可以在 App Config 中修改配置
src/backend/config/config/config.ts

  • 方法 1: 直接修改 Openapi 参数
  1. // onions
  2. config.onions = {
  3.   entity: {
  4.     'demo-student:student': {
  5.       fields: {
  6.         name: { exclude: false },
  7.       },
  8.     },
  9.   },
  10. };
复制代码

  • 方法 2: 构造一个新的 schema
  1. import { $makeSchema, v } from 'vona-module-a-openapi';
  2. // onions
  3. config.onions = {
  4.   entity: {
  5.     'demo-student:student': {
  6.       fields: {
  7.         name: $makeSchema(v.serializerExclude(), z.string()),
  8.       },
  9.     },
  10.   },
  11. };
复制代码
@Serializer.replace/v.serializerReplace

比如,将EntityStudent中的name字段值进行脱敏处理
比如,name 原始值为tom,脱敏之后为t***m
1. @Serializer.replace
  1. class EntityStudent {
  2. + @Serializer.replace({ patternFrom: /(\w)(\w+)(\w)/, patternTo: '$1***$3' })
  3.   @Api.field(v.title($locale('Name')))
  4.   name: string;
  5. }
复制代码
2. v.serializerReplace
  1. class EntityStudent {
  2.   @Api.field(
  3. +   v.serializerReplace({ patternFrom: /(\w)(\w+)(\w)/, patternTo: '$1***$3' }),
  4.     v.title($locale('Name')),
  5.   )
  6.   name: string;
  7. }
复制代码
3. App Config

可以在 App Config 中修改配置
src/backend/config/config/config.ts

  • 方法 1: 直接修改 Openapi 参数
  1. // onions
  2. config.onions = {
  3.   entity: {
  4.     'demo-student:student': {
  5.       fields: {
  6.         name: {
  7.           serializerTransforms: {
  8.             'a-serialization:replace': {
  9.               patternFrom: /(\w)(\w+)(\w)/,
  10.               patternTo: '$1***$3',
  11.             },
  12.           },
  13.         },
  14.       },
  15.     },
  16.   },
  17. };
复制代码

  • a-serialization:replace: a-serialization模块提供的 Serializer Transform


  • 方法 2: 构造一个新的 schema
  1. import { $makeSchema, v } from 'vona-module-a-openapi';
  2. // onions
  3. config.onions = {
  4.   entity: {
  5.     'demo-student:student': {
  6.       fields: {
  7.         name: $makeSchema(
  8.           v.serializerReplace({ patternFrom: /(\w)(\w+)(\w)/, patternTo: '$1***$3' }),
  9.           z.string(),
  10.         ),
  11.       },
  12.     },
  13.   },
  14. };
复制代码
@Serializer.getter/v.serializerGetter

比如,EntityStudent中的fullName字段由firstName和lastName字段组合而成
1. getter
  1. class EntityStudent {
  2.   @Api.field()
  3.   firstName: string;
  4.   @Api.field()
  5.   lastName: string;
  6.   @Api.field()
  7. + get fullName(): string | undefined {
  8. +   return `${this.firstName} ${this.lastName}`;
  9. + }
  10. }
复制代码
2. @Serializer.getter
  1. class EntityStudent {
  2. + @Serializer.getter((data: EntityStudent) => {
  3. +   return `${data.firstName} ${data.lastName}`;
  4. + })
  5.   @Api.field()
  6.   fullName: string;
  7. }
复制代码
3. v.serializerGetter
  1. class EntityStudent {
  2. + @Api.field(v.serializerGetter((data: EntityStudent) => {
  3. +   return `${data.firstName} ${data.lastName}`;
  4. + }))
  5.   fullName: string;
  6. }
复制代码
4. App Config

可以在 App Config 中修改配置
src/backend/config/config/config.ts

  • 方法 1: 直接修改 Openapi 参数
  1. // onions
  2. config.onions = {
  3.   entity: {
  4.     'demo-student:student': {
  5.       fields: {
  6.         fullName: {
  7.           serializerTransforms: {
  8.             'a-serialization:getter': {
  9.               getter: (data: EntityStudent) => {
  10.                 return `${data.firstName} ${data.lastName}`;
  11.               },
  12.             },
  13.           },
  14.         },
  15.       },
  16.     },
  17.   },
  18. };
复制代码

  • a-serialization:getter: a-serialization模块提供的 Serializer Transform


  • 方法 2: 构造一个新的 schema
  1. import { $makeSchema, v } from 'vona-module-a-openapi';
  2. // onions
  3. config.onions = {
  4.   entity: {
  5.     'demo-student:student': {
  6.       fields: {
  7.         fullName: $makeSchema(
  8.           v.serializerGetter((data: EntityStudent) => {
  9.             return `${data.firstName} ${data.lastName}`;
  10.           }),
  11.           z.string(),
  12.         ),
  13.       },
  14.     },
  15.   },
  16. };
复制代码
@Serializer.custom/v.serializerCustom

比如,将EntityStudent中的name字段值转换为大写
1. @Serializer.custom
  1. class EntityStudent {
  2. + @Serializer.custom((value: string) => {
  3. +   return value.toUpperCase();
  4. + })
  5.   @Api.field(v.title($locale('Name')))
  6.   name: string;
  7. }
复制代码
2. v.serializerCustom
  1. class EntityStudent {
  2.   @Api.field(
  3. +   v.serializerCustom((value: string) => {
  4. +     return value.toUpperCase();
  5. +   }),
  6.     v.title($locale('Name')),
  7.   )
  8.   name: string;
  9. }
复制代码
3. App Config

可以在 App Config 中修改配置
src/backend/config/config/config.ts

  • 方法 1: 直接修改 Openapi 参数
  1. // onions
  2. config.onions = {
  3.   entity: {
  4.     'demo-student:student': {
  5.       fields: {
  6.         name: {
  7.           serializerTransforms: {
  8.             'a-serialization:custom': {
  9.               custom: (value: string) => {
  10.                 return value.toUpperCase();
  11.               },
  12.             },
  13.           },
  14.         },
  15.       },
  16.     },
  17.   },
  18. };
复制代码

  • a-serialization:custom: a-serialization模块提供的 Serializer Transform


  • 方法 2: 构造一个新的 schema
  1. import { $makeSchema, v } from 'vona-module-a-openapi';
  2. // onions
  3. config.onions = {
  4.   entity: {
  5.     'demo-student:student': {
  6.       fields: {
  7.         name: $makeSchema(
  8.           v.serializerCustom((value: string) => {
  9.             return value.toUpperCase();
  10.           }),
  11.           z.string(),
  12.         ),
  13.       },
  14.     },
  15.   },
  16. };
复制代码
资源


  • Github:https://github.com/vonajs/vona
  • 文档:https://vona.js.org

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册