@ellipticltd/aml-utils 0.15.32 → 0.15.34

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 (66) hide show
  1. package/lib/errors/errors.d.ts +9 -0
  2. package/lib/errors/errors.js +19 -30
  3. package/lib/errors/errors.js.map +1 -0
  4. package/lib/file-parser/errors.d.ts +3 -0
  5. package/lib/file-parser/errors.js +11 -0
  6. package/lib/file-parser/errors.js.map +1 -0
  7. package/lib/file-parser/file-parser.d.ts +6 -0
  8. package/lib/file-parser/file-parser.js +60 -0
  9. package/lib/file-parser/file-parser.js.map +1 -0
  10. package/lib/file-parser/parse-row.d.ts +2 -0
  11. package/lib/file-parser/parse-row.js +40 -0
  12. package/lib/file-parser/parse-row.js.map +1 -0
  13. package/lib/file-parser/sanitizeRows.d.ts +5 -0
  14. package/lib/file-parser/sanitizeRows.js +15 -0
  15. package/lib/file-parser/sanitizeRows.js.map +1 -0
  16. package/lib/formatting/formatting.d.ts +2 -0
  17. package/lib/formatting/formatting.js +11 -12
  18. package/lib/formatting/formatting.js.map +1 -0
  19. package/lib/{index.ts → index.d.ts} +0 -1
  20. package/lib/index.js +21 -0
  21. package/lib/index.js.map +1 -0
  22. package/lib/middleware/middleware.d.ts +4 -0
  23. package/lib/middleware/middleware.js +14 -17
  24. package/lib/middleware/middleware.js.map +1 -0
  25. package/lib/orm-helpers/ormHelpers.d.ts +1 -0
  26. package/lib/orm-helpers/ormHelpers.js +7 -9
  27. package/lib/orm-helpers/ormHelpers.js.map +1 -0
  28. package/lib/structured-file-parser/errors.d.ts +12 -0
  29. package/lib/structured-file-parser/errors.js +33 -0
  30. package/lib/structured-file-parser/errors.js.map +1 -0
  31. package/lib/structured-file-parser/parse-row.d.ts +2 -0
  32. package/lib/structured-file-parser/parse-row.js +40 -0
  33. package/lib/structured-file-parser/parse-row.js.map +1 -0
  34. package/lib/structured-file-parser/sanitize-rows.d.ts +2 -0
  35. package/lib/structured-file-parser/sanitize-rows.js +14 -0
  36. package/lib/structured-file-parser/sanitize-rows.js.map +1 -0
  37. package/lib/structured-file-parser/structured-file-parser.d.ts +10 -0
  38. package/lib/structured-file-parser/structured-file-parser.js +96 -0
  39. package/lib/structured-file-parser/structured-file-parser.js.map +1 -0
  40. package/lib/types/types.d.ts +11 -0
  41. package/lib/types/types.js +190 -200
  42. package/lib/types/types.js.map +1 -0
  43. package/lib/validations/validations.d.ts +179 -0
  44. package/lib/validations/validations.js +500 -505
  45. package/lib/validations/validations.js.map +1 -0
  46. package/package.json +15 -1
  47. package/.eslintrc.js +0 -22
  48. package/jest.config.ts +0 -13
  49. package/lib/errors/errors.spec.js +0 -49
  50. package/lib/file-parser/__tests/parse-row.spec.js +0 -34
  51. package/lib/file-parser/__tests/sanitize-rows.spec.js +0 -85
  52. package/lib/file-parser/errors.ts +0 -6
  53. package/lib/file-parser/file-parser.ts +0 -67
  54. package/lib/file-parser/parse-row.ts +0 -46
  55. package/lib/file-parser/sanitizeRows.ts +0 -22
  56. package/lib/formatting/formatting.spec.js +0 -47
  57. package/lib/orm-helpers/ormHelpers.spec.js +0 -47
  58. package/lib/structured-file-parser/errors.ts +0 -25
  59. package/lib/structured-file-parser/parse-row.ts +0 -46
  60. package/lib/structured-file-parser/sanitize-rows.ts +0 -17
  61. package/lib/structured-file-parser/structured-file-parser.ts +0 -129
  62. package/lib/validations/validations.spec.js +0 -973
  63. package/project.json +0 -55
  64. package/tsconfig.json +0 -13
  65. package/tsconfig.lib.json +0 -14
  66. package/tsconfig.spec.json +0 -10
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isHeaderRowValid = exports.hasHeaderRow = void 0;
4
+ const tslib_1 = require("tslib");
5
+ /* eslint-disable no-await-in-loop */
6
+ /* eslint-disable no-restricted-syntax */
7
+ const parse_row_1 = tslib_1.__importDefault(require("./parse-row"));
8
+ const errors_1 = require("./errors");
9
+ const sanitize_rows_1 = tslib_1.__importDefault(require("./sanitize-rows"));
10
+ function chunk(array, size) {
11
+ if (!array.length) {
12
+ return [];
13
+ }
14
+ const head = array.slice(0, size);
15
+ const tail = array.slice(size);
16
+ return [head, ...chunk(tail, size)];
17
+ }
18
+ function processChunk(parsedRows, dependencies) {
19
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
20
+ const { processRow, headers } = dependencies;
21
+ return new Promise((resolve, reject) => {
22
+ setTimeout(() => {
23
+ try {
24
+ const processedRows = parsedRows.map((row) => processRow(row, headers));
25
+ resolve(processedRows);
26
+ }
27
+ catch (ex) {
28
+ reject(ex);
29
+ }
30
+ }, 0);
31
+ });
32
+ });
33
+ }
34
+ // a row is said to contain headers if two valid header names are present
35
+ const hasHeaderRow = (rows, requiredHeaderNames) => {
36
+ const firstRow = rows[0];
37
+ if (!firstRow) {
38
+ return false;
39
+ }
40
+ const foundIndex = firstRow.findIndex((value) => requiredHeaderNames.includes(value.toLowerCase()));
41
+ if (foundIndex < 0) {
42
+ return false;
43
+ }
44
+ const containsSecondHeader = firstRow
45
+ .filter((_val, i) => i !== foundIndex)
46
+ .some((value) => requiredHeaderNames.includes(value.toLowerCase()));
47
+ return containsSecondHeader;
48
+ };
49
+ exports.hasHeaderRow = hasHeaderRow;
50
+ const isHeaderRowValid = (rows, requiredHeaderNames) => {
51
+ const firstRow = rows[0];
52
+ if (!firstRow) {
53
+ return false;
54
+ }
55
+ return (requiredHeaderNames.every((header) => firstRow.map((value) => value.toLowerCase()).includes(header)) &&
56
+ requiredHeaderNames.length === firstRow.length);
57
+ };
58
+ exports.isHeaderRowValid = isHeaderRowValid;
59
+ function parseFile(fileContent, maxRows, requiredHeaderNames, dependencies) {
60
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
61
+ if (fileContent === 'invalidFileType') {
62
+ throw new errors_1.InvalidFileError();
63
+ }
64
+ const rows = fileContent.split('\n');
65
+ const parsedRows = rows.map(parse_row_1.default);
66
+ const sanitizedRows = (0, sanitize_rows_1.default)(parsedRows);
67
+ if (sanitizedRows.length > maxRows) {
68
+ throw new errors_1.TooManyRowsError();
69
+ }
70
+ if (sanitizedRows.length === 0) {
71
+ throw new errors_1.NoRowsError();
72
+ }
73
+ const containsHeaders = (0, exports.hasHeaderRow)(sanitizedRows, requiredHeaderNames);
74
+ if (containsHeaders && !(0, exports.isHeaderRowValid)(sanitizedRows, requiredHeaderNames)) {
75
+ throw new errors_1.InvalidHeadersError();
76
+ }
77
+ const firstSanitizedRow = sanitizedRows[0];
78
+ if (!firstSanitizedRow) {
79
+ throw new errors_1.NoRowsError();
80
+ }
81
+ const headers = containsHeaders ? firstSanitizedRow.map((value) => value.toLowerCase()) : requiredHeaderNames;
82
+ const dataRows = containsHeaders ? sanitizedRows.splice(1) : sanitizedRows;
83
+ if (dataRows.length === 0) {
84
+ throw new errors_1.NoRowsError();
85
+ }
86
+ const parsedRowChunks = chunk(dataRows, 30);
87
+ let results = [];
88
+ for (const currentRowChunk of parsedRowChunks) {
89
+ const chunkResult = yield processChunk(currentRowChunk, Object.assign(Object.assign({}, dependencies), { headers }));
90
+ results = results.concat(chunkResult);
91
+ }
92
+ return results;
93
+ });
94
+ }
95
+ exports.default = parseFile;
96
+ //# sourceMappingURL=structured-file-parser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"structured-file-parser.js","sourceRoot":"","sources":["../../../../../libs/aml-utils/lib/structured-file-parser/structured-file-parser.ts"],"names":[],"mappings":";;;;AAAA,qCAAqC;AACrC,yCAAyC;AACzC,oEAAmC;AACnC,qCAAgG;AAChG,4EAA2C;AAe3C,SAAS,KAAK,CAAI,KAAU,EAAE,IAAY;IACxC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE;QACjB,OAAO,EAAE,CAAC;KACX;IACD,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;IAClC,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAE/B,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IAAA,wBAAgB,EAAC,aAAa,EAAE,mBAAmB,CAAC,EAAE;YAC5E,MAAM,IAAI,4BAAmB,EAAE,CAAC;SACjC;QAED,MAAM,iBAAiB,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC;QAE3C,IAAI,CAAC,iBAAiB,EAAE;YACtB,MAAM,IAAI,oBAAW,EAAE,CAAC;SACzB;QAED,MAAM,OAAO,GAAG,eAAe,CAAC,CAAC,CAAC,iBAAiB,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,CAAC,mBAAmB,CAAC;QAE9G,MAAM,QAAQ,GAAG,eAAe,CAAC,CAAC,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC;QAE3E,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE;YACzB,MAAM,IAAI,oBAAW,EAAE,CAAC;SACzB;QAED,MAAM,eAAe,GAAG,KAAK,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;QAC5C,IAAI,OAAO,GAAQ,EAAE,CAAC;QACtB,KAAK,MAAM,eAAe,IAAI,eAAe,EAAE;YAC7C,MAAM,WAAW,GAAG,MAAM,YAAY,CAAC,eAAe,kCACjD,YAAY,KACf,OAAO,IACP,CAAC;YACH,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;SACvC;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;CAAA;AAED,kBAAe,SAAS,CAAC","sourcesContent":["/* eslint-disable no-await-in-loop */\n/* eslint-disable no-restricted-syntax */\nimport parseRow from './parse-row';\nimport { InvalidFileError, TooManyRowsError, NoRowsError, InvalidHeadersError } from './errors';\nimport sanitizeRows from './sanitize-rows';\n\nexport type ProcessingEntry = {\n isValid: boolean;\n};\n\ntype Dependencies<T extends ProcessingEntry> = {\n processRow: (cells: string[], headers: string[]) => T;\n};\n\ntype CompleteDependencies<T extends ProcessingEntry, U extends string[]> = {\n processRow: (cells: string[], headers: U) => T;\n headers: U;\n};\n\nfunction chunk<T>(array: T[], size: number): T[][] {\n if (!array.length) {\n return [];\n }\n const head = array.slice(0, size);\n const tail = array.slice(size);\n\n return [head, ...chunk(tail, size)];\n}\n\nasync function processChunk<T extends ProcessingEntry, U extends string[]>(\n parsedRows: string[][],\n dependencies: CompleteDependencies<T, U>\n): Promise<T[]> {\n const { processRow, headers } = dependencies;\n return new Promise((resolve: (value: T[] | undefined) => void, reject: (error: Error) => void): void => {\n setTimeout(() => {\n try {\n const processedRows = parsedRows.map((row) => processRow(row, headers));\n resolve(processedRows);\n } catch (ex) {\n reject(ex);\n }\n }, 0);\n });\n}\n\n// a row is said to contain headers if two valid header names are present\nexport const hasHeaderRow = (rows: string[][], requiredHeaderNames: string[]): boolean => {\n const firstRow = rows[0];\n if (!firstRow) {\n return false;\n }\n const foundIndex = firstRow.findIndex((value) => requiredHeaderNames.includes(value.toLowerCase()));\n if (foundIndex < 0) {\n return false;\n }\n const containsSecondHeader = firstRow\n .filter((_val, i) => i !== foundIndex)\n .some((value) => requiredHeaderNames.includes(value.toLowerCase()));\n return containsSecondHeader;\n};\n\nexport const isHeaderRowValid = (rows: string[][], requiredHeaderNames: string[]): boolean => {\n const firstRow = rows[0];\n if (!firstRow) {\n return false;\n }\n return (\n requiredHeaderNames.every((header) => firstRow.map((value) => value.toLowerCase()).includes(header)) &&\n requiredHeaderNames.length === firstRow.length\n );\n};\n\nasync function parseFile<T extends ProcessingEntry>(\n fileContent: string,\n maxRows: number,\n requiredHeaderNames: string[],\n dependencies: Dependencies<T>\n): Promise<T[]> {\n if (fileContent === 'invalidFileType') {\n throw new InvalidFileError();\n }\n\n const rows = fileContent.split('\\n');\n const parsedRows = rows.map(parseRow);\n\n const sanitizedRows = sanitizeRows(parsedRows);\n\n if (sanitizedRows.length > maxRows) {\n throw new TooManyRowsError();\n }\n\n if (sanitizedRows.length === 0) {\n throw new NoRowsError();\n }\n\n const containsHeaders = hasHeaderRow(sanitizedRows, requiredHeaderNames);\n if (containsHeaders && !isHeaderRowValid(sanitizedRows, requiredHeaderNames)) {\n throw new InvalidHeadersError();\n }\n\n const firstSanitizedRow = sanitizedRows[0];\n\n if (!firstSanitizedRow) {\n throw new NoRowsError();\n }\n\n const headers = containsHeaders ? firstSanitizedRow.map((value) => value.toLowerCase()) : requiredHeaderNames;\n\n const dataRows = containsHeaders ? sanitizedRows.splice(1) : sanitizedRows;\n\n if (dataRows.length === 0) {\n throw new NoRowsError();\n }\n\n const parsedRowChunks = chunk(dataRows, 30);\n let results: T[] = [];\n for (const currentRowChunk of parsedRowChunks) {\n const chunkResult = await processChunk(currentRowChunk, {\n ...dependencies,\n headers,\n });\n results = results.concat(chunkResult);\n }\n\n return results;\n}\n\nexport default parseFile;\n"]}
@@ -0,0 +1,11 @@
1
+ declare const withNullable: {};
2
+ export function parseType(str: any): any;
3
+ export function parsedTypeCheck(type: any, obj: any): any;
4
+ export function typeCheck(type: any, obj: any): any;
5
+ export namespace opts {
6
+ export { withNullable as customTypes };
7
+ }
8
+ export function ensureType(type: any, obj: any, msg: any): boolean;
9
+ export function checkArg(obj: any, prop: any, type: any, msg: any): any;
10
+ export function checkArgs(input: any, types: any): void;
11
+ export {};
@@ -1,211 +1,201 @@
1
1
  const TC = require('type-check');
2
-
3
2
  const _ = require('lodash');
4
-
5
3
  const E = require('../errors/errors');
6
-
7
4
  const V = require('../validations/validations');
8
-
9
5
  const customTypes = {
10
- Integer: {
11
- typeOf: 'Number',
12
- validate(x) {
13
- return x % 1 === 0;
14
- },
15
- },
16
- IntString: {
17
- typeOf: 'String',
18
- validate: V.isInt,
19
- },
20
- BoolString: {
21
- typeOf: 'String',
22
- validate: V.isBoolean,
23
- },
24
- Hex32: {
25
- typeOf: 'String',
26
- validate(x) {
27
- return V.isHexadecimal(x) && x.length === 32;
28
- },
29
- },
30
- Email: {
31
- typeOf: 'String',
32
- validate: V.isEmail,
33
- },
34
- UUID: {
35
- typeOf: 'String',
36
- validate: V.isUUID,
37
- },
38
- UUIDv4: {
39
- typeOf: 'String',
40
- validate(x) {
41
- return V.isUUID(x, 4);
42
- },
43
- },
44
- JSON: {
45
- typeOf: 'String',
46
- validate: V.isJSON,
47
- },
48
- Url: {
49
- typeOf: 'String',
50
- validate: V.isURL,
51
- },
52
- DateString: {
53
- typeOf: 'String',
54
- validate: V.isDate,
55
- },
56
- NonEmptyArray: {
57
- typeOf: 'Array',
58
- validate: V.nonEmpty,
59
- },
60
- NonNullString: {
61
- typeOf: 'String',
62
- validate: V.nonEmpty,
63
- },
64
- CustomerReference: {
65
- typeOf: 'String',
66
- validate: V.isCustomerReference,
67
- },
68
- CustomerLabelName: {
69
- typeOf: 'String',
70
- validate: V.isCustomerLabelName,
71
- },
72
- BitcoinAddress: {
73
- typeOf: 'String',
74
- validate: V.bitcoin.isAddress,
75
- },
76
- EthereumAddress: {
77
- typeOf: 'String',
78
- validate: V.ethereum.isAddress,
79
- },
80
- EthereumTx: {
81
- typeOf: 'String',
82
- validate: V.ethereum.isTxHash,
83
- },
84
- EthereumBlockHash: {
85
- typeOf: 'String',
86
- validate: V.ethereum.isBlockHash,
87
- },
88
- EthereumWeiAmount: {
89
- typeOf: 'String',
90
- validate: V.ethereum.isValidWeiAmount,
91
- },
92
- EthereumAddressCode: {
93
- typeOf: 'String',
94
- validate: V.ethereum.isAddressCode,
95
- },
96
- MineId: {
97
- typeOf: 'String',
98
- validate(x) {
99
- return x === 'mine';
100
- },
101
- },
102
- BitcoinTxHash: {
103
- typeOf: 'String',
104
- validate: V.bitcoin.isTxHash,
105
- },
106
- BitcoinAddressArray: {
107
- typeOf: 'Array',
108
- validate(as) {
109
- return _.every(as, V.bitcoin.isAddress);
110
- },
111
- },
112
- BitcoinTxHashArray: {
113
- typeOf: 'Array',
114
- validate(as) {
115
- return _.every(as, V.bitcoin.isTxHash);
116
- },
117
- },
118
- BitcoinTxHex: {
119
- typeOf: 'String',
120
- validate: V.bitcoin.isTxHex,
121
- },
122
- BitcoinScriptHex: {
123
- typeOf: 'String',
124
- validate: V.bitcoin.isScriptHex,
125
- },
126
- BitcoinHDPath: {
127
- typeOf: 'String',
128
- validate: V.bitcoin.isHDPath,
129
- },
130
- BitcoinPublicKey: {
131
- typeOf: 'String',
132
- validate: V.bitcoin.isPublicKey,
133
- },
134
- BitcoinXpub: {
135
- typeOf: 'String',
136
- validate: V.bitcoin.isHDPublicKey,
137
- },
138
- BitcoinTxSignature: {
139
- typeOf: 'String',
140
- validate: V.bitcoin.isTxSignature,
141
- },
142
- BitcoinBlockHeight: {
143
- typeOf: 'Number',
144
- validate: V.bitcoin.isBlockHeight,
145
- },
6
+ Integer: {
7
+ typeOf: 'Number',
8
+ validate(x) {
9
+ return x % 1 === 0;
10
+ },
11
+ },
12
+ IntString: {
13
+ typeOf: 'String',
14
+ validate: V.isInt,
15
+ },
16
+ BoolString: {
17
+ typeOf: 'String',
18
+ validate: V.isBoolean,
19
+ },
20
+ Hex32: {
21
+ typeOf: 'String',
22
+ validate(x) {
23
+ return V.isHexadecimal(x) && x.length === 32;
24
+ },
25
+ },
26
+ Email: {
27
+ typeOf: 'String',
28
+ validate: V.isEmail,
29
+ },
30
+ UUID: {
31
+ typeOf: 'String',
32
+ validate: V.isUUID,
33
+ },
34
+ UUIDv4: {
35
+ typeOf: 'String',
36
+ validate(x) {
37
+ return V.isUUID(x, 4);
38
+ },
39
+ },
40
+ JSON: {
41
+ typeOf: 'String',
42
+ validate: V.isJSON,
43
+ },
44
+ Url: {
45
+ typeOf: 'String',
46
+ validate: V.isURL,
47
+ },
48
+ DateString: {
49
+ typeOf: 'String',
50
+ validate: V.isDate,
51
+ },
52
+ NonEmptyArray: {
53
+ typeOf: 'Array',
54
+ validate: V.nonEmpty,
55
+ },
56
+ NonNullString: {
57
+ typeOf: 'String',
58
+ validate: V.nonEmpty,
59
+ },
60
+ CustomerReference: {
61
+ typeOf: 'String',
62
+ validate: V.isCustomerReference,
63
+ },
64
+ CustomerLabelName: {
65
+ typeOf: 'String',
66
+ validate: V.isCustomerLabelName,
67
+ },
68
+ BitcoinAddress: {
69
+ typeOf: 'String',
70
+ validate: V.bitcoin.isAddress,
71
+ },
72
+ EthereumAddress: {
73
+ typeOf: 'String',
74
+ validate: V.ethereum.isAddress,
75
+ },
76
+ EthereumTx: {
77
+ typeOf: 'String',
78
+ validate: V.ethereum.isTxHash,
79
+ },
80
+ EthereumBlockHash: {
81
+ typeOf: 'String',
82
+ validate: V.ethereum.isBlockHash,
83
+ },
84
+ EthereumWeiAmount: {
85
+ typeOf: 'String',
86
+ validate: V.ethereum.isValidWeiAmount,
87
+ },
88
+ EthereumAddressCode: {
89
+ typeOf: 'String',
90
+ validate: V.ethereum.isAddressCode,
91
+ },
92
+ MineId: {
93
+ typeOf: 'String',
94
+ validate(x) {
95
+ return x === 'mine';
96
+ },
97
+ },
98
+ BitcoinTxHash: {
99
+ typeOf: 'String',
100
+ validate: V.bitcoin.isTxHash,
101
+ },
102
+ BitcoinAddressArray: {
103
+ typeOf: 'Array',
104
+ validate(as) {
105
+ return _.every(as, V.bitcoin.isAddress);
106
+ },
107
+ },
108
+ BitcoinTxHashArray: {
109
+ typeOf: 'Array',
110
+ validate(as) {
111
+ return _.every(as, V.bitcoin.isTxHash);
112
+ },
113
+ },
114
+ BitcoinTxHex: {
115
+ typeOf: 'String',
116
+ validate: V.bitcoin.isTxHex,
117
+ },
118
+ BitcoinScriptHex: {
119
+ typeOf: 'String',
120
+ validate: V.bitcoin.isScriptHex,
121
+ },
122
+ BitcoinHDPath: {
123
+ typeOf: 'String',
124
+ validate: V.bitcoin.isHDPath,
125
+ },
126
+ BitcoinPublicKey: {
127
+ typeOf: 'String',
128
+ validate: V.bitcoin.isPublicKey,
129
+ },
130
+ BitcoinXpub: {
131
+ typeOf: 'String',
132
+ validate: V.bitcoin.isHDPublicKey,
133
+ },
134
+ BitcoinTxSignature: {
135
+ typeOf: 'String',
136
+ validate: V.bitcoin.isTxSignature,
137
+ },
138
+ BitcoinBlockHeight: {
139
+ typeOf: 'Number',
140
+ validate: V.bitcoin.isBlockHeight,
141
+ },
146
142
  };
147
-
148
143
  // add support for nullable properties
149
144
  const withNullable = Object.keys(customTypes).reduce((acc, type) => {
150
- acc[type] = customTypes[type];
151
- const { typeOf, validate } = customTypes[type];
152
-
153
- // for every type, add Nullable+type. A nullable string can also be empty
154
- acc[`Nullable${type}`] = { ...customTypes[type], validate: (x) => x == null || (typeOf === 'String' && x === '') || validate(x) };
155
-
156
- return acc;
145
+ acc[type] = customTypes[type];
146
+ const { typeOf, validate } = customTypes[type];
147
+ // for every type, add Nullable+type. A nullable string can also be empty
148
+ acc[`Nullable${type}`] = Object.assign(Object.assign({}, customTypes[type]), { validate: (x) => x == null || (typeOf === 'String' && x === '') || validate(x) });
149
+ return acc;
157
150
  }, {});
158
-
159
151
  module.exports = {
160
- parseType(str) {
161
- return TC.parseType(str);
162
- },
163
- parsedTypeCheck(type, obj) {
164
- return TC.parsedTypeCheck(type, obj, this.opts);
165
- },
166
- typeCheck(type, obj) {
167
- return TC.typeCheck(type, obj, this.opts);
168
- },
169
- opts: {
170
- customTypes: withNullable,
171
- },
172
- ensureType(type, obj, msg) {
173
- return V.ensure(this.typeCheck(type, obj), msg);
174
- },
175
- checkArg(obj, prop, type, msg) {
176
- let message;
177
- if (msg == null) {
178
- message = `Invalid ${prop}`;
179
- } else {
180
- message = msg;
181
- }
182
- this.ensureType(type, obj[prop], message);
183
- return obj[prop];
184
- },
185
- checkArgs(input, types) {
186
- const inputKeys = {};
187
- const [typ] = types;
188
- const fields = typ.of;
189
- let numInputKeys = 0;
190
- let numKeys;
191
-
192
- Object.keys(input).forEach((k) => {
193
- inputKeys[k] = true;
194
- numInputKeys += 1;
195
- });
196
-
197
- numKeys = 0;
198
-
199
- Object.keys(fields).forEach((key) => {
200
- const objTypes = fields[key];
201
- V.ensure(this.parsedTypeCheck(objTypes, input[key]), `Invalid ${key}`);
202
- if (inputKeys[key]) {
203
- numKeys += 1;
204
- }
205
- });
206
-
207
- if (!(typ.subset || numInputKeys === numKeys)) {
208
- throw new E.BadRequest('invalid extra arguments are present');
209
- }
210
- },
152
+ parseType(str) {
153
+ return TC.parseType(str);
154
+ },
155
+ parsedTypeCheck(type, obj) {
156
+ return TC.parsedTypeCheck(type, obj, this.opts);
157
+ },
158
+ typeCheck(type, obj) {
159
+ return TC.typeCheck(type, obj, this.opts);
160
+ },
161
+ opts: {
162
+ customTypes: withNullable,
163
+ },
164
+ ensureType(type, obj, msg) {
165
+ return V.ensure(this.typeCheck(type, obj), msg);
166
+ },
167
+ checkArg(obj, prop, type, msg) {
168
+ let message;
169
+ if (msg == null) {
170
+ message = `Invalid ${prop}`;
171
+ }
172
+ else {
173
+ message = msg;
174
+ }
175
+ this.ensureType(type, obj[prop], message);
176
+ return obj[prop];
177
+ },
178
+ checkArgs(input, types) {
179
+ const inputKeys = {};
180
+ const [typ] = types;
181
+ const fields = typ.of;
182
+ let numInputKeys = 0;
183
+ let numKeys;
184
+ Object.keys(input).forEach((k) => {
185
+ inputKeys[k] = true;
186
+ numInputKeys += 1;
187
+ });
188
+ numKeys = 0;
189
+ Object.keys(fields).forEach((key) => {
190
+ const objTypes = fields[key];
191
+ V.ensure(this.parsedTypeCheck(objTypes, input[key]), `Invalid ${key}`);
192
+ if (inputKeys[key]) {
193
+ numKeys += 1;
194
+ }
195
+ });
196
+ if (!(typ.subset || numInputKeys === numKeys)) {
197
+ throw new E.BadRequest('invalid extra arguments are present');
198
+ }
199
+ },
211
200
  };
201
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../libs/aml-utils/lib/types/types.js"],"names":[],"mappings":"AAAA,MAAM,EAAE,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;AAEjC,MAAM,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;AAE5B,MAAM,CAAC,GAAG,OAAO,CAAC,k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ACD,SAAS,CAAC,KAAK,EAAE,KAAK;QACpB,MAAM,SAAS,GAAG,EAAE,CAAC;QACrB,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;QACpB,MAAM,MAAM,GAAG,GAAG,CAAC,EAAE,CAAC;QACtB,IAAI,YAAY,GAAG,CAAC,CAAC;QACrB,IAAI,OAAO,CAAC;QAEZ,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE;YAC/B,SAAS,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;YACpB,YAAY,IAAI,CAAC,CAAC;QACpB,CAAC,CAAC,CAAC;QAEH,OAAO,GAAG,CAAC,CAAC;QAEZ,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YAClC,MAAM,QAAQ,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;YAC7B,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,eAAe,CAAC,QAAQ,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,EAAE,WAAW,GAAG,EAAE,CAAC,CAAC;YACvE,IAAI,SAAS,CAAC,GAAG,CAAC,EAAE;gBAClB,OAAO,IAAI,CAAC,CAAC;aACd;QACH,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,CAAC,GAAG,CAAC,MAAM,IAAI,YAAY,KAAK,OAAO,CAAC,EAAE;YAC7C,MAAM,IAAI,CAAC,CAAC,UAAU,CAAC,qCAAqC,CAAC,CAAC;SAC/D;IACH,CAAC;CACF,CAAC","sourcesContent":["const TC = require('type-check');\n\nconst _ = require('lodash');\n\nconst E = require('../errors/errors');\n\nconst V = require('../validations/validations');\n\nconst customTypes = {\n Integer: {\n typeOf: 'Number',\n validate(x) {\n return x % 1 === 0;\n },\n },\n IntString: {\n typeOf: 'String',\n validate: V.isInt,\n },\n BoolString: {\n typeOf: 'String',\n validate: V.isBoolean,\n },\n Hex32: {\n typeOf: 'String',\n validate(x) {\n return V.isHexadecimal(x) && x.length === 32;\n },\n },\n Email: {\n typeOf: 'String',\n validate: V.isEmail,\n },\n UUID: {\n typeOf: 'String',\n validate: V.isUUID,\n },\n UUIDv4: {\n typeOf: 'String',\n validate(x) {\n return V.isUUID(x, 4);\n },\n },\n JSON: {\n typeOf: 'String',\n validate: V.isJSON,\n },\n Url: {\n typeOf: 'String',\n validate: V.isURL,\n },\n DateString: {\n typeOf: 'String',\n validate: V.isDate,\n },\n NonEmptyArray: {\n typeOf: 'Array',\n validate: V.nonEmpty,\n },\n NonNullString: {\n typeOf: 'String',\n validate: V.nonEmpty,\n },\n CustomerReference: {\n typeOf: 'String',\n validate: V.isCustomerReference,\n },\n CustomerLabelName: {\n typeOf: 'String',\n validate: V.isCustomerLabelName,\n },\n BitcoinAddress: {\n typeOf: 'String',\n validate: V.bitcoin.isAddress,\n },\n EthereumAddress: {\n typeOf: 'String',\n validate: V.ethereum.isAddress,\n },\n EthereumTx: {\n typeOf: 'String',\n validate: V.ethereum.isTxHash,\n },\n EthereumBlockHash: {\n typeOf: 'String',\n validate: V.ethereum.isBlockHash,\n },\n EthereumWeiAmount: {\n typeOf: 'String',\n validate: V.ethereum.isValidWeiAmount,\n },\n EthereumAddressCode: {\n typeOf: 'String',\n validate: V.ethereum.isAddressCode,\n },\n MineId: {\n typeOf: 'String',\n validate(x) {\n return x === 'mine';\n },\n },\n BitcoinTxHash: {\n typeOf: 'String',\n validate: V.bitcoin.isTxHash,\n },\n BitcoinAddressArray: {\n typeOf: 'Array',\n validate(as) {\n return _.every(as, V.bitcoin.isAddress);\n },\n },\n BitcoinTxHashArray: {\n typeOf: 'Array',\n validate(as) {\n return _.every(as, V.bitcoin.isTxHash);\n },\n },\n BitcoinTxHex: {\n typeOf: 'String',\n validate: V.bitcoin.isTxHex,\n },\n BitcoinScriptHex: {\n typeOf: 'String',\n validate: V.bitcoin.isScriptHex,\n },\n BitcoinHDPath: {\n typeOf: 'String',\n validate: V.bitcoin.isHDPath,\n },\n BitcoinPublicKey: {\n typeOf: 'String',\n validate: V.bitcoin.isPublicKey,\n },\n BitcoinXpub: {\n typeOf: 'String',\n validate: V.bitcoin.isHDPublicKey,\n },\n BitcoinTxSignature: {\n typeOf: 'String',\n validate: V.bitcoin.isTxSignature,\n },\n BitcoinBlockHeight: {\n typeOf: 'Number',\n validate: V.bitcoin.isBlockHeight,\n },\n};\n\n// add support for nullable properties\nconst withNullable = Object.keys(customTypes).reduce((acc, type) => {\n acc[type] = customTypes[type];\n const { typeOf, validate } = customTypes[type];\n\n // for every type, add Nullable+type. A nullable string can also be empty\n acc[`Nullable${type}`] = { ...customTypes[type], validate: (x) => x == null || (typeOf === 'String' && x === '') || validate(x) };\n\n return acc;\n}, {});\n\nmodule.exports = {\n parseType(str) {\n return TC.parseType(str);\n },\n parsedTypeCheck(type, obj) {\n return TC.parsedTypeCheck(type, obj, this.opts);\n },\n typeCheck(type, obj) {\n return TC.typeCheck(type, obj, this.opts);\n },\n opts: {\n customTypes: withNullable,\n },\n ensureType(type, obj, msg) {\n return V.ensure(this.typeCheck(type, obj), msg);\n },\n checkArg(obj, prop, type, msg) {\n let message;\n if (msg == null) {\n message = `Invalid ${prop}`;\n } else {\n message = msg;\n }\n this.ensureType(type, obj[prop], message);\n return obj[prop];\n },\n checkArgs(input, types) {\n const inputKeys = {};\n const [typ] = types;\n const fields = typ.of;\n let numInputKeys = 0;\n let numKeys;\n\n Object.keys(input).forEach((k) => {\n inputKeys[k] = true;\n numInputKeys += 1;\n });\n\n numKeys = 0;\n\n Object.keys(fields).forEach((key) => {\n const objTypes = fields[key];\n V.ensure(this.parsedTypeCheck(objTypes, input[key]), `Invalid ${key}`);\n if (inputKeys[key]) {\n numKeys += 1;\n }\n });\n\n if (!(typ.subset || numInputKeys === numKeys)) {\n throw new E.BadRequest('invalid extra arguments are present');\n }\n },\n};\n"]}