@excofy/utils 2.5.0 → 2.5.1
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.
- package/dist/index.cjs +2 -0
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/package.json +2 -2
- package/src/index.ts +5 -1
- package/tests/helpers/validator.spec.ts +75 -0
- package/tsconfig.json +2 -1
package/dist/index.cjs
CHANGED
|
@@ -36,6 +36,7 @@ __export(index_exports, {
|
|
|
36
36
|
cryptoUtils: () => cryptoUtils,
|
|
37
37
|
generateCode: () => generateCode,
|
|
38
38
|
htmlEntityDecode: () => htmlEntityDecode,
|
|
39
|
+
htmlToPlainText: () => sanitizeValue,
|
|
39
40
|
numberUtils: () => number_exports,
|
|
40
41
|
slugUtils: () => slug_exports,
|
|
41
42
|
stringUtils: () => stringUtils
|
|
@@ -973,6 +974,7 @@ var divide = (numerator, denominator) => {
|
|
|
973
974
|
cryptoUtils,
|
|
974
975
|
generateCode,
|
|
975
976
|
htmlEntityDecode,
|
|
977
|
+
htmlToPlainText,
|
|
976
978
|
numberUtils,
|
|
977
979
|
slugUtils,
|
|
978
980
|
stringUtils
|
package/dist/index.d.cts
CHANGED
|
@@ -6,6 +6,7 @@ type AllowedTag = Tag | {
|
|
|
6
6
|
tag: Tag;
|
|
7
7
|
attributes?: Attributes[];
|
|
8
8
|
};
|
|
9
|
+
declare function sanitizeValue(value: string, allowedTags?: AllowedTag[]): string;
|
|
9
10
|
declare function htmlEntityDecode(str?: string): string;
|
|
10
11
|
|
|
11
12
|
type TMessage = {
|
|
@@ -241,4 +242,4 @@ declare class ExpiredTokenError extends CryptoError {
|
|
|
241
242
|
constructor(message?: string);
|
|
242
243
|
}
|
|
243
244
|
|
|
244
|
-
export { ExpiredTokenError, InvalidTokenError, createValidator, cryptoUtils, generateCode, htmlEntityDecode, number as numberUtils, slug as slugUtils, stringUtils };
|
|
245
|
+
export { ExpiredTokenError, InvalidTokenError, createValidator, cryptoUtils, generateCode, htmlEntityDecode, sanitizeValue as htmlToPlainText, number as numberUtils, slug as slugUtils, stringUtils };
|
package/dist/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ type AllowedTag = Tag | {
|
|
|
6
6
|
tag: Tag;
|
|
7
7
|
attributes?: Attributes[];
|
|
8
8
|
};
|
|
9
|
+
declare function sanitizeValue(value: string, allowedTags?: AllowedTag[]): string;
|
|
9
10
|
declare function htmlEntityDecode(str?: string): string;
|
|
10
11
|
|
|
11
12
|
type TMessage = {
|
|
@@ -241,4 +242,4 @@ declare class ExpiredTokenError extends CryptoError {
|
|
|
241
242
|
constructor(message?: string);
|
|
242
243
|
}
|
|
243
244
|
|
|
244
|
-
export { ExpiredTokenError, InvalidTokenError, createValidator, cryptoUtils, generateCode, htmlEntityDecode, number as numberUtils, slug as slugUtils, stringUtils };
|
|
245
|
+
export { ExpiredTokenError, InvalidTokenError, createValidator, cryptoUtils, generateCode, htmlEntityDecode, sanitizeValue as htmlToPlainText, number as numberUtils, slug as slugUtils, stringUtils };
|
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@excofy/utils",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.1",
|
|
4
4
|
"description": "Biblioteca de utilitários para o Excofy",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@biomejs/biome": "^1.9.4",
|
|
34
34
|
"@types/big.js": "^6.2.2",
|
|
35
|
-
"@types/node": "^24.
|
|
35
|
+
"@types/node": "^24.10.13",
|
|
36
36
|
"tsup": "^8.5.0",
|
|
37
37
|
"tsx": "^4.20.4",
|
|
38
38
|
"typescript": "^5.8.3"
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { createValidator } from './helpers/validator';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
htmlEntityDecode,
|
|
4
|
+
sanitizeValue as htmlToPlainText,
|
|
5
|
+
} from './helpers/sanitize';
|
|
3
6
|
import { generateCode } from './helpers/generateCode';
|
|
4
7
|
import { stringUtils } from './helpers/string';
|
|
5
8
|
import { cryptoUtils } from './helpers/crypto';
|
|
@@ -9,6 +12,7 @@ import * as numberUtils from './helpers/number';
|
|
|
9
12
|
export {
|
|
10
13
|
createValidator,
|
|
11
14
|
htmlEntityDecode,
|
|
15
|
+
htmlToPlainText,
|
|
12
16
|
slugUtils,
|
|
13
17
|
numberUtils,
|
|
14
18
|
stringUtils,
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { createValidator } from '../../src/helpers/validator';
|
|
4
|
+
|
|
5
|
+
describe('validator', () => {
|
|
6
|
+
describe('asNumber', () => {
|
|
7
|
+
it('deve converter uma string "10" para o número 10', () => {
|
|
8
|
+
type InputRaw = { limit: string };
|
|
9
|
+
type InputParsed = { limit: number };
|
|
10
|
+
|
|
11
|
+
const v = createValidator<InputRaw, InputParsed>();
|
|
12
|
+
v.setInputs({ limit: '10' });
|
|
13
|
+
v.validate('limit')
|
|
14
|
+
.isRequired('Limite é obrigatório')
|
|
15
|
+
.type('string', 'Limite deve ser uma string')
|
|
16
|
+
.asNumber('Limite deve ser um número válido');
|
|
17
|
+
|
|
18
|
+
const inputs = v.getSanitizedInputs();
|
|
19
|
+
|
|
20
|
+
assert.equal(inputs.limit, 10);
|
|
21
|
+
assert.equal(typeof inputs.limit, 'number');
|
|
22
|
+
assert.equal(v.hasErrors(), false);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('deve adicionar erro quando o valor não pode ser convertido para número', () => {
|
|
26
|
+
type InputRaw = { limit: string };
|
|
27
|
+
type InputParsed = { limit: number };
|
|
28
|
+
|
|
29
|
+
const v = createValidator<InputRaw, InputParsed>();
|
|
30
|
+
v.setInputs({ limit: 'abc' });
|
|
31
|
+
v.validate('limit')
|
|
32
|
+
.isRequired('Limite é obrigatório')
|
|
33
|
+
.type('string', 'Limite deve ser uma string')
|
|
34
|
+
.asNumber('Limite deve ser um número válido');
|
|
35
|
+
|
|
36
|
+
assert.equal(v.hasErrors(), true);
|
|
37
|
+
const errors = v.getFlatErrors();
|
|
38
|
+
assert.equal(errors.limit[0].message, 'Limite deve ser um número válido');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('deve converter um número diretamente', () => {
|
|
42
|
+
type InputRaw = { limit: number };
|
|
43
|
+
type InputParsed = { limit: number };
|
|
44
|
+
|
|
45
|
+
const v = createValidator<InputRaw, InputParsed>();
|
|
46
|
+
v.setInputs({ limit: 25 });
|
|
47
|
+
v.validate('limit')
|
|
48
|
+
.isRequired('Limite é obrigatório')
|
|
49
|
+
.type('number', 'Limite deve ser um número')
|
|
50
|
+
.asNumber('Limite deve ser um número válido');
|
|
51
|
+
|
|
52
|
+
const inputs = v.getSanitizedInputs();
|
|
53
|
+
|
|
54
|
+
assert.equal(inputs.limit, 25);
|
|
55
|
+
assert.equal(typeof inputs.limit, 'number');
|
|
56
|
+
assert.equal(v.hasErrors(), false);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('deve lidar com valores opcionais quando não fornecidos', () => {
|
|
60
|
+
type InputRaw = { limit?: string };
|
|
61
|
+
type InputParsed = { limit?: number };
|
|
62
|
+
|
|
63
|
+
const v = createValidator<InputRaw, InputParsed>();
|
|
64
|
+
v.setInputs({});
|
|
65
|
+
v.validate('limit')
|
|
66
|
+
.isNotRequired()
|
|
67
|
+
.type('string', 'Limite deve ser uma string')
|
|
68
|
+
.asNumber('Limite deve ser um número válido');
|
|
69
|
+
|
|
70
|
+
assert.equal(v.hasErrors(), false);
|
|
71
|
+
const inputs = v.getSanitizedInputs();
|
|
72
|
+
assert.equal(inputs.limit, undefined);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
});
|
package/tsconfig.json
CHANGED