-
Notifications
You must be signed in to change notification settings - Fork 8
/
mandatoryTest_6_1_30.js
86 lines (78 loc) · 2.12 KB
/
mandatoryTest_6_1_30.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import Ajv from 'ajv/dist/jtd.js'
const ajv = new Ajv()
const inputSchema = /** @type {const} */ ({
additionalProperties: true,
properties: {
document: {
additionalProperties: true,
properties: {
tracking: {
additionalProperties: true,
optionalProperties: {
revision_history: {
elements: {
additionalProperties: true,
optionalProperties: {
number: { type: 'string' },
},
},
},
version: { type: 'string' },
},
},
},
},
},
})
const validate = ajv.compile(inputSchema)
/**
* @param {any} doc
*/
export default function mandatoryTest_6_1_30(doc) {
const ctx = {
errors:
/** @type {Array<{ instancePath: string; message: string }>} */ ([]),
isValid: true,
}
if (!validate(doc)) {
return ctx
}
/** @type {'SEMANTIC' | 'INTEGER' | null} */
let versioningSchema = null
doc.document.tracking.revision_history?.forEach((revision, revisionIndex) => {
if (typeof revision.number === 'string') {
const revisionNumberVersioningSchema = detectVersionSchema(
revision.number
)
if (versioningSchema === null) {
versioningSchema = revisionNumberVersioningSchema
}
if (versioningSchema !== revisionNumberVersioningSchema) {
ctx.isValid = false
ctx.errors.push({
instancePath: `/document/tracking/revisions/${revisionIndex}/number`,
message: 'mixed integer and semantic versioning',
})
}
}
})
if (versioningSchema != null && typeof doc.document.tracking.version === 'string') {
if (
versioningSchema !== detectVersionSchema(doc.document.tracking.version)
) {
ctx.isValid = false
ctx.errors.push({
instancePath: `/document/tracking/version`,
message: 'mixed integer and semantic versioning',
})
}
}
return ctx
}
/**
* @param {string} version
*/
function detectVersionSchema(version) {
if (parseInt(version).toString() === version) return 'INTEGER'
return 'SEMANTIC'
}