@nodecfdi/xml-schema-validator v1.1.1
@nodecfdi/xml-schema-validator
Library for XML Schema Validations
:us: The documentation of this project is in spanish as this is the natural language for intended audience.
:mexico: La documentación del proyecto está en español porque ese es el lenguaje principal de los usuarios.
Acerca de @nodecfdi/xml-schema-validator
Esta es una librería para validar archivos XML a través de multiples Esquemas XSD acorde a sus definiciones.
- Recibe un string xml válido con el contenido a ser evaluado.
- Escanear el archivo por cada schemaLocation.
- Generar un esquema que incluya todos los esquemas.
- Validar el xml nuevamente con el esquema generado.
Librería inspirada por la versión para php https://github.com/eclipxe13/XmlSchemaValidator
Advertencia
Actualmente solo está soportada la carga de esquema local, no funciona con esquemas remotos, debido a incompatibilidad con la lib actual.
Instalación
NPM
npm i @nodecfdi/xml-schema-validator --save
YARN
yarn add @nodecfdi/xml-schema-validator
PNPM
pnpm add @nodecfdi/xml-schema-validator
Ejemplo básico de uso usando xmldom
import { readFileSync } from 'fs';
import { SchemaValidator } from "@nodecfdi/xml-schema-validator";
import { install } from '@nodecfdi/cfdiutils-common';
import { DOMImplementation, DOMParser, XMLSerializer } from '@xmldom/xmldom';
// from version 1.2.x on @nodecfdi/cfdiutils-common required install dom resolution
install(new DOMParser(), new XMLSerializer(), new DOMImplementation());
const contents = readFileSync('example.xml', 'binary');
// expect references on schemalocations are locally files
const validator = SchemaValidator.createFromString(contents);
if (!validator.validate()){
console.log(`Found error: ${validator.getLastError()}`);
}
Ejemplo avanzado de uso usando xmldom
import { getParser, install } from '@nodecfdi/cfdiutils-common';
import { DOMImplementation, DOMParser, XMLSerializer } from '@xmldom/xmldom';
import { SchemaValidator } from "@nodecfdi/xml-schema-validator";
// from version 1.2.x on @nodecfdi/cfdiutils-common required install dom resolution
install(new DOMParser(), new XMLSerializer(), new DOMImplementation());
// create SchemaValidator using a Document
const docParse = getParser().parseFromString('example.xml', 'text/xml');
const validator = new SchemaValidator(docParse);
// change schemas collection to override the schema location of a specific namespace
const schemas = validator.buildSchemas();
schemas.create('http://example.org/schemas/x1', './local-schemas/x1.xsd');
// validateWithSchemas does not return boolean, it throws an exception
try{
validator.validateWithSchemas(schemas);
}catch (e) {
console.log(`Found error: ${e.message}`);
}
// or validate with boolean
if (!validator.validate(schemas)){
console.log(`Found error: ${validator.getLastError()}`);
}
Soporte
Puedes obtener soporte abriendo un ticket en Github.
Adicionalmente, esta librería pertenece a la comunidad OcelotlStudio, así que puedes usar los mismos canales de comunicación para obtener ayuda de algún miembro de la comunidad.
Compatibilidad
Esta librería se mantendrá compatible con al menos la versión con soporte activo de Node más reciente.
También utilizamos Versionado Semántico 2.0.0 por lo que puedes usar esta librería sin temor a romper tu aplicación.
Contribuciones
Las contribuciones con bienvenidas. Por favor lee CONTRIBUTING para más detalles y recuerda revisar el archivo CHANGELOG.
Copyright and License
The @nodecfdi/xml-schema-validator
library is copyright © NodeCfdi - OcelotlStudio and licensed for use under the MIT License (MIT). Please see LICENSE for more information.