-
Notifications
You must be signed in to change notification settings - Fork 8
/
mandatoryTest_6_1_12.js
46 lines (40 loc) · 1.13 KB
/
mandatoryTest_6_1_12.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
import bcpLanguageTagChecker from '../shared/bcpLanguageTagChecker.js'
/**
* @param {any} doc
*/
export default function mandatoryTest_6_1_12(doc) {
/** @type {Array<{ message: string; instancePath: string }>} */
const errors = []
let isValid = true
if (hasLangField(doc) && !bcpLanguageTagChecker(doc.document.lang)) {
isValid = false
errors.push({
message: 'is not a valid language-tag',
instancePath: '/document/lang',
})
}
if (hasSourceLangField(doc)) {
if (!bcpLanguageTagChecker(doc.document.source_lang)) {
isValid = false
errors.push({
message: 'is not a valid language-tag',
instancePath: '/document/source_lang',
})
}
}
return { errors, isValid }
}
/**
* @param {any} doc
* @returns {doc is { document: { lang: string } }}
*/
const hasLangField = (doc) =>
doc && doc.document && typeof doc.document.lang === 'string' ? true : false
/**
* @param {any} doc
* @returns {doc is { document: { source_lang: string } }}
*/
const hasSourceLangField = (doc) =>
doc && doc.document && typeof doc.document.source_lang === 'string'
? true
: false