Skip to content

Commit

Permalink
use native xmllint for schema validation (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitakuye authored and tngan committed May 21, 2019
1 parent d22ace7 commit e2e20c1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"dependencies": {
"camelcase": "^5.2.0",
"deflate-js": "^0.2.3",
"global": "^4.3.2",
"node-forge": "^0.7.5",
"node-rsa": "^0.4.2",
"uuid": "^3.3.2",
Expand All @@ -46,7 +47,8 @@
"optionalDependencies": {
"@authenio/xml-encryption": "^0.11.2",
"libxml-xsd": "^0.5.2",
"node-xmllint": "^1.0.0"
"node-xmllint": "^1.0.0",
"validate-with-xmllint": "^1.0.2"
},
"devDependencies": {
"@types/camelcase": "^5.2.0",
Expand All @@ -58,6 +60,6 @@
"coveralls": "^3.0.2",
"nyc": "^11.9.0",
"tslint": "^5.12.1",
"typescript": "^3.3.3333"
"typescript": "^3.4.5"
}
}
28 changes: 26 additions & 2 deletions src/schema-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import * as path from 'path';
enum SchemaValidators {
JAVAC = '@authenio/xsd-schema-validator',
LIBXML = 'libxml-xsd',
XMLLINT = 'node-xmllint'
XMLLINT = 'node-xmllint',
NATIVEXMLLINT = 'validate-with-xmllint'
}

export interface SchemaValidator {
Expand Down Expand Up @@ -141,6 +142,29 @@ const getValidatorModule: GetValidatorModuleSpec = async () => {
};
}

if (selectedValidator === SchemaValidators.NATIVEXMLLINT) {

const mod = await import (SchemaValidators.NATIVEXMLLINT);

return {
validate: (xml: string) => {
return new Promise((resolve, reject) => {

process.chdir(path.resolve(__dirname, '../schemas'));

mod.validateXMLWithXSD(xml, xsd)
.then(function(result) {
return resolve('SUCCESS_VALIDATE_XML');
}, function(err) {
console.error('[ERROR] validateXML', err);
return reject('ERR_INVALID_XML');
})

});
}
};
}

// allow to skip the validate function if it's in development or test mode if no schema validator is provided
if (process.env.NODE_ENV === 'dev' || process.env.NODE_ENV === 'test') {
return {
Expand All @@ -156,4 +180,4 @@ const getValidatorModule: GetValidatorModuleSpec = async () => {

export {
getValidatorModule
};
};

0 comments on commit e2e20c1

Please sign in to comment.