Skip to content

About

Librería para validaciones de XML con Esquemas XSD

Resources

Code of conduct

Contributing

Security policy

Stars

4 stars

Watchers

0 watching

Forks

Repository files navigation

@nodecfdi/xml-schema-validator

Source Code Npm Node Version Support Discord Latest Version Software License Build Status Reliability Maintainability Code Coverage Violations Total Downloads

🇺🇸 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.

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.

  1. Recibe un string xml válido con el contenido a ser evaluado.
  2. Escanear el archivo por cada schemaLocation.
  3. Generar un esquema que incluya todos los esquemas.
  4. Validar el xml nuevamente con el esquema generado.

Librería inspirada por la versión para php https://github.com/eclipxe13/XmlSchemaValidator

Inicio rápido

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-validator

Valida 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());
}

Esquemas locales y remotos

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);

Documentación

La documentación está disponible en el sitio web NodeCfdi

Soporte

Puedes obtener soporte abriendo un ticket en Github.

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 and licensed for use under the MIT License (MIT). Please see LICENSE for more information.

About

Librería para validaciones de XML con Esquemas XSD

Resources

Code of conduct

Contributing

Security policy

Stars

4 stars

Watchers

0 watching

Forks

Releases

Used by

Contributors

Languages