@adyllsxn/kwavalidator 1.0.0

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.
@@ -0,0 +1,4 @@
1
+ import { BiValidationResult } from '../models/bi-validation-result.js';
2
+ export interface IBiValidator {
3
+ validate(bi: string): BiValidationResult;
4
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ export declare enum BiValidationError {
2
+ Empty = "Empty",
3
+ InvalidLength = "InvalidLength",
4
+ InvalidFormat = "InvalidFormat",
5
+ InvalidProvince = "InvalidProvince"
6
+ }
@@ -0,0 +1,7 @@
1
+ export var BiValidationError;
2
+ (function (BiValidationError) {
3
+ BiValidationError["Empty"] = "Empty";
4
+ BiValidationError["InvalidLength"] = "InvalidLength";
5
+ BiValidationError["InvalidFormat"] = "InvalidFormat";
6
+ BiValidationError["InvalidProvince"] = "InvalidProvince";
7
+ })(BiValidationError || (BiValidationError = {}));
@@ -0,0 +1,7 @@
1
+ import { BiValidationResult } from './models/bi-validation-result.js';
2
+ export * from './abstractions/i-bi-validator.js';
3
+ export * from './enums/bi-validation-error.js';
4
+ export * from './models/province.js';
5
+ export * from './models/bi-validation-result.js';
6
+ export * from './services/bi-validator.js';
7
+ export declare function validateBI(bi: string): BiValidationResult;
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ import { BiValidator } from './services/bi-validator.js';
2
+ export * from './abstractions/i-bi-validator.js';
3
+ export * from './enums/bi-validation-error.js';
4
+ export * from './models/province.js';
5
+ export * from './models/bi-validation-result.js';
6
+ export * from './services/bi-validator.js';
7
+ // Adicione esta função que o teu app.js está a tentar chamar:
8
+ export function validateBI(bi) {
9
+ const validator = new BiValidator();
10
+ return validator.validate(bi);
11
+ }
@@ -0,0 +1,11 @@
1
+ import { BiValidationError } from '../enums/bi-validation-error.js';
2
+ import { Province } from './province.js';
3
+ export declare class BiValidationResult {
4
+ readonly isValid: boolean;
5
+ readonly errorMessage?: string;
6
+ readonly errorCode?: BiValidationError;
7
+ readonly province?: Province;
8
+ private constructor();
9
+ static success(province: Province): BiValidationResult;
10
+ static failure(error: BiValidationError, message: string): BiValidationResult;
11
+ }
@@ -0,0 +1,18 @@
1
+ export class BiValidationResult {
2
+ isValid;
3
+ errorMessage;
4
+ errorCode;
5
+ province;
6
+ constructor(init) {
7
+ this.isValid = init.isValid ?? false;
8
+ this.errorMessage = init.errorMessage;
9
+ this.errorCode = init.errorCode;
10
+ this.province = init.province;
11
+ }
12
+ static success(province) {
13
+ return new BiValidationResult({ isValid: true, province });
14
+ }
15
+ static failure(error, message) {
16
+ return new BiValidationResult({ isValid: false, errorCode: error, errorMessage: message });
17
+ }
18
+ }
@@ -0,0 +1,5 @@
1
+ export declare class Province {
2
+ readonly code: string;
3
+ readonly name: string;
4
+ constructor(code: string, name: string);
5
+ }
@@ -0,0 +1,8 @@
1
+ export class Province {
2
+ code;
3
+ name;
4
+ constructor(code, name) {
5
+ this.code = code;
6
+ this.name = name;
7
+ }
8
+ }
@@ -0,0 +1,7 @@
1
+ import { IBiValidator } from '../abstractions/i-bi-validator.js';
2
+ import { BiValidationResult } from '../models/bi-validation-result.js';
3
+ export declare class BiValidator implements IBiValidator {
4
+ private static readonly BiRegex;
5
+ private static readonly Provinces;
6
+ validate(bi: string): BiValidationResult;
7
+ }
@@ -0,0 +1,48 @@
1
+ import { BiValidationResult } from '../models/bi-validation-result.js';
2
+ import { BiValidationError } from '../enums/bi-validation-error.js';
3
+ import { Province } from '../models/province.js';
4
+ export class BiValidator {
5
+ // Regex atualizada para suportar as 2 letras das províncias normais E as 3 letras de KNo/KSu
6
+ static BiRegex = /^\d{9}([A-Z]{2}\d{3}|K[A-Za-z]{2}\d{2})$/;
7
+ static Provinces = {
8
+ 'BL': new Province('BL', 'Bengo'),
9
+ 'BG': new Province('BG', 'Benguela'),
10
+ 'BI': new Province('BI', 'Bié'),
11
+ 'CB': new Province('CB', 'Cabinda'),
12
+ 'CC': new Province('CC', 'Cuando Cubango'),
13
+ 'CN': new Province('CN', 'Cunene'),
14
+ 'HU': new Province('HU', 'Huambo'),
15
+ 'HL': new Province('HL', 'Huíla'),
16
+ 'LA': new Province('LA', 'Luanda'),
17
+ 'LN': new Province('LN', 'Lunda Norte'), // Corrigido de LD para LN (padrão oficial)
18
+ 'LS': new Province('LS', 'Lunda Sul'),
19
+ 'ML': new Province('ML', 'Malanje'),
20
+ 'MO': new Province('MO', 'Moxico'),
21
+ 'NA': new Province('NA', 'Namibe'),
22
+ 'UI': new Province('UI', 'Uíge'),
23
+ 'ZA': new Province('ZA', 'Zaire'),
24
+ 'KNO': new Province('KNO', 'Cuanza Norte'),
25
+ 'KSU': new Province('KSU', 'Cuanza Sul')
26
+ };
27
+ validate(bi) {
28
+ if (!bi || bi.trim() === '') {
29
+ return BiValidationResult.failure(BiValidationError.Empty, 'BI is required');
30
+ }
31
+ if (bi.length !== 14) {
32
+ return BiValidationResult.failure(BiValidationError.InvalidLength, 'BI must have 14 characters');
33
+ }
34
+ const upperBi = bi.toUpperCase();
35
+ if (!BiValidator.BiRegex.test(upperBi)) {
36
+ return BiValidationResult.failure(BiValidationError.InvalidFormat, 'BI format is invalid');
37
+ }
38
+ // Captura o código da província de forma dinâmica (se começa com K pega 3 letras, senão 2)
39
+ const provinceCode = upperBi.startsWith('K', 9)
40
+ ? upperBi.substring(9, 12)
41
+ : upperBi.substring(9, 11);
42
+ const province = BiValidator.Provinces[provinceCode];
43
+ if (!province) {
44
+ return BiValidationResult.failure(BiValidationError.InvalidProvince, 'Invalid province code');
45
+ }
46
+ return BiValidationResult.success(province);
47
+ }
48
+ }
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@adyllsxn/kwavalidator",
3
+ "version": "1.0.0",
4
+ "description": "Cross-platform library for validating Angolan documents with speed, simplicity",
5
+ "keywords": [
6
+ "bi",
7
+ "nif"
8
+ ],
9
+ "license": "MIT",
10
+ "author": "Domingos Nascimento",
11
+ "type": "module",
12
+ "main": "./dist/index.js",
13
+ "types": "./dist/index.d.ts",
14
+ "scripts": {
15
+ "build": "tsc",
16
+ "test": "echo \"Error: no test specified\" && exit 1"
17
+ },
18
+ "devDependencies": {
19
+ "typescript": "^6.0.3"
20
+ }
21
+ }
@@ -0,0 +1,5 @@
1
+ import { BiValidationResult } from '../models/bi-validation-result.js';
2
+
3
+ export interface IBiValidator {
4
+ validate(bi: string): BiValidationResult;
5
+ }
@@ -0,0 +1,6 @@
1
+ export enum BiValidationError {
2
+ Empty = 'Empty',
3
+ InvalidLength = 'InvalidLength',
4
+ InvalidFormat = 'InvalidFormat',
5
+ InvalidProvince = 'InvalidProvince'
6
+ }
package/src/index.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { BiValidator } from './services/bi-validator.js';
2
+ import { BiValidationResult } from './models/bi-validation-result.js';
3
+
4
+ export * from './abstractions/i-bi-validator.js';
5
+ export * from './enums/bi-validation-error.js';
6
+ export * from './models/province.js';
7
+ export * from './models/bi-validation-result.js';
8
+ export * from './services/bi-validator.js';
9
+
10
+ // Adicione esta função que o teu app.js está a tentar chamar:
11
+ export function validateBI(bi: string): BiValidationResult {
12
+ const validator = new BiValidator();
13
+ return validator.validate(bi);
14
+ }
@@ -0,0 +1,24 @@
1
+ import { BiValidationError } from '../enums/bi-validation-error.js';
2
+ import { Province } from './province.js';
3
+
4
+ export class BiValidationResult {
5
+ public readonly isValid: boolean;
6
+ public readonly errorMessage?: string;
7
+ public readonly errorCode?: BiValidationError;
8
+ public readonly province?: Province;
9
+
10
+ private constructor(init: Partial<BiValidationResult>) {
11
+ this.isValid = init.isValid ?? false;
12
+ this.errorMessage = init.errorMessage;
13
+ this.errorCode = init.errorCode;
14
+ this.province = init.province;
15
+ }
16
+
17
+ public static success(province: Province): BiValidationResult {
18
+ return new BiValidationResult({ isValid: true, province });
19
+ }
20
+
21
+ public static failure(error: BiValidationError, message: string): BiValidationResult {
22
+ return new BiValidationResult({ isValid: false, errorCode: error, errorMessage: message });
23
+ }
24
+ }
@@ -0,0 +1,6 @@
1
+ export class Province {
2
+ constructor(
3
+ public readonly code: string,
4
+ public readonly name: string
5
+ ) {}
6
+ }
@@ -0,0 +1,59 @@
1
+ import { IBiValidator } from '../abstractions/i-bi-validator.js';
2
+ import { BiValidationResult } from '../models/bi-validation-result.js';
3
+ import { BiValidationError } from '../enums/bi-validation-error.js';
4
+ import { Province } from '../models/province.js';
5
+
6
+ export class BiValidator implements IBiValidator {
7
+ // Regex atualizada para suportar as 2 letras das províncias normais E as 3 letras de KNo/KSu
8
+ private static readonly BiRegex = /^\d{9}([A-Z]{2}\d{3}|K[A-Za-z]{2}\d{2})$/;
9
+
10
+ private static readonly Provinces: Record<string, Province> = {
11
+ 'BL': new Province('BL', 'Bengo'),
12
+ 'BG': new Province('BG', 'Benguela'),
13
+ 'BI': new Province('BI', 'Bié'),
14
+ 'CB': new Province('CB', 'Cabinda'),
15
+ 'CC': new Province('CC', 'Cuando Cubango'),
16
+ 'CN': new Province('CN', 'Cunene'),
17
+ 'HU': new Province('HU', 'Huambo'),
18
+ 'HL': new Province('HL', 'Huíla'),
19
+ 'LA': new Province('LA', 'Luanda'),
20
+ 'LN': new Province('LN', 'Lunda Norte'), // Corrigido de LD para LN (padrão oficial)
21
+ 'LS': new Province('LS', 'Lunda Sul'),
22
+ 'ML': new Province('ML', 'Malanje'),
23
+ 'MO': new Province('MO', 'Moxico'),
24
+ 'NA': new Province('NA', 'Namibe'),
25
+ 'UI': new Province('UI', 'Uíge'),
26
+ 'ZA': new Province('ZA', 'Zaire'),
27
+ 'KNO': new Province('KNO', 'Cuanza Norte'),
28
+ 'KSU': new Province('KSU', 'Cuanza Sul')
29
+ };
30
+
31
+ public validate(bi: string): BiValidationResult {
32
+ if (!bi || bi.trim() === '') {
33
+ return BiValidationResult.failure(BiValidationError.Empty, 'BI is required');
34
+ }
35
+
36
+ if (bi.length !== 14) {
37
+ return BiValidationResult.failure(BiValidationError.InvalidLength, 'BI must have 14 characters');
38
+ }
39
+
40
+ const upperBi = bi.toUpperCase();
41
+
42
+ if (!BiValidator.BiRegex.test(upperBi)) {
43
+ return BiValidationResult.failure(BiValidationError.InvalidFormat, 'BI format is invalid');
44
+ }
45
+
46
+ // Captura o código da província de forma dinâmica (se começa com K pega 3 letras, senão 2)
47
+ const provinceCode = upperBi.startsWith('K', 9)
48
+ ? upperBi.substring(9, 12)
49
+ : upperBi.substring(9, 11);
50
+
51
+ const province = BiValidator.Provinces[provinceCode];
52
+
53
+ if (!province) {
54
+ return BiValidationResult.failure(BiValidationError.InvalidProvince, 'Invalid province code');
55
+ }
56
+
57
+ return BiValidationResult.success(province);
58
+ }
59
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "NodeNext",
5
+ "moduleResolution": "NodeNext",
6
+ "declaration": true,
7
+ "outDir": "./dist",
8
+ "rootDir": "./src",
9
+ "strict": true,
10
+ "esModuleInterop": true,
11
+ "skipLibCheck": true,
12
+ "forceConsistentCasingInFileNames": true
13
+ },
14
+ "include": ["src/**/*"]
15
+ }