vin-api-valid-tracker.js
1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/** 车架号 API 异步校验状态 — 对齐 createOrder vinValid + validateVin */
import { buildGroupErrorKey } from './group-values.js'
export function buildVinErrorKey({ groupKey, rowIndex, childKey } = {}) {
if (groupKey != null && rowIndex != null && childKey) {
return buildGroupErrorKey(groupKey, rowIndex, childKey)
}
return childKey || ''
}
export function shouldRunVinApiValidate(vin) {
const str = String(vin ?? '').trim()
return str.length === 17
}
/**
* @param {string} vin
* @param {Function} validateVinFn - utils/vinVaildate.validateVin
* @returns {Promise<{ ok: boolean, valid: boolean }>}
*/
export async function runVinApiValidate(vin, validateVinFn) {
if (!shouldRunVinApiValidate(vin)) {
return { ok: true, valid: true }
}
if (typeof validateVinFn !== 'function') {
return { ok: false, valid: false }
}
try {
const result = await validateVinFn(vin)
const valid = result?.data?.data?.valid
return { ok: true, valid: valid !== false }
} catch {
return { ok: false, valid: false }
}
}
export default {
buildVinErrorKey,
shouldRunVinApiValidate,
runVinApiValidate
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
buildVinErrorKey,
shouldRunVinApiValidate,
runVinApiValidate,
default: {
buildVinErrorKey,
shouldRunVinApiValidate,
runVinApiValidate
}
}
}