detailed-xml-validator
Advanced tools
Comparing version
{ | ||
"name": "detailed-xml-validator", | ||
"version": "0.0.7", | ||
"version": "1.0.0", | ||
"description": "Validate for XML schema and returns all the possible failures", | ||
@@ -5,0 +5,0 @@ "main": "./src/validator.js", |
@@ -27,7 +27,9 @@ # detailed-xml-validator | ||
<marks> | ||
<subject repeatable minOccurs="5" maxOccurs="6" > | ||
<subject repeatable minOccurs="5" maxOccurs="6" checkBy="subjectValidator"> | ||
<name pattern="math|hindi|english|science|history"></name> | ||
<!-- <name in="math,hindi,english,science,history"></name> --> | ||
<score type="positiveDecimal"></score> | ||
</subject> | ||
</marks> | ||
</student> | ||
@@ -58,7 +60,11 @@ </students> | ||
* **length**: | ||
* **pattern**: regex | ||
* **in**: comma separated string for exact match (from v1.0.0) | ||
* **fixed**: exact match (from v1.0.0) | ||
* **pattern**: regex match | ||
* **pattern_i**: regex (case insensitive) | ||
* **pattern_m**: regex (multiline) | ||
* **pattern_im**: regex (case insencitive and multiline) | ||
* **checkBy**: (from v1.0.0) Give the name of validator that you registered with validator. This validator will be called with an object of nested tags (or value if it is a leaf node) and path. | ||
Sample code | ||
@@ -74,2 +80,7 @@ ```js | ||
const validator = new Validator(rules, options); | ||
validator.register("subjectValidator", (obj, path) => { //From v1.0.0 | ||
//return; //if no error | ||
//return {} //return an error msg object | ||
}) | ||
const failures = validator.validate(xmlStringData); | ||
@@ -76,0 +87,0 @@ const originalXmlJsObj = validator.data; |
@@ -9,5 +9,6 @@ /// @ts-check | ||
class Traverser{ | ||
constructor(options){ | ||
constructor(options, validators){ | ||
this.options = options; | ||
this.failures = []; | ||
this.validators=validators; | ||
} | ||
@@ -50,5 +51,15 @@ | ||
} | ||
this.applyCustomValidators(ele, rules, path); | ||
} | ||
} | ||
applyCustomValidators(ele, rules, path){ | ||
if(rules['@rules'] && rules['@rules'].checkBy && typeof this.validators[rules['@rules'].checkBy] === "function"){ | ||
let res = this.validators[rules['@rules'].checkBy](ele, path.substr(1)); | ||
if(typeof res === "object"){ | ||
this.failures.push(res); | ||
} | ||
} | ||
} | ||
callForCommonProperties(ele, rules, path){ | ||
@@ -62,2 +73,3 @@ const tags = Object.keys(ele); | ||
this.applyCustomValidators(ele, rules, path); | ||
sets.common.forEach(key => { | ||
@@ -203,2 +215,10 @@ const newpath = path + "." + key; | ||
}); | ||
["fixed","in"].forEach( rule => { | ||
if(rules[rule] !== undefined){ | ||
const expected = rules[rule]; | ||
if( !validations.string[rule](expected, actual) ){ | ||
this.setInvalidValueError(rule, newpath, actual, expected); | ||
}; | ||
} | ||
}); | ||
// if(rules.pattern){ | ||
@@ -205,0 +225,0 @@ // const expected = rules[rule]; |
@@ -14,2 +14,8 @@ module.exports.string = { | ||
return regxp.test(actual); | ||
}, | ||
fixed: function(expected, actual){ | ||
return expected === actual; | ||
}, | ||
in: function(expected, actual){ | ||
return expected.split(",").indexOf(actual) > -1 | ||
} | ||
@@ -26,3 +32,2 @@ } | ||
module.exports.list = { | ||
minOccurs: function(expected, actual){ | ||
@@ -29,0 +34,0 @@ return actual >= expected; |
@@ -20,4 +20,8 @@ /// @ts-check | ||
this.options = Object.assign({}, defaultOptions, options); | ||
this.validators={}; | ||
} | ||
register(validator, fn){ | ||
this.validators[validator] = fn; | ||
} | ||
validate(xmldata){ | ||
@@ -32,3 +36,3 @@ validateXMlData(xmldata); | ||
this.data = xmlObj; | ||
const traverser = new Traverser(this.options); | ||
const traverser = new Traverser(this.options,this.validators); | ||
traverser.traverse (xmlObj, "", this.rules, ""); | ||
@@ -35,0 +39,0 @@ return traverser.failures; |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
63017
25.08%25
13.64%1574
30.84%1
-50%106
11.58%6
20%