-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
83 lines (74 loc) · 2.82 KB
/
index.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
/**
* Node.js bindings for XSD validation from libxml
* @module libxml-xsd
*/
var fs = require('fs');
var libxmljs = require('libxmljs2');
var binding = require('bindings')('node-libxml-xsd');
/**
* The libxmljs module. Prevents the need for a user's code to require it a second time. Also prevent weird bugs.
*/
exports.libxmljs = libxmljs;
/**
* A compiled schema. Do not call this constructor, instead use parse or parseFile.
*
* store both the source document and the parsed schema
* if we don't store the schema doc it will be deleted by garbage collector and it will result in segfaults.
*
* @constructor
* @param {Document} schemaDoc - XML document source of the schema
* @param {Document} schemaObj - Simple wrapper of a XSD schema
*/
var Schema = function(schemaDoc, schemaObj){
this.schemaDoc = schemaDoc;
this.schemaObj = schemaObj;
};
/**
* Parse a XSD schema
*
* @param {string|Document} source - The content of the schema as a string or a [libxmljs document]{@link https://github.com/polotek/libxmljs/wiki/Document}
* @param {object} options - [The parsing options]{@link https://github.com/marudor/libxmljs2/blob/a61fdccf87a2928c084a3ec3e2a44f163998d81b/index.d.ts#L11}
* @returns The parsed Schema
*/
exports.parse = function(source, options) {
// schema can be given as a string or a pre-parsed xml document
if (typeof source === 'string') {
source = libxmljs.parseXml(source, options);
}
return new Schema(source, binding.schemaSync(source));
};
/**
* Parse a XSD schema
*
* @param {stringPath} sourcePath - The path of the file
* @param {object} options - [The parsing options]{@link https://github.com/marudor/libxmljs2/blob/a61fdccf87a2928c084a3ec3e2a44f163998d81b/index.d.ts#L11}
* @returns The parsed Schema
*/
exports.parseFile = function(sourcePath, options) {
var data = fs.readFileSync(sourcePath, 'utf8');
return exports.parse(data.toString(), options);
};
/**
* Validate a XML document over a schema
*
* @param {string|Document} source - The XML content to validate with the schema, to be given as a string or a [libxmljs document]{@link https://github.com/polotek/libxmljs/wiki/Document}
* @return An array of validation errors, or null if document is valid
*/
Schema.prototype.validate = function(source) {
// xml can be given as a string or a pre-parsed xml document
if (typeof source === 'string') {
source = libxmljs.parseXml(source);
}
var validationErrors = binding.validateSync(this.schemaObj, source);
return validationErrors.length > 0 ? validationErrors : null;
};
/**
* Apply a schema to a XML file
*
* @param {string} sourcePath - The path of the file to read
* @return An array of validation errors, or null if document is valid
*/
Schema.prototype.validateFile = function(sourcePath) {
var data = fs.readFileSync(sourcePath, 'utf8');
return this.validate(data.toString());
};