Git Product home page Git Product logo

digitaliapl / fatturapa Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 3.0 214 KB

TypeScript library for bidirectional (XML-JS/JSON-XML) conversion and validation of italian electronic invoices (FatturaElettronica)

Home Page: https://www.npmjs.com/package/@digitalia/fatturapa

License: European Union Public License 1.2

TypeScript 99.51% JavaScript 0.49%
italian-electronic-invoices json fattura-elettronica fattura-pa fatturapa electronic-invoicing xml-parsing xml2json json2xml

fatturapa's Introduction

@digitalia/fatturapa

Isomorphic JavaScript library for handling Italian electronic invoice FatturaPA XML to JS/JSON and JS/JSON to XML conversions and validation.

Libreria JavaScript isomorfica per la gestione della validazione e conversione bidirezionale della fattura elettronica FatturaPA da XML a JS/JSON e da JS/JSON a XML.

Bundle

The package is made of 2 sub-modules

  • parser : Contains all the parsing functions (fpa2js, fpa2xml) - ~10.68KB
  • validator: Contains the validation function fpaValidate and the FatturaPA Yup object schema FPAYupSchema - ~25.20KB

How to use

Just install it as dependency via $ npm install @digitalia/fatturapa or $ yarn add @digitalia/fatturapa. The library works either in NodeJS or browser environments, making it possible to implement different data management strategies and easily integrate with pre-exisisting software systems.

Semplicemente installando come dipendenza via $ npm install @digitalia/fatturapa o $ yarn add @digitalia/fatturapa. La libreria funziona sia in ambiente NodeJS che browser, rendendo possibile l'implementazione di differenti strategie di gestione dei dati e l'integrazione con sistemi software pre-esistenti.

Conversion

The conversion module exports the 3 functions:

Il modulo di conversione esporta le 3 funzioni:

  • fpa2js - XML => JS Object
  • fpa2xml - JS Object => XML
  • fpa2json - XML => JSON string

Each function runs, by default, a syntactic XML pre-validation against the W3C Schema. This is meant to avoid inconsistencies and wrong outputs. If you're sure the data input is correct, you can opt-out the pre-validation. To opt-out the XML pre-validation, just set the 'validate' property to false in the configuration object.

Tutte le funzioni effettuano, di default, una pre-validazione sintattica dell'XML secondo lo schema W3C. Questo serve ad evitare inconsistenze ed errori in output. Se i dati XML sono sicuramente corretti, puoi disattivare la pre-validazione. Per disattivare la pre-validazione XML, imposta la proprietà 'validate' a false nell'oggetto di configurazione.

fpa2whatever(data, { validate: false });

XML to JS Object

import { fpa2js } from '@digitalia/fatturapa';

let xmlData = `
  <?xml version="1.0" encoding="UTF-8"?>
  <ns:FatturaElettronica versione="FPA12">
    <FatturaElettronicaHeader>
    .....
    </FatturaElettronicaHeader>
    <FatturaElettronicaBody>
    .....
    </FatturaElettronicaBody>
  </p:FatturaElettronica>
`;

let invoice = fpa2js(xmlData, { validate: false, valuesOnly: false });

console.log('Invoice :', invoice);

JSON to XML

import { fpa2xml } from '@digitalia/fatturapa';

let jsonData = `
  {
    ns:FatturaElettronica: {
      '@': { versione: 'FPA12', .... },
      FatturaElettronicaHeader: { .... },
      FatturaElettronicaBody: { .... }
    }
  }
`;

let invoice = fpa2xml(jsonData);

console.log('Invoice :', invoice);

Validation

The module also exports a yup object schema FPAYupSchema along with a fpaValidate function to check the document validity against the FatturaPA specs

Il modulo esporta anche l'oggetto FPAYupSchema contenente lo schema yup e la funzione fpaValidate per il controllo di validità del documento secondo le specifiche FatturaPA

JS/JSON Validation

Example in React with Formik
import React from 'react';
import { Formik, Form, Field } from 'formik';
import * as Yup from 'yup';
import { FPAYupSchema } from '@digitalia/fatturapa';

const ClientSideForm = () => {
  return (
    <Formik initalValues={{...}} validationSchema={FPAYupSchema}>
      {({errors, touched}) => (
        <Form>
          ....
          <Field name='IdFiscaleIVA'>
            {errors.IdFiscaleIVA && touched.IdFiscaleIVA ? (<div>{errors.IdFiscaleIVA}</div>) : null }
          </Field>
          .....
        </Form>
      )}
    </Formik>
  )
}
Example in Node/JS
import {fpaValidate, FPAYupSchema} from '@digitalia/fatturapa'

let invoice = {
  FatturaElettronica: {
    '@': { versione: 'FPA12', .... },
    FatturaElettronicaHeader: { .... },
    FatturaElettronicaBody: { .... }
  }
}

let isValid = fpaValidate(invoice, FPAYupSchema)

XML Validation

To validate an XML file, you must convert it to a JS object via the fpa2js() function above mentioned.

Per validare un file XML, è necessario prima convertirlo in un oggetto JS tramite la funzione fpa2js() menzionata sopra.

import { fpa2js, fpaValidate, FatturaPASchema } from '@digitalia/fatturapa';

let xmlData = `
  <?xml version="1.0" encoding="UTF-8"?>
  <ns:FatturaElettronica versione="FPA12">
    <FatturaElettronicaHeader>
    .....
    </FatturaElettronicaHeader>
    <FatturaElettronicaBody>
    .....
    </FatturaElettronicaBody>
  </p:FatturaElettronica>
`;

let isValid = fpaValidate(fpa2js(xmlData), FPAYupSchema);

Credits

This project was bootstrapped with TSDX and makes use of the great fast-xml-parser library under the hood.

fatturapa's People

Contributors

djr3 avatar marco-mastella avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

fatturapa's Issues

NaturaType

According to fex documentation the natura type can be also N2.1,N2.2 and so on.
At the moment the library supports only the "primary codes".

Would be possible to add those types too?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.