https://github.com/naturalintelligence/detailed-xml-validator
Validate for XML schema and returns all the possible failures
https://github.com/naturalintelligence/detailed-xml-validator
Last synced: about 1 year ago
JSON representation
Validate for XML schema and returns all the possible failures
- Host: GitHub
- URL: https://github.com/naturalintelligence/detailed-xml-validator
- Owner: NaturalIntelligence
- License: mit
- Created: 2021-09-27T03:33:10.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-04-09T10:01:51.000Z (about 3 years ago)
- Last Synced: 2025-04-23T13:41:42.042Z (about 1 year ago)
- Language: JavaScript
- Size: 54.7 KB
- Stars: 17
- Watchers: 4
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# detailed-xml-validator
Validate for XML schema and returns all the possible failures
This module uses it's own rule file which is different than XSD and looks more like XML data file. More features would be added in future versions. Currently, it just ensures frequency, type, range, value length, value pattern and null validations only on top of syntax check done by FXP.
If there is no syntax error, then this module reports all failures and don't exit on first faliure. So you can report all the issues in one go.
Sample Rules file
```xml
<:a>
```
* **:a**: This is the special tag used to define rules for attributes
* **nillable**: By default all the elements are nillable. Set it to `false` to mark an element mandatory. For lists, if `minOccurs` is set to `1`, it means it can't be nillable.
* **repeatable**: A list must have this attribute
* **minOccurs**: To validate minimum number of elements in a list
* **maxOccurs**: To validate maximum number of elements in a list
* **type**: You can define type to put the restriction on their values. Eg `positiveInteger` can't have negative values. Following types are supported
* **positiveInteger** :
* **positiveDecimal** :
* **integer** :
* **decimal** :
* **number** :
* **date** :
* **string** : default type (optional)
* **map** : object type (optional)
* **Number type**: Following validations can be applied on number type
* **min**:
* **max**:
* **String type**: Following validations can be applied on string type
* **minLength**:
* **maxLength**:
* **length**:
* **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
```js
const Validator = require("detailed-xml-validator");
const options = {
unknownAllow: true,
boolean: ["true", "false"],
};
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);
const originalXmlJsObj = validator.data;
console.log(`Found ${failures.length} issues`);
```
Sample Response
```js
[
{ code: 'missing', path: 'root.d'} ,
{ code: 'unknown', path: 'root.f'}
{ code: 'minLength', path: 'root.a[0]', actual: '0', expected: 15 },
{
code: 'pattern',
path: 'root.a[0]',
actual: '0',
expected: '[a-z]+@gmail.com'
},
{ code: 'not a boolean', path: 'root.a[0]', value: 'yes' },
{ code: 'not a integer', path: 'root.f[2]', value: 'acbc' },
{ code: 'max', path: 'root.d', actual: 3.2, expected: 1.5 },
{ code: 'unexpected value in a map', path: 'root.b[1]', value: 'amit' }
]
```