Skip to content

Commit

Permalink
🦁 新增 schema 校验逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
metowolf committed Jul 18, 2023
1 parent f506276 commit d3fda6d
Show file tree
Hide file tree
Showing 8 changed files with 560 additions and 552 deletions.
2 changes: 1 addition & 1 deletion data/政府机构/工业和信息化部.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ basic:
organization: 工业和信息化部
cellPhone:
- 12381
- 010-12300
- 12300
url: http://www.miit.gov.cn/
4 changes: 2 additions & 2 deletions data/租车代驾/哈啰出行.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ basic:
organization: 哈啰出行
cellPhone:
- 021-61679500
- 9521-8900
url: https://www.hellobike.com/
- 95175177
url: https://www.hello-inc.com/
2 changes: 1 addition & 1 deletion data/金融银行/平安银行.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
basic:
organization: 平安银行
cellPhone:
- 95511;3
- 95511
url: http://bank.pingan.com/
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@
"license": "MIT",
"private": true,
"devDependencies": {
"ava": "^4.0.1",
"del": "^6.0.0",
"globby": "^13.1.1",
"ava": "^5.3.1",
"del": "^7.0.0",
"globby": "^13.2.2",
"google-libphonenumber": "^3.2.32",
"gulp": "^4.0.2",
"gulp-concat": "^2.6.1",
"gulp-concat-folders": "^1.3.1",
"gulp-rename": "^2.0.0",
"gulp-zip": "^5.1.0",
"image-size": "^1.0.1",
"image-size": "^1.0.2",
"joi": "^17.9.2",
"js-yaml": "^4.1.0",
"pretty-bytes": "^6.0.0",
"read-chunk": "^4.0.2",
"pretty-bytes": "^6.1.1",
"read-chunk": "^4.0.3",
"vcards-js": "^2.10.0"
}
}
27 changes: 27 additions & 0 deletions src/const/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Joi from 'joi'
import libphonenumber from 'google-libphonenumber'

const checkPhone = (phone) => {
const phoneUtil = libphonenumber.PhoneNumberUtil.getInstance()
const phoneNumber = phoneUtil.parseAndKeepRawInput(phone, 'CN')
return phoneUtil.isValidNumber(phoneNumber)
}

const schema = Joi.object({
basic: Joi.object({
organization: Joi.string().required(),
cellPhone: Joi.array().items(
Joi.string().custom((value, helper) => {
if (!checkPhone(value)) {
return helper.message("phone is incorrect")
}
return value
}),
Joi.number()
).required(),
url: Joi.string().uri().optional(),
workEmail: Joi.array().items(Joi.string().email()).optional()
}).required()
})

export default schema
6 changes: 3 additions & 3 deletions src/gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs'
import path from 'path'
import del from 'del'
import { deleteAsync } from 'del'
import through2 from 'through2'

import gulp from 'gulp'
Expand Down Expand Up @@ -46,7 +46,7 @@ const allinone = () => {
}

const clean = () => {
return del([
return deleteAsync([
'public',
'temp'
])
Expand All @@ -64,7 +64,7 @@ const createRadicale = () => {
}

const cleanRadicale = () => {
return del([
return deleteAsync([
'radicale'
], {force: true})
}
Expand Down
12 changes: 10 additions & 2 deletions src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import imageSize from 'image-size'
import prettyBytes from 'pretty-bytes'
import isPng from './utils/isPng.js'
import blockList from './const/block.js'
import schema from './const/schema.js'

const checkImage = (t, path) => {
const buffer = readChunkSync(path, {
Expand All @@ -31,9 +32,16 @@ const checkVCard = (t, path) => {
const data = fs.readFileSync(path, 'utf8')
const json = yaml.load(data)

// 检查 schema
const { value, error } = schema.validate(json)
if (error) {
t.fail(`schema 校验失败 ${error.message}, ${JSON.stringify(value)}`)
}

for (let phone of json.basic.cellPhone) {
if (phone.toString().substr(0, 3) === '106') {
t.fail('不收录 106 短信通道号码')
// 不收录 106 短信通道号码(短号码例外)
if (phone.toString().substr(0, 3) === '106' && phone.toString().length > 10) {
t.fail(`不收录 ${phone},原因:106 短信通道号码`)
}
}

Expand Down
Loading

0 comments on commit d3fda6d

Please sign in to comment.