🇺🇸 The documentation of this project is in spanish as this is the natural language for intended audience.
🇲🇽 La documentación del proyecto está en español porque ese es el lenguaje principal de los usuarios.
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
Instala el paquete en tu proyecto:
# pnpm
pnpm add @nodecfdi/xml-schema-validator
# npm
npm install @nodecfdi/xml-schema-validator
# yarn
yarn add @nodecfdi/xml-schema-validator
# bun
bun add @nodecfdi/xml-schema-validatorValida un documento XML contra un esquema XSD local:
import { readFileSync } from 'node:fs';
import { Schemas, SchemaValidator } from '@nodecfdi/xml-schema-validator';
const xmlContent = readFileSync('./factura.xml', 'utf8');
const validator = SchemaValidator.createFromString(xmlContent);
const schemas = new Schemas();
schemas.create('http://www.sat.gob.mx/cfd/4', './xsd/cfdv40.xsd');
if (!validator.validate(schemas)) {
console.error(validator.getLastError());
}Por diseño, rendimiento y seguridad (evitar peticiones de red implícitas/SSRF y no bloquear el event loop de Node.js), esta librería solo carga esquemas XSD locales.
Si necesitas validar documentos XML que referencian esquemas remotos (http:// o https://), utiliza @nodecfdi/xml-resource-retriever para descargar y almacenar localmente los esquemas junto con todas sus dependencias:
import { XsdRetriever } from '@nodecfdi/xml-resource-retriever';
import { SchemaValidator } from '@nodecfdi/xml-schema-validator';
const validator = SchemaValidator.createFromString(xmlContent);
const retriever = new XsdRetriever('./storage');
const schemas = validator.buildSchemas();
for (const schema of schemas.values()) {
const localPath = await retriever.retrieve(schema.getLocation());
schemas.create(schema.getNamespace(), localPath);
}
validator.validateWithSchemas(schemas);La documentación está disponible en el sitio web NodeCfdi
Puedes obtener soporte abriendo un ticket en Github.
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.
Las contribuciones con bienvenidas. Por favor lee CONTRIBUTING para más detalles y recuerda revisar el archivo CHANGELOG.
The @nodecfdi/xml-schema-validator library is copyright © NodeCfdi and licensed for use under the MIT License (MIT). Please see LICENSE for more information.