upn-library-v2 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +16 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "upn-library-v2",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "main": "src/index.js",
5
5
  "keywords": [
6
6
  "UPN",
package/src/index.js CHANGED
@@ -29,20 +29,30 @@ function generateText(fields) {
29
29
  }, "") + calculateControlSum(fields) + "";
30
30
  }
31
31
 
32
- function validateOption(object, option, level = '') {
32
+ function validateOption(object, option, level = '', type = String) {
33
33
  if (object[option] === undefined || object[option] === null) {
34
34
  throw new Error(`${level} ${option} is not defined, define it using options.${level ? level + "." : ''}${option}`)
35
35
  }
36
+
37
+ if (typeof object[option] !== type) {
38
+ throw new Error(`${level} ${option} is not the correct datatype`)
39
+ }
36
40
  }
37
41
 
38
42
  function validateOptions(options) {
39
43
  if (!options) throw new Error('Options are not defined');
40
44
 
41
- const requiredTopLevel = ['payer', 'payee', 'price', 'title_code', 'title', 'due_date', 'reference'];
45
+ const requiredTopLevel = ['payer', 'payee', 'title_code', 'title', 'reference'];
42
46
  for (const field of requiredTopLevel) {
43
47
  validateOption(options, field)
44
48
  }
45
49
 
50
+ validateOption(options, 'price', '', Number)
51
+ validateOption(options, 'due_date', '', Date)
52
+ if(options.payment_date)
53
+ validateOption(options, 'payment_date', '', Date)
54
+
55
+
46
56
  const payerFields = ['name', 'surname', 'address', 'zip', 'city'];
47
57
  for (const field of payerFields) {
48
58
  validateOption(options.payer, field, 'Payer')
@@ -66,14 +76,14 @@ export async function generateUPNQR(options, size = 300) {
66
76
  `${options.payer.name} ${options.payer.surname}`,
67
77
  options.payer.address + "",
68
78
  `${options.payer.zip} ${options.payer.city}`,
69
- formatPrice(options.price) + "",
79
+ formatPrice(options.price),
70
80
  formatDate(options.payment_date),
71
81
  "",
72
82
  options.title_code ?? 'COST',
73
83
  options.title + "",
74
- formatDate(options.due_date) + "",
75
- stripSpaces(options.payee.iban) + "",
76
- stripSpaces(options.reference) + "",
84
+ formatDate(options.due_date),
85
+ stripSpaces(options.payee.iban),
86
+ stripSpaces(options.reference),
77
87
  options.payee.title + "",
78
88
  options.payee.address + "",
79
89
  `${options.payee.zip} ${options.payee.city}`