-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
49 lines (39 loc) · 1.05 KB
/
index.ts
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
import { validateXML } from 'xmllint-wasm';
import * as fs from 'fs';
import * as path from 'path';
const schemas = [
'saml-schema-protocol-2.0.xsd',
'datatypes.dtd',
'saml-schema-assertion-2.0.xsd',
'xmldsig-core-schema.xsd',
'XMLSchema.dtd',
'xenc-schema.xsd'
];
export const validate = async (xml: string) => {
const schemaPath = path.resolve(__dirname, 'schemas');
const [schema, ...preload] = await Promise.all(schemas.map(async file => ({
fileName: file,
contents: await fs.readFileSync(`${schemaPath}/${file}`, 'utf-8')
})));
try {
const validationResult = await validateXML({
xml: [
{
fileName: 'content.xml',
contents: xml,
},
],
extension: 'schema',
schema: [schema.contents],
preload: preload
});
if (validationResult.valid) {
return true;
}
console.debug(validationResult);
throw validationResult.errors;
} catch (error) {
console.error('[ERROR] validateXML', error);
throw new Error('ERR_EXCEPTION_VALIDATE_XML');
}
};