{"id":19952197,"url":"https://github.com/timluq/schematron-runner","last_synced_at":"2025-06-11T05:32:49.175Z","repository":{"id":94638592,"uuid":"161989061","full_name":"TimLuq/schematron-runner","owner":"TimLuq","description":null,"archived":false,"fork":false,"pushed_at":"2019-03-29T12:16:37.000Z","size":353,"stargazers_count":3,"open_issues_count":3,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-04T04:19:21.801Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TimLuq.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-12-16T10:14:11.000Z","updated_at":"2024-11-08T15:45:18.000Z","dependencies_parsed_at":null,"dependency_job_id":"b7900e52-e9fe-4d96-8716-6b56127f1c9c","html_url":"https://github.com/TimLuq/schematron-runner","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimLuq%2Fschematron-runner","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimLuq%2Fschematron-runner/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimLuq%2Fschematron-runner/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimLuq%2Fschematron-runner/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TimLuq","download_url":"https://codeload.github.com/TimLuq/schematron-runner/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimLuq%2Fschematron-runner/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259207656,"owners_count":22821733,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T01:12:07.765Z","updated_at":"2025-06-11T05:32:49.145Z","avatar_url":"https://github.com/TimLuq.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# schematron-runner\n[![Build Status](https://travis-ci.org/TimLuq/schematron-runner.svg?branch=master)](https://travis-ci.org/TimLuq/schematron-runner)\n[![npm version](https://img.shields.io/npm/v/schematron-runner.svg)](https://npm.im/schematron-runner)\n[![license](https://img.shields.io/npm/l/schematron-runner.svg)](https://npm.im/schematron-runner)\n[![dependencies Status](https://david-dm.org/TimLuq/schematron-runner/status.svg)](https://david-dm.org/TimLuq/schematron-runner)\n[![Coverage Status](https://coveralls.io/repos/github/TimLuq/schematron-runner/badge.svg?branch=master)](https://coveralls.io/github/TimLuq/schematron-runner?branch=master)\n\nA javascript implementation of schematron testing for XML documents. This specifically resolves a need for a package that allows a quick, reliable install for validating HL7 clinical documents, such as C-CDA.\n\nDue to `schematron-runner` being written in TypeScript. This grants typing and typechecking possibilities if this project is used as a libary.\n\n## Table of Contents\n\n* [Install](#install)\n  * [Prebuilt files](#prebuilt-files)\n* [CLI](#cli)\n* [API](#api)\n  * [Example](#example-validating-xml)\n  * [Interface: Options](#interface-options)\n  * [Interface: Results](#interface-results)\n  * [clearCache()](#clearcache)\n  * [validate(xml, schematron[, options])](#validate-xml-schematron-options)\n  * [validateFocused(xml, schematron, defaults[, options])](#validatefocused-xml-schematron-defaults-options)\n  * [parseSchematron(document)](#parseschematron-document)\n  * [polymorphicDefaults(field, type)](#polymorphicDefaults-field-type)\n  * [throwDefaults(field, type)](#throwdefaults-field-type)\n  * [webDefaults(field, type)](#webDefaults-field-type)\n* [License (MIT)](#license-mit)\n\n## Install\n\nFor the CLI application to be available system wide you may wish to install globally.\nIf you wish to use it for a project a module installation is recommended.\n\nUsing `npm`:\n```shell\nnpm install schematron-runner # project\nnpm install -g schematron-runner # global\n```\n\nUsing `yarn`:\n```shell\nyarn add schematron-runner # global\nyarn global add schematron-runner #project\n```\n\nBuilding from source:\n```shell\ngit clone git+https://github.com/TimLuq/schematron-runner.git\ncd schematron-runner\nnpm install\n```\n\n### Prebuilt files\n\nThe published packages includes a number of files in the `build` directory. I recommend you to use a bundler, such as `rollup` or `webpack`, if your project is an end product. But these files are provided for other uses.\n\n- `build/bin.js`\n    This file is the CLI program. When installing the package using `yarn` or `npm` this is registerad as te application `schematron-runner`.\n- `build/schematron-browser.js`\n    This file is a prebuilt UMD version for web use. It removes the polymorphic options and only exposes web compatible versions of all exposed APIs.\n- `build/schematron-runner.js`\n    This file is a CommonJS module optimized for Node 8+.\n- `build/schematron-runner.mjs`\n    This file is a ES6 module additionally featuring dynamic imports. This may be used by bundlers, Node's ES module loader or modern browsers using `\u003cscript type=\"module\" src=\"schematron-runner.mjs\"\u003e\u003c/script\u003e`.\n\n## CLI\n\nAfter installation (or by running `npx`) an executable program named `schematron-runner` is available.\n\nFor information about CLI usage run `schematron-runner --help`.\n\n## API\n\nThe following APIs are available.\n\n### Example: Validating xml\n```javascript\nimport { validate, validateFocused, webDefaults } from \"schematron-runner\";\nimport { promises as fs } from \"fs\";\n\nconst xmlPath = 'someFile.xml';\nconst schematronPath = 'someFile.sch';\n\n(async () =\u003e {\n    const xml = await fs.readFile(xmlPath, \"utf8\");\n    const schematron = await fs.readFile(schematronPath, \"utf8\");\n\n    const results = await validate(xml, schematron);\n    // do stuff with results\n})();\n```\nFile paths can also be passed to the validator directly. The following lines all return the same results:\n```javascript\nconst results0 = await validate(xml, schematronPath);\nconst results1 = await validate(xmlPath, schematron);\nconst results2 = await validate(xmlPath, schematronPath);\n```\n\n### Interface: Options\n\n- `options.excludeWarnings` [\u0026lt;boolean\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) Determines whether or not warnings should be tested and returned. If this is set to `true` the `result.warnings` array will be empty and the assertions that are detected as warnings will not be included in `result.passed`. *Defaults to `false`.*\n- `options.resourceDir` [\u0026lt;string\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) The path to a directory containing resource files (eg. `voc.xml`) which may be necessary for some schematron tests. *Defaults to `'./'`, the current directory.*\n- `options.xmlSnippetMaxLength` [\u0026lt;number\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) An integer which is the maximum length of the `xml` field in validation results. Set to `0` for unlimited length. *Defaults to `200`.*\n- `options.DOMParser` [\u0026lt;Promise\u0026lt;class DOMParser\u0026gt;\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) | [\u0026lt;class DOMParser\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/API/DOMParser) Class to use to parse XML documents. *Defaults to global `DOMParser` or one provided by the `xmldom` package.*\n- `options.hashFunction` [\u0026lt;Promise\u0026lt;(string) =\u003e Promise\u0026lt;string\u0026gt;\u0026gt;\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) | [\u0026lt;(string) =\u003e Promise\u0026lt;string\u0026gt;\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) Function used to hash content to a key representing the data. Used for creating caching keys. *Defaults to an SHA1 implementation using `crypto.subtle` or the `crypto` package.*\n- `options.loadXMLFile` [\u0026lt;(options, string) =\u003e Promise\u0026lt;Document\u0026gt;\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) Function used to load file paths. Will throw when using the `webDefaults`. *Using `polymorphicDefaults` the default is to use the `fs` package.*\n- `options.loadXMLUrl` [\u0026lt;(options, string) =\u003e Promise\u0026lt;Document\u0026gt;\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) Function used to load URL documents. *Defaults to using global `fetch` and `polymorphicDefaults` falls back upon the `node-fetch` package.*\n\n\nThe [`validate`](#validate-xml-schematron-options) and [`validateFocused`](#validatefocused-xml-schematron-defaults-options) functions takes an `options` object as an optional argument. These options may be used to change the behavior of the validation.\n\nBelow is an example with warnings disabled and using web compatible defaults:\n\n```javascript\nconst results = await validateFocused(xml, schematron, webDefaults(), {\n    excludeWarnings: true\n});\n```\n\n### Interface: Results\n\n- `results.errors` [\u0026lt;Object[]\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) An array of error objects.\n- `results.ignored` [\u0026lt;Object[]\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) An array of ingored objects.\n- `results.passed` [\u0026lt;Object[]\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) An array of passed objects.\n- `results.warnings` [\u0026lt;Object[]\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) An array of warning objects.\n\nThe `results` object contains arrays `errors`, `warnings`, `passed` and `ignored`.\n\nThe `errors`, `passed`, and `warnings` arrays are reported as determined by the schematron and test descriptions.\nThey are instances of the following form:\n```javascript\n{\n    type: type,                     // \"error\" or \"warning\"\n    test: test,                     // xpath test\n    simplifiedTest: simplifiedTest, // xpath test with resource values included, if applicable, null otherwise\n    description: description,       // schematron description of the test case\n    line: line,                     // line number of the violating context\n    path: path,                     // xpath path of the violating context\n    patternId: patternId,           // schematron-assigned pattern id\n    ruleId: ruleId,                 // schematron-assigned rule id\n    assertionId: assertionId,       // schematron-assigned assertion id\n    context: context,               // xpath context of the rule\n    xml: xml                        // xml snippet of the violating context\n}\n```\n\nThe `ignored` tests are those that resulted in an exception while running (eg. the test is invalid xpath and could not be parsed properly) and require manual inspection. They are of the following form:\n```javascript\n{\n    errorMessage: errorMessage,     // reason for the exception/ignoring the test\n    type: type,                     // \"error\" or \"warning\"\n    test: test,                     // xpath test\n    simplifiedTest: simplifiedTest, // xpath test with resource values included, if applicable, null otherwise\n    description: description,       // schematron description of the test case\n    patternId: patternId,           // schematron-assigned pattern id\n    ruleId: ruleId,                 // schematron-assigned rule id\n    assertionId: assertionId,       // schematron-assigned assertion id\n    context: context,               // xpath context of the rule\n}\n```\n\n### clearCache ()\n\nThe validator uses a cache to store parsed schematrons, an intermediate data structure used to store revelant schematron information. This reduces the runtime of the validator when validating against the same schematron multiple times.\n\n### validate (xml, schematron[, options])\n\n- `xml` [\u0026lt;string\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) The string path, url or contents representing an XML document to be validated.\n- `schematron` [\u0026lt;string\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) The string path, url or contents representing a Schematron XML document to use for validation.\n- `options` [\u0026lt;Options\u0026gt;](#interface-options) Optional options object overriding default processing.\n- Returns: [\u0026lt;Promise\u0026lt;Results\u0026gt;\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)\n\nEquivalent of calling `validateFocused(xml, schematron, polymorphicDefaults(), options)`. See [Interface: Results](#interface-results) for description of the results object.\n\n### validateFocused (xml, schematron, defaults[, options])\n\n- `xml` [\u0026lt;string\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) The string path, url or contents representing an XML document to be validated.\n- `schematron` [\u0026lt;string\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) The string path, url or contents representing a Schematron XML document to use for validation.\n- `defaults` [\u0026lt;Function\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) Function used to fill default options where nothing is provided. See [polymorphicDefaults](#polymorphicdefaults-field-type), [throwDefaults](#throwdefaults-field-type), and [webDefaults](#webdefaults-field-type). Custom functions should fallback upon one of the three provided functions.\n- `options` [\u0026lt;Options\u0026gt;](#interface-options) Optional options object overriding default processing.\n- Returns: [\u0026lt;Promise\u0026lt;Results\u0026gt;\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)\n\nValidates a document against a schematron ruleset.\nSee [Interface: Results](#interface-results) for description of the results object.\n\n### parseSchematron (document)\n\n- `document` [\u0026lt;Document\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/API/Document) Schematron DOM document.\n- Returns: [\u0026lt;Object\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) The parsed information representing the schematron document.\n\nParse a schematron document to an internal representation. Might be usefull for displaying the discovered parts of a schematron file.\n\n### polymorphicDefaults (field, type)\n\n- `field` [\u0026lt;string\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) The name of the option to provide a default value for.\n- `type` [\u0026lt;string\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) The type of the option provided by the base options object.\n\nThe provider will prefer gobal objects as if in a web context. If the global objects are not set it will load external packages to provide the functionality.\n\nFor the prebuilt `build/schematron-browser.js` this is an alias for [webDefaults](#webdefaults-field-type).\n\n### throwDefaults (field, type)\n\n- `field` [\u0026lt;string\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) The name of the option to provide a default value for.\n- `type` [\u0026lt;string\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) The type of the option provided by the base options object.\n\nThe provider will throw when called. A full options object must have been used.\n\n### webDefaults (field, type)\n\n- `field` [\u0026lt;string\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) The name of the option to provide a default value for.\n- `type` [\u0026lt;string\u0026gt;](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) The type of the option provided by the base options object.\n\nThe provider will use gobal objects as if in a web context. If the global objects are not set it will throw or result in an undefined state.\n\n---\n## License (MIT)\n\nCopyright \u0026copy; 2017 [Eric Wadkins](http://www.ericwadkins.com/), 2018-2019 [Tim Lundqvist](https://github.com/TimLuq/).\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimluq%2Fschematron-runner","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimluq%2Fschematron-runner","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimluq%2Fschematron-runner/lists"}