
Security News
pnpm 11.5 Adds Support for Recognizing npm Staged Publishes
pnpm 11.5 now recognizes npm staged publish approvals in release metadata, preventing those releases from being mistaken for lower-trust package publishes.
数据清洗主要针对异步数据进行整理和转化,填充缺失的数据结构和字段。
一方面保证接口中约定的字段一定能正常取到不会报错,另一方面可以用于代码提示和检查,便于开发并减少拼写错误
// 第一步,导入清洗方法,分严格和非严格模式两种,根据需求自选,具体区别后面介绍
// 清洗方法的入参有两个,第一个是待清洗的数据,第二个是schema
import {strictClean, clean} from '@/utils/dataClean'
// 第二步,定义schema,根据接口提供的文档写出空数据接口。
// 如果是数组格式,需要在数组中写一个对象,清洗时会跟据数组中第一个元素的结构进行清洗
const searchSchema = {
errorCode: null, content: {
products: {
count: 0, rows: [{
skuTitle: '',
hasStock: true,
areaPrice: 0,
promotion: { pmprice: 0 },
skuId: 0,
skuPicCdnUrl: '',
marketLabel: '',
marketInfo: { ruleType: 0, ruleSubType: 0, taskType: 0, ruleToken: '', floorPrice: '', link: '' }
}]
}
}
}
// 第三步,在页面的state定义部分中先用null清洗一份空数据放入state中。
// 保证页面能够正常的进行代码提示,同时保证无数据状态下渲染不会报错。
state={
products: strictClean(null, searchSchema).content.products.rows,
}
// 第四步,在代码执行时获取到数据后直接放入清洗方法中进行清洗,之后再对数据进行进一步操作
const result = strictClean((await request()), searchSchema)
// 该取值符合shcema定义,书写过程中有代码提示,且可以保证运行时不会出现取值异常
result.content.products.count
// 不符合shcema定义,书写过程中有product部分会报红
result.content.product.count
// 该取值符合shcema定义,有代码提示且不报红。
// 但是需要注意实际运行时rows中可能数据量不足。
// 因此仅能保证对数组进行forEach/map等遍历操作时无异常
// 直接下标取值时,若对应数据不存在仍有可能出现异常
result.content.products.rows[5].skuId
需要说明的是严格模式和非严格模式在执行时是一样的,并不影响最终清洗结果。区别只是代码校验时的严格程度
在schema中将一个字段定义为不同的类型会有不同的操作,具体如下
// 定义目标数据结构
const schema = {
a: '',
b: 1,
c: [{d: ''}],
o: {}
}
const result = clean(null, schema)
result.a // 不报错, 类型为string
result.b // 不报错, 类型为number
result.c // 不报错, 类型为数组
result.c[0].d // 不报错, 类型为string
result.c[1].d // 不报错, 类型为string
result.c[1].a // 不报错, 类型为string | number | boolean | null | undefined
result.c[1].a.a // 报错
result.d // 不报错, 类型为string | number | boolean | null | undefined
result.d.d // 报错
result.o // 不报错, 类型为object
result.o.a // 不报错, 类型为string | number | boolean | null | undefined
result.o.a.a // 报错
FAQs
We found that dataclean demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
pnpm 11.5 now recognizes npm staged publish approvals in release metadata, preventing those releases from being mistaken for lower-trust package publishes.

Security News
Federal audit finds NIST lacked a plan to clear the NVD backlog, wasted funds on duplicate work, and delayed use of CISA data.

Research
/Security News
A mini Shai-Hulud campaign compromised Red Hat Cloud Services npm packages to steal developer and CI/CD secrets during installation.