<commit_before>var express = require('express');
var Invoice = require('../models/Invoice')

var router = express.Router();

/* Validation Middleware. */
function validateInvoice(req, res, next) {
  req.checkBody('Document', 'Document can not be empty').notEmpty();
  req.checkBody('ReferenceYear', 'Year must be a number').isNumeric();

  var errors = req.validationErrors();
  if (errors) {
    var response = {errors: []};
    errors.forEach(function(error) {
      response.errors.push(error.msg);
    });

    res.statusCode = 400;
    return res.json(response);
  }
  return next();
}

/* GET single Invoice Middleware. */
function lookupInvoice(req, res, next) {
  var id = req.params.id;
  Invoice.getInvoiceById(id, function(error, results){
    if (error) {
      console.error(error);
      res.statusCode = 500;
      return res.json({errors: ['Could not retrieve Invoice.']});
    }
    if (results.length === 0) {
      res.statusCode = 404;
      return res.json({errors: ['Invoice not found.']});
    }
    req.invoice = results[0];
    next();
  });<commit_msg>:rocket: Add Validation en Sanitization for Invoice<commit_after>var express = require('express');
var Invoice = require('../models/Invoice')

var router = express.Router();

/* Validation Middleware. */
function validateInvoice(req, res, next) {
  req.checkBody('ReferenceMonth', 'Month must be an integer between [1, 12]').isInt({min: 1, max: 12});
  req.checkBody('ReferenceYear', 'Year must be a positive integer number').isInt({min: 0});
  req.checkBody('Document', 'Document cannot be empty').notEmpty();
  req.checkBody('Document', 'Document cannot be greater than 14 characters').isLength({max: 14});
  req.checkBody('Amount', 'Amount must be a valid currency (separators: decimal (.), thousands (,)').isCurrency();
  req.checkBody('IsActive', 'IsActive must be 0 (False) or 1 (True)').isIn([0, 1]);

  req.getValidationResult().then(function(result) {
    if (!result.isEmpty()) {
      res.statusCode = 400;
      var response = {errors: result.array()};
      return res.json(response);
    } else {
      req.sanitize('Amount').blacklist(',');
      return next();
    }
  });
}

/* GET single Invoice Middleware. */
function lookupInvoice(req, res, next) {
  var id = req.params.id;
  Invoice.getInvoiceById(id, function(error, results){
    if (error) {
      console.error(error);
      res.statusCode = 500;
      return res.json({errors: ['Could not retrieve Invoice.']});
    }
    if (results.length === 0) {
      res.statusCode = 404;
      return res.json({errors: ['Invoice not found.']});
    }
    req.invoice = results[0];
    next();
  });
{'fraction_non_alphanumeric': 0.16754916792738275, 'fraction_numerical': 0.01399394856278366, 'mean_word_length': 3.6403508771929824, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2604683', 'n_tokens_mistral': 853, 'n_tokens_neox': 807, 'n_words': 213}