xsd-schema-validator
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -15,17 +15,22 @@ var fs = require('fs'); | ||
var JAVA = JAVA_HOME + '/bin/java'; | ||
var JAVAC = JAVA_HOME + '/bin/javac'; | ||
// Assume jvm/jdk are on the PATH if JAVA_HOME is not set | ||
var JAVA = JAVA_HOME ? JAVA_HOME + '/bin/java' : 'java'; | ||
var JAVAC = JAVA_HOME ? JAVA_HOME + '/bin/javac' : 'javac'; | ||
function withValidator(callback) { | ||
if (!JAVA_HOME) { | ||
callback(new Error('JAVA_HOME is not defined')); | ||
} else | ||
if (!fs.existsSync(JAVAC) && !fs.existsSync(JAVAC + '.exe')) { | ||
callback(new Error('JDK required at JAVA_HOME to compile helper')); | ||
if (fs.existsSync(VALIDATOR + '.class')) { | ||
callback(); | ||
} else { | ||
if (fs.existsSync(VALIDATOR + '.class')) { | ||
callback(); | ||
if (JAVA_HOME && !fs.existsSync(JAVAC) && !fs.existsSync(JAVAC + '.exe')) { | ||
callback(new Error('JDK required at JAVA_HOME or in path to compile helper')); | ||
} else { | ||
spawn(JAVAC, [ 'support/XMLValidator.java' ], { cwd: BASE_DIR }).on('exit', callback); | ||
spawn(JAVAC, [ 'support/XMLValidator.java' ], { cwd: BASE_DIR }) | ||
.on('exit', function(exitCode) { | ||
if (exitCode !== 0) { | ||
callback(new Error('Failed to compile helper. Is javac on the PATH?')); | ||
} else { | ||
callback(); | ||
} | ||
}); | ||
} | ||
@@ -52,7 +57,7 @@ } | ||
/** | ||
* Validate a xml file agains the given schema | ||
* Validate a xml file against the given schema | ||
* | ||
* @param {String} xml | ||
* @param {String} schemaLocation the location of the schema | ||
* @param {Function} callback to be invoked with (err, result) | ||
* @param {String|ReadableStream|Object} xml | ||
* @param {String} schema path to schema | ||
* @param {Function} callback to be invoked with (err, result) | ||
*/ | ||
@@ -70,2 +75,8 @@ Validator.prototype.validateXML = function(xml, schema, callback) { | ||
var input = '-stdin'; | ||
if (typeof xml === 'object' && xml.file) { | ||
input = '-file='+ xml.file; | ||
} | ||
var validator = spawn(JAVA, [ | ||
@@ -76,3 +87,3 @@ '-Dfile.encoding=UTF-8', | ||
'support.XMLValidator', | ||
'-stdin', | ||
input, | ||
'-schema=' + schema | ||
@@ -141,3 +152,6 @@ ], { cwd: cwd }); | ||
xml.pipe(stdin); | ||
} else { | ||
} | ||
// handle string input | ||
if (typeof xml === 'string') { | ||
stdin.write(xml); | ||
@@ -144,0 +158,0 @@ stdin.end(); |
{ | ||
"name": "xsd-schema-validator", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "A (XSD) schema validator for nodejs", | ||
@@ -5,0 +5,0 @@ "main": "lib/validator.js", |
@@ -10,5 +10,6 @@ # xsd-schema-validator | ||
Ensure a `JAVA_HOME` environment variable exists that points to an installed JDK. | ||
This utility assumes that `javac` and `java` are on the path _or_ that | ||
the `JAVA_HOME` environment exists and points to an installed JDK. | ||
On some platforms, i.e. [Mac OSX](http://www.mkyong.com/java/how-to-set-java_home-environment-variable-on-mac-os-x/) you need to define it manually. | ||
On some platforms, i.e. [Mac OSX](http://www.mkyong.com/java/how-to-set-java_home-environment-variable-on-mac-os-x/) you need to define `JAVA_HOME` manually. | ||
@@ -48,3 +49,9 @@ | ||
...and files, too: | ||
```javascript | ||
validator.validateXML({ file: 'some.xml' }, ...); | ||
``` | ||
## Why | ||
@@ -51,0 +58,0 @@ |
@@ -41,2 +41,9 @@ var validator = require('../lib/validator'); | ||
expect(err).toBeDefined(); | ||
// correct error message | ||
expect(err.message).toMatch(/Attribute 'unknownAttr' is not allowed to appear in element 'bpmn2:definitions'/); | ||
// and line number | ||
expect(err.message).toMatch(/\(1:476\)/); | ||
done(); | ||
@@ -110,2 +117,29 @@ }); | ||
describe('should validate { file }', function() { | ||
it('correct', function(done) { | ||
validator.validateXML({ file: BPMN_FILE }, BPMN_SCHEMA, function(err, result) { | ||
if (err) { | ||
done(err); | ||
} else { | ||
expect(result.valid).toBe(true); | ||
done(); | ||
} | ||
}); | ||
}); | ||
it('broken', function(done) { | ||
validator.validateXML({ file: INVALID_BPMN_FILE }, BPMN_SCHEMA, function(err, result) { | ||
expect(err).toBeDefined(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
88028
266
63